本文實(shí)例講述了Android添加(創(chuàng)建)、刪除及判斷是否存在桌面快捷方式的方法。分享給大家供大家參考。具體實(shí)現(xiàn)方法如下:
/*** 判斷桌面是否已添加快捷方式* * @param cx* @param titleName* 快捷方式名稱* @return*/public static boolean hasShortcut(Context cx) {boolean result = false;// 獲取當(dāng)前應(yīng)用名稱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;}/*** 刪除當(dāng)前應(yīng)用的桌面快捷方式* * @param cx*/public static void delShortcut(Context cx) {Intent shortcut = new Intent("com.android.launcher.action.UNINSTALL_SHORTCUT");// 獲取當(dāng)前應(yīng)用名稱String title = null;try {final PackageManager pm = cx.getPackageManager();title = pm.getApplicationLabel(pm.getApplicationInfo(cx.getPackageName(),PackageManager.GET_META_DATA)).toString();Log.v("test", "title:" + title);} catch (Exception e) {}// 快捷方式名稱shortcut.putExtra(Intent.EXTRA_SHORTCUT_NAME, title);Intent shortcutIntent = cx.getPackageManager().getLaunchIntentForPackage(cx.getPackageName());shortcut.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);cx.sendBroadcast(shortcut);}/*** 為當(dāng)前應(yīng)用添加桌面快捷方式* * @param cx* @param appName* 快捷方式名稱*/public static void addShortcut(Context cx) {Intent shortcut = new Intent("com.android.launcher.action.INSTALL_SHORTCUT");Intent shortcutIntent = cx.getPackageManager().getLaunchIntentForPackage(cx.getPackageName());shortcut.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);// 獲取當(dāng)前應(yīng)用名稱String title = null;try {final PackageManager pm = cx.getPackageManager();title = pm.getApplicationLabel(pm.getApplicationInfo(cx.getPackageName(),PackageManager.GET_META_DATA)).toString();Log.v("test", "title:" + title);} catch (Exception e) {}// 快捷方式名稱shortcut.putExtra(Intent.EXTRA_SHORTCUT_NAME, title);// 不允許重復(fù)創(chuàng)建(不一定有效)shortcut.putExtra("duplicate", false);// 快捷方式的圖標(biāo)Parcelable iconResource = Intent.ShortcutIconResource.fromContext(cx, R.drawable.icon);shortcut.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, iconResource);cx.sendBroadcast(shortcut);}
希望本文所述對(duì)大家的Android程序設(shè)計(jì)有所幫助。
新聞熱點(diǎn)
疑難解答
圖片精選