Android快捷圖標的創建與移除
/*** 添加桌面快捷方式 * * @param view */ public void click1(View view) { if (isExit()) { Toast.makeText(getApplicationContext(), "快捷方式已經存在", 0).show(); return; } // 取得圖標資源 Parcelable icon = Intent.ShortcutIconResource.fromContext( getApplicationContext(), R.drawable.logo); Intent intent = new Intent(); // 設置意圖的動作 intent.setAction("com.android.launcher.action.INSTALL_SHORTCUT"); // 設置圖標的名稱 intent.putExtra(Intent.EXTRA_SHORTCUT_NAME, "LOL"); // 設置圖標 intent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, icon); // 設置意圖的動作和類別 Intent intent2 = new Intent(); intent2.setAction(Intent.ACTION_MAIN); intent2.addCategory(Intent.CATEGORY_LAUNCHER); intent2.setComponent(new ComponentName(this, MainActivity.class)); intent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, intent2); sendBroadcast(intent); } </pre>
/*** 刪除桌面快捷方式 * * @param view */ public void click2(View view) { Parcelable icon = Intent.ShortcutIconResource.fromContext( getApplicationContext(), R.drawable.logo); Intent intent = new Intent(); // 設置意圖的動作 intent.setAction("com.android.launcher.action.UNINSTALL_SHORTCUT"); // 設置圖標的名稱 intent.putExtra(Intent.EXTRA_SHORTCUT_NAME, "LOL"); // 設置圖標 intent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, icon); // 設置意圖的動作和類別 Intent intent2 = new Intent(); intent2.setAction(Intent.ACTION_MAIN); intent2.addCategory(Intent.CATEGORY_LAUNCHER); intent2.setComponent(new ComponentName(this, MainActivity.class)); intent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, intent2); sendBroadcast(intent); } </pre>
/*** 判斷桌面是否已經存在快捷方式 * * @return */ private boolean isExit() { Uri uri = null; // 不同版本中的Uri不同 if (getSdkVersion() < 8) { uri = Uri .parse("content://com.android.launcher.settings/favorites"); } else { uri = Uri .parse("content://com.android.launcher2.settings/favorites"); } String selection = "title=?"; String[] selectionArgs = new String[] { "LOL" }; Cursor cursor = getContentResolver().query(uri, null, selection, selectionArgs, null); if (cursor.moveToNext()) { cursor.close(); return true; } else { cursor.close(); return false; } } </pre>
/*** 獲得手機SDK的版本 * * @return */ private int getSdkVersion() { return android.os.Build.VERSION.SDK_INT; } </pre>
需要聲明的權限<!-- 添加快捷方式權限 -->
<uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT" />
<!-- 讀取設置權限 -->
<uses-permission android:name="com.android.launcher.permission.READ_SETTINGS" />
<!-- 刪除快捷方式權限 -->
<uses-permission android:name="com.android.launcher.permission.UNINSTALL_SHORTCUT"/>
本文由用戶 jopen 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!