读取launcher数据库数据

[java] view plaincopy

  1. import java.util.List;  

  2.   

  3. import android.content.ContentResolver;  

  4. import android.content.Context;  

  5. import android.content.pm.PackageInfo;  

  6. import android.content.pm.PackageManager;  

  7. import android.content.pm.ProviderInfo;  

  8. import android.database.Cursor;  

  9. import android.net.Uri;  

  10. import android.util.Log;  

  11.   

  12. public class LauncherUtils {  

  13.   

  14.     public static void readLauncherDB(Context context) {  

  15.         final ContentResolver cr = context.getContentResolver();  

  16.         String AUTHORITY = getAuthorityFromPermission(context,  

  17.                 "com.android.launcher.permission.READ_SETTINGS");  

  18.         if (AUTHORITY == null) {  

  19.             AUTHORITY = getAuthorityFromPermission(context,  

  20.                     "com.android.launcher.permission.WRITE_SETTINGS");  

  21.         }  

  22.         if (AUTHORITY == null) {  

  23.             Log.e("""no authority");  

  24.             return;  

  25.         }  

  26.         Log.e("""authority:" + AUTHORITY);  

  27.   

  28.         Uri CONTENT_URI = Uri.parse("content://" + AUTHORITY  

  29.                 + "/favorites?notify=true");  

  30.   

  31.         Cursor c = cr.query(CONTENT_URI, nullnullnullnull);  

  32.         if (c != null) {  

  33.             while (c.moveToNext()) {  

  34.                 int id = c.getInt(c.getColumnIndex("_id"));  

  35.                 // 快捷方式的名称  

  36.                 String title = c.getString(c.getColumnIndex("title"));  

  37.                 // 快捷方式启动的对象  

  38.                 String intent = c.getString(c.getColumnIndex("intent"));  

  39.                 int container = c.getInt(c.getColumnIndex("container"));  

  40.                 // 快捷方式在哪个屏幕,默认是1  

  41.                 int screen = c.getInt(c.getColumnIndex("screen"));  

  42.                 // 快捷方式的位置  

  43.                 int cellX = c.getInt(c.getColumnIndex("cellX"));  

  44.                 int cellY = c.getInt(c.getColumnIndex("cellY"));  

  45.                 int spanX = c.getInt(c.getColumnIndex("spanX"));  

  46.                 int spanY = c.getInt(c.getColumnIndex("spanY"));  

  47.                 int itemType = c.getInt(c.getColumnIndex("itemType"));  

  48.                 int appWidgetId = c.getInt(c.getColumnIndex("appWidgetId"));  

  49.                 int isShortcut = c.getInt(c.getColumnIndex("isShortcut"));  

  50.                 int iconType = c.getInt(c.getColumnIndex("iconType"));  

  51.                 // 快捷方式的图标  

  52.                 String iconPackage = c.getString(c  

  53.                         .getColumnIndex("iconPackage"));  

  54.                 // "iconResource"  

  55.                 // "icon"  

  56.                 // "uri"  

  57.                 // "displayMode"  

  58.                 int launchCount = c.getInt(c.getColumnIndex("launchCount"));  

  59.                 // "sortMode"  

  60.                 int itemFlags = c.getInt(c.getColumnIndex("itemFlags"));  

  61.                 String log = "id=" + id + "|title=" + title + "|intent="  

  62.                         + intent + "|container=" + container + "|screen="  

  63.                         + screen + "|cell=[" + cellX + "," + cellY + "]|"  

  64.                         + "span=[" + spanX + "," + spanY + "]|itemType="  

  65.                         + itemType + "|appWidgetId=" + appWidgetId  

  66.                         + "|isShortcut=" + isShortcut + "|iconType=" + iconType  

  67.                         + "|iconPackage=" + iconPackage + "|launchCount="  

  68.                         + launchCount + "|itemFlags=" + itemFlags;  

  69.                 Log.e("", log);  

  70.             }  

  71.   

  72.         }  

  73.     }  

  74.   

  75.     /** 

  76.      * 获取authority 

  77.      * @param context 

  78.      * @param permission 

  79.      * @return  

  80.      */  

  81.     static String getAuthorityFromPermission(Context context, String permission) {  

  82.         if (permission == null)  

  83.             return null;  

  84.         List<PackageInfo> packs = context.getPackageManager()  

  85.                 .getInstalledPackages(PackageManager.GET_PROVIDERS);  

  86.         if (packs != null) {  

  87.             for (PackageInfo pack : packs) {  

  88.                 ProviderInfo[] providers = pack.providers;  

  89.                 if (providers != null) {  

  90.                     for (ProviderInfo provider : providers) {  

  91.                         if (permission.equals(provider.readPermission))  

  92.                             return provider.authority;  

  93.                         if (permission.equals(provider.writePermission))  

  94.                             return provider.authority;  

  95.                     }  

  96.                 }  

  97.             }  

  98.         }  

  99.         return null;  

  100.     }  

  101.   

  102. }  


你可能感兴趣的:(读取launcher数据库数据)