新聞中心

        EEPW首頁 > 嵌入式系統(tǒng) > 設計應用 > android 創(chuàng)建 刪除桌面快捷方式

        android 創(chuàng)建 刪除桌面快捷方式

        作者: 時間:2016-10-08 來源:網(wǎng)絡 收藏

        創(chuàng)建

        本文引用地址:http://www.104case.com/article/201610/305647.htm

        /**

        為程序創(chuàng)建桌面快捷方式

        */

        private void addShortcut(){

        Intent shortcut = new Intent(“com.android.launcher.action.INSTALL_SHORTCUT”);

        //快捷方式的名稱

        shortcut.putExtra(Intent.EXTRA_SHORTCUT_NAME, getString(R.string.app_name));

        shortcut.putExtra(“duplicate”, false); //不允許重復創(chuàng)建

        //指定當前的Activity為快捷方式啟動的對象: 如 com.everest.video.VideoPlayer

        //注意: ComponentName的第二個參數(shù)必須加上點號(.),否則快捷方式無法啟動相應程序

        ComponentName comp = new ComponentName(this.getPackageName(), “.“+this.getLocalClassName());

        shortcut.putExtra(Intent.EXTRA_SHORTCUT_INTENT, new Intent(Intent.ACTION_MAIN).setComponent(comp));

        //快捷方式的圖標

        ShortcutIconResource iconRes = Intent.ShortcutIconResource.fromContext(this, R.drawable.icon);

        shortcut.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, iconRes);

        sendBroadcast(shortcut);

        }/**

        為程序創(chuàng)建桌面快捷方式

        */

        private void addShortcut(){

        Intent shortcut = new Intent(“com.android.launcher.action.INSTALL_SHORTCUT”);

        //快捷方式的名稱

        shortcut.putExtra(Intent.EXTRA_SHORTCUT_NAME, getString(R.string.app_name));

        shortcut.putExtra(“duplicate”, false); //不允許重復創(chuàng)建

        //指定當前的Activity為快捷方式啟動的對象: 如 com.everest.video.VideoPlayer

        //注意: ComponentName的第二個參數(shù)必須加上點號(.),否則快捷方式無法啟動相應程序

        ComponentName comp = new ComponentName(this.getPackageName(), “.“+this.getLocalClassName());

        shortcut.putExtra(Intent.EXTRA_SHORTCUT_INTENT, new Intent(Intent.ACTION_MAIN).setComponent(comp));

        //快捷方式的圖標

        ShortcutIconResource iconRes = Intent.ShortcutIconResource.fromContext(this, R.drawable.icon);

        shortcut.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, iconRes);

        sendBroadcast(shortcut);

        }

        2, 刪除

        /**

        刪除程序的快捷方式

        */

        private void delShortcut(){

        Intent shortcut = new Intent(“com.android.launcher.action.UNINSTALL_SHORTCUT”);

        //快捷方式的名稱

        shortcut.putExtra(Intent.EXTRA_SHORTCUT_NAME, getString(R.string.app_name));

        //指定當前的Activity為快捷方式啟動的對象: 如 com.everest.video.VideoPlayer

        //注意: ComponentName的第二個參數(shù)必須是完整的類名(包名+類名),否則無法刪除快捷方式

        String appClass = this.getPackageName() + “.” +this.getLocalClassName();

        ComponentName comp = new ComponentName(this.getPackageName(), appClass);

        shortcut.putExtra(Intent.EXTRA_SHORTCUT_INTENT, new Intent(Intent.ACTION_MAIN).setComponent(comp));

        sendBroadcast(shortcut);

        }/**

        刪除程序的快捷方式

        */

        private void delShortcut(){

        Intent shortcut = new Intent(“com.android.launcher.action.UNINSTALL_SHORTCUT”);

        //快捷方式的名稱

        shortcut.putExtra(Intent.EXTRA_SHORTCUT_NAME, getString(R.string.app_name));

        //指定當前的Activity為快捷方式啟動的對象: 如 com.everest.video.VideoPlayer

        //注意: ComponentName的第二個參數(shù)必須是完整的類名(包名+類名),否則無法刪除快捷方式

        String appClass = this.getPackageName() + “.” +this.getLocalClassName();

        ComponentName comp = new ComponentName(this.getPackageName(), appClass);

        shortcut.putExtra(Intent.EXTRA_SHORTCUT_INTENT, new Intent(Intent.ACTION_MAIN).setComponent(comp));

        sendBroadcast(shortcut);

        }

        3, 聲明權限

        在AndroidManifest.xml 文件中聲明 創(chuàng)建和刪除快捷方式時聲明權限

        Java代碼



        關鍵詞:

        評論


        相關推薦

        技術專區(qū)

        關閉
        主站蜘蛛池模板: 高平市| 樟树市| 通海县| 苏尼特右旗| 玉林市| 长沙市| 古交市| 英山县| 陆良县| 东台市| 宁津县| 班玛县| 昌邑市| 丰都县| 海南省| 宣恩县| 彭泽县| 靖安县| 仁化县| 琼海市| 扬中市| 白沙| 遵义市| 进贤县| 房产| 怀宁县| 高陵县| 淅川县| 哈巴河县| 九龙坡区| 锡林浩特市| 谢通门县| 濮阳县| 离岛区| 舒城县| 罗山县| 东方市| 涪陵区| 广元市| 千阳县| 汶川县|