(8)Launcher3客制化之ContentProvider内容提供者,实现其它应用改动数据库更新等操作

时间:2022-08-21 17:02:41

首先加入两个权限

<uses-permission android:name="com.android.launcher3.permission.READ_SETTINGS"/>

 <uses-permission android:name="com.android.launcher3.permission.WRITE_SETTINGS"/>

这两个是改动 桌面的数据库的权限

不加入就改动不了哦

直接通过内容提供者获取数据,

	static ArrayList<ShortcutInfo> getItemsInLocalCoordinates(Context context) {
ArrayList<ShortcutInfo> items = new ArrayList<ShortcutInfo>();
final ContentResolver cr = context.getContentResolver();
Cursor c = cr.query(LauncherSettings.Favorites.CONTENT_URI, new String[] {
LauncherSettings.Favorites.ITEM_TYPE, LauncherSettings.Favorites.CONTAINER,
LauncherSettings.Favorites.SCREEN, LauncherSettings.Favorites.CELLX, LauncherSettings.Favorites.CELLY,
LauncherSettings.Favorites.SPANX, LauncherSettings.Favorites.SPANY ,LauncherSettings.Favorites.TITLE,LauncherSettings.Favorites.INTENT}, null, null, null); final int itemTypeIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ITEM_TYPE);
final int containerIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CONTAINER);
final int screenIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.SCREEN);
final int cellXIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLX);
final int cellYIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLY);
final int spanXIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.SPANX);
final int spanYIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.SPANY);
final int titleIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.TITLE);
final int intenIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.INTENT);
try {
while (c.moveToNext()) {
ShortcutInfo item = new ShortcutInfo();
item.cellX = c.getInt(cellXIndex);
item.cellY = c.getInt(cellYIndex);
item.spanX = Math.max(1, c.getInt(spanXIndex));
item.spanY = Math.max(1, c.getInt(spanYIndex));
item.container = c.getInt(containerIndex);
item.itemType = c.getInt(itemTypeIndex);
item.screenId = c.getInt(screenIndex);
item.title=c.getString(titleIndex);
if(c.getString(intenIndex)!=null){
item.intent=new Intent(c.getString(intenIndex));
} items.add(item);
}
} catch (Exception e) {
items.clear();
} finally {
c.close();
} return items;
}

改动内容同理通过对于的URL进行改动。

详细我就不进行代码编写了。