国产探花免费观看_亚洲丰满少妇自慰呻吟_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就可以了。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 自治县| 固镇县| 海南省| 电白县| 博客| 久治县| 思南县| 方正县| 天全县| 甘谷县| 克山县| 浪卡子县| 赞皇县| 喜德县| 同心县| 西充县| 柘荣县| 苗栗市| 平原县| 包头市| 浙江省| 南岸区| 永寿县| 绵竹市| 房山区| 芒康县| 博湖县| 巧家县| 湘阴县| 获嘉县| 高州市| 东明县| 东辽县| 贡山| 郑州市| 西充县| 兴义市| 涪陵区| 苍溪县| 新蔡县| 石渠县|