public static boolean hasShortcut(Context cx) { boolean result = false; // 获取当前应用名称 String title = null; try { final PackageManager pm = cx.getPackageManager(); title = pm.getApplicationLabel( pm.getApplicationInfo(cx.getPackageName(), PackageManager.GET_META_DATA)).toString(); } catch (Exception e) { } final String uriStr; if (android.os.Build.VERSION.SDK_INT < 8) { uriStr = "content://com.android.launcher.settings/favorites?notify=true"; } else { uriStr = "content://com.android.launcher2.settings/favorites?notify=true"; } final Uri CONTENT_URI = Uri.parse(uriStr); final Cursor c = cx.getContentResolver().query(CONTENT_URI, null, "title=?", new String[] { title }, null); if (c != null && c.getCount() > 0) { result = true; } return result; }
<uses-permission android:name="com.android.launcher.permission.READ_SETTINGS" />
几个相关的Action
// 系统启动完成 static final String BOOT_COMPLETED_ACTION = "android.intent.action.BOOT_COMPLETED"; // 设备上新安装了一个应用程序包 static final String PACKAGE_ADDED_ACTION = "android.intent.action.PACKAGE_ADDED"; // 设备上删除了一个应用程序包 static final String PACKAGE_REMOVED_ACTION = "android.intent.action.PACKAGE_REMOVED"; // 删除应用程序快捷方式,需要如下权限 // com.android.launcher.permission.UNINSTALL_SHORTCUT static final String UNINSTALL_SHORTCUT_ACTION = "com.android.launcher.action.UNINSTALL_SHORTCUT"; // 添加快捷方式,需要如下权限 // com.android.launcher.permission.INSTALL_SHORTCUT static final String INSTALL_SHORTCUT_ACTION = "com.android.launcher.permission.INSTALL_SHORTCUT";
4.监听app安装/卸载过程,需要用到上面的PACKAGE_ADDED和PACKAGE_REMOVED两个Action,可以对获取到的应用程序包名进行相应的判断处理;
@Override public void onReceive(Context context, Intent intent) { if (intent.getAction().equals(PACKAGE_ADDED_ACTION)) { // doSomething ...获取应用程序包名 String packageName = intent.getDataString(); } }
添加如下配置,对Receiver进行配置
<receiver android:name="com.example.async.BootBroadcastReceiver" > <intent-filter> <action android:name="android.intent.action.PACKAGE_ADDED" /> <!--PACKAGE_REMOVED--> <data android:scheme="package" /> <!-- 一定要添加此节点 --> </intent-filter> </receiver>