1添加到Shortcut選項中
/** * 添加到Shortcut選項中(預設案頭上長按調出) * * 同時需要在manifest中為activity提供一個包含 * action="android.intent.action.CREATE_SHORTCUT"的intent-filter */ private void addShortcutToOptions(){ // 建立一個預設的Intent Intent shortcut = new Intent(); //捷徑的名稱 shortcut.putExtra(Intent.EXTRA_SHORTCUT_NAME, getString(R.string.app_name)); //不允許重複建立 shortcut.putExtra("duplicate", false); //指定當前的Activity為捷徑啟動的對象: 如 com.everest.video.VideoPlayer //注意: ComponentName的第二個參數必須加上點號(.),否則捷徑無法啟動相應程式 String appClass = this.getPackageName() + "." +this.getLocalClassName(); ComponentName comp = new ComponentName(this.getPackageName(), appClass); shortcut.putExtra(Intent.EXTRA_SHORTCUT_INTENT, newIntent(Intent.ACTION_MAIN).setComponent(comp)); // 下面的方法與上面的效果是一樣的,另一種構建形式而已 // Intent respondIntent = new Intent(this, this.getClass()); // shortcut.putExtra(Intent.EXTRA_SHORTCUT_INTENT, respondIntent); //捷徑的表徵圖 ShortcutIconResource iconRes = Intent.ShortcutIconResource.fromContext(this, R.drawable.icon); shortcut.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, iconRes); // 發送到訊息佇列 setResult(RESULT_OK, shortcut); }
2.為程式建立案頭捷徑
/** * 為程式建立案頭捷徑 * * 同時需要在manifest中設定以下許可權: * <uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT" /> */ private void addShortcut(){ Intent shortcut = newIntent("com.android.launcher.action.INSTALL_SHORTCUT"); // 捷徑的名稱 shortcut.putExtra(Intent.EXTRA_SHORTCUT_NAME, getString(R.string.app_name)); // 不允許重複建立 shortcut.putExtra("duplicate", false); // 指定當前的Activity為捷徑啟動的對象: 如 com.everest.video.VideoPlayer // 這裡必須為Intent設定一個action,可以任意(但安裝和卸載時該參數必須一致) String action = "com.android.action.test"; Intent respondIntent = new Intent(this, this.getClass()); respondIntent.setAction(action); shortcut.putExtra(Intent.EXTRA_SHORTCUT_INTENT, respondIntent); // 下面的方法與上面的效果是一樣的,另一種構建形式而已 // 注意: ComponentName的第二個參數必須加上點號(.),否則捷徑無法啟動相應程式 // String appClass = this.getPackageName() + "." + this.getLocalClassName(); // ComponentName comp = new ComponentName(this.getPackageName(), appClass); // shortcut.putExtra(Intent.EXTRA_SHORTCUT_INTENT, new Intent(action).setComponent(comp)); // 捷徑的表徵圖 ShortcutIconResource iconRes = Intent.ShortcutIconResource.fromContext(this, R.drawable.icon); shortcut.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, iconRes); sendBroadcast(shortcut); }
3.刪除程式的捷徑
/** * 刪除程式的捷徑 * * 同時需要在manifest中設定以下許可權: * <uses-permission android:name="com.android.launcher.permission.UNINSTALL_SHORTCUT" /> */ private void delShortcut() { Intent shortcut = newIntent("com.android.launcher.action.UNINSTALL_SHORTCUT"); // 捷徑的名稱 shortcut.putExtra(Intent.EXTRA_SHORTCUT_NAME, getString(R.string.app_name)); // 指定當前的Activity為捷徑啟動的對象: 如 com.everest.video.VideoPlayer // 這裡必須為Intent設定一個action,可以任意(但安裝和卸載時該參數必須一致) String action = "com.android.action.test"; Intent respondIntent = new Intent(this, this.getClass()); respondIntent.setAction(action); shortcut.putExtra(Intent.EXTRA_SHORTCUT_INTENT, respondIntent); // 下面的方法與上面的效果是一樣的,另一種構建形式而已 // 注意: ComponentName的第二個參數必須加上點號(.),否則捷徑無法啟動相應程式 // String appClass = this.getPackageName() + "." + this.getLocalClassName(); // ComponentName comp = new ComponentName(this.getPackageName(), appClass); // shortcut.putExtra(Intent.EXTRA_SHORTCUT_INTENT, new Intent(action).setComponent(comp)); sendBroadcast(shortcut); }
來自:http://www.eoeandroid.com/thread-92125-1-1.html
4.判斷是否存在捷徑
/** * 判斷案頭是否已添加捷徑 * * @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;}
上面內容從網上找來的,不過我發現我在三星4.0的機器上判斷是否存在捷徑時總是返回false。然後看了一下系統源碼,找到了一個系統用來判斷是否存在捷徑的方法。
/** * Returns true if the shortcuts already exists in the database. * we identify a shortcut by its title and intent. */ static boolean shortcutExists(Context context, String title, Intent intent) { final ContentResolver cr = context.getContentResolver(); Cursor c = cr.query(LauncherSettings.Favorites.CONTENT_URI, new String[] { "title", "intent" }, "title=? and intent=?", new String[] { title, intent.toUri(0) }, null); boolean result = false; try { result = c.moveToFirst(); } finally { c.close(); } return result; }
LauncherSettings.Favorites.CONTENT_URI = Uri.parse("content://com.android.launcher2.settings/favorites?notify=true")
上方法中的intent指的是用來建立捷徑的Intent。上面方法的大概意思就是判斷資料庫裡是否有了一條title相同,intent轉成的字串相同的資料,有就是此捷徑存在。
嗯,還是沒有解決問題,查詢這個表得到null,三星改了資料庫吧!
網上的東西不可盡信,上面建立捷徑的代碼中是存在一個問題的,就是第一次登進程式,按home,再進去又會建立一個Activity而不會恢複原來的,注意:還一種情況能導致這種現象的出現,那就是給第一個Activity設定了SingleTask之類的launchMode。在另一種形式代碼中加上一句
respondIntent.addCategory("android.intent.category.LAUNCHER");
修複此問題。