Android添加(創(chuàng)建)、刪除及判斷是否存在桌面快捷方式的方法
更新時間:2015年05月21日 10:12:01 作者:3H
這篇文章主要介紹了Android添加(創(chuàng)建)、刪除及判斷是否存在桌面快捷方式的方法,涉及Android針對桌面快捷方式的相關操作技巧,需要的朋友可以參考下
本文實例講述了Android添加(創(chuàng)建)、刪除及判斷是否存在桌面快捷方式的方法。分享給大家供大家參考。具體實現(xiàn)方法如下:
/** * 判斷桌面是否已添加快捷方式 * * @param cx * @param titleName * 快捷方式名稱 * @return */ 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; } /** * 刪除當前應用的桌面快捷方式 * * @param cx */ public static void delShortcut(Context cx) { Intent shortcut = new Intent( "com.android.launcher.action.UNINSTALL_SHORTCUT"); // 獲取當前應用名稱 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); } /** * 為當前應用添加桌面快捷方式 * * @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); // 獲取當前應用名稱 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); // 不允許重復創(chuàng)建(不一定有效) shortcut.putExtra("duplicate", false); // 快捷方式的圖標 Parcelable iconResource = Intent.ShortcutIconResource.fromContext(cx, R.drawable.icon); shortcut.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, iconResource); cx.sendBroadcast(shortcut); }
希望本文所述對大家的Android程序設計有所幫助。
相關文章
Android編程實現(xiàn)圖片的瀏覽、縮放、拖動和自動居中效果
這篇文章主要介紹了Android編程實現(xiàn)圖片的瀏覽、縮放、拖動和自動居中效果,以具體實例形式分析了Android針對圖片各種常見顯示效果的布局及功能實現(xiàn)技巧,需要的朋友可以參考下2015-11-11Android使用ContentResolver搜索手機通訊錄的方法
這篇文章主要介紹了Android使用ContentResolver搜索手機通訊錄的方法,結合實例形式分析了Android中ContentResolver操作手機通訊錄的具體步驟與相關實現(xiàn)技巧,需要的朋友可以參考下2016-01-01android設置adb自帶screenrecord錄屏命令
這篇文章主要介紹了android設置adb自帶screenrecord錄屏命令,需要的朋友可以參考下2018-11-11老生常談ProgressBar、ProgessDialog的用法
下面小編就為大家?guī)硪黄仙U凱rogressBar、ProgessDialog的用法。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-07-07