国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統 > Android > 正文

Android中創建快捷方式代碼實例

2020-04-11 11:34:47
字體:
來源:轉載
供稿:網友

1、添加權限(必須)

復制代碼 代碼如下:

<uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT" />

2、添加快捷鍵

復制代碼 代碼如下:

    public static void setupShortcut(Activity activity)
    {
        Intent shortcutIntent = new Intent(activity, MainActivity.class); //啟動首頁(launcher Activity)

        Intent intent = new Intent("com.android.launcher.action.INSTALL_SHORTCUT");
        intent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);
        intent.putExtra(Intent.EXTRA_SHORTCUT_NAME, "hello");//快捷鍵名字可以任意,不過最好為app名稱
        Parcelable iconResource = Intent.ShortcutIconResource.fromContext(activity, R.drawable.ic_launcher);
        intent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, iconResource);
        intent.putExtra("duplicate", false);//不允許重復創建

        activity.sendBroadcast(intent);//發送廣播創建快捷鍵
    }

3、快捷鍵也可以指向非Launcher activity,只需要在AndroidManifest中對應的Activity 中添加如下配置

復制代碼 代碼如下:

<intent-filter>
     <action android:name="android.intent.action.CREATE_SHORTCUT" />
<intent-filter>

例如可以將2 中的MainActivity 改為任意其他Activity,同時在AndroidManifest中對應添加上述intent-filter就可以了。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 丁青县| 都兰县| 文成县| 重庆市| 叶城县| 革吉县| 谢通门县| 潼关县| 宾阳县| 宝兴县| 青田县| 惠安县| 夹江县| 鲁山县| 濉溪县| 文昌市| 福建省| 巍山| 黔西县| 南陵县| 德令哈市| 炉霍县| 中西区| 蓬莱市| 乌恰县| 三江| 德州市| 安吉县| 林西县| 鹿邑县| 濮阳市| 黄浦区| 淮安市| 东乌珠穆沁旗| 东宁县| 贡嘎县| 古蔺县| 彭水| 泾源县| 东安县| 镇巴县|