Android Shortcut操作(捷徑)

來源:互聯網
上載者:User

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");

修複此問題。

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.