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

首頁 > 系統(tǒng) > Android > 正文

Android應用創(chuàng)建桌面快捷方式代碼

2019-12-12 05:08:19
字體:
供稿:網(wǎng)友

android的快捷方式比較簡單,就是發(fā)一個系統(tǒng)的廣播,然后為快捷方式設置Intent---

package com.xikang.android.slimcoach.utils;/** * @author huiych * 創(chuàng)建快捷方式 * @created 2013-02-21 * */import android.content.Intent;import android.os.Parcelable;import com.xikang.android.slimcoach.AppXiKang;import com.xikang.android.slimcoach.R;import com.xikang.android.slimcoach.ui.AppStart;public class ShortCutUtil { public static void initShortCut(){ Intent addShortCut = new Intent("com.android.launcher.action.INSTALL_SHORTCUT"); //不能重復創(chuàng)建快捷方式 addShortCut.putExtra("duplicate", false);    String title = AppXiKang.getApp().getString(R.string.app_name);//名稱     Parcelable icon = Intent.ShortcutIconResource.fromContext(AppXiKang.getApp(), R.drawable.icon);//圖標     //點擊快捷方式后操作Intent,快捷方式建立后,再次啟動該程序      Intent intent = new Intent(AppXiKang.getApp(), AppStart.class);      //設置快捷方式的標題      addShortCut.putExtra(Intent.EXTRA_SHORTCUT_NAME, title);      //設置快捷方式的圖標      addShortCut.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, icon);      //設置快捷方式對應的Intent      addShortCut.putExtra(Intent.EXTRA_SHORTCUT_INTENT, intent);     //發(fā)送廣播添加快捷方式      AppXiKang.getApp().sendBroadcast(addShortCut); }}

AppXiKange.getApp(),是獲取Activity對象。

注意,要在清單文件中設置權(quán)限

復制代碼 代碼如下:
<uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT"/>

這樣在希望增加快捷方式的時候,就可以給用戶一個alertdialog,提示,然后引用。就可以了。

市場上也有很多應用是在應用安裝的時候直接創(chuàng)建快捷方式。不過這樣的實現(xiàn)不是很友好。不建議使用。

下面上個完整的代碼演示,使用的方法和上面的稍有不同:

public class ShortCutUtil {public static void initShortCut(Activity acti){Intent shortcut = new Intent("com.android.launcher.action.INSTALL_SHORTCUT"); //快捷方式的名稱 shortcut.putExtra(Intent.EXTRA_SHORTCUT_NAME, AppXiKang.getApp().getString(R.string.app_name)); shortcut.putExtra("duplicate", false); //不允許重復創(chuàng)建 //指定當前的Activity為快捷方式啟動的對象: 如 //com.everest.video.VideoPlayer //注意: ComponentName的第二個參數(shù)必須加上點號(.),否則快捷方式無法啟動相應程序 ComponentName comp = new ComponentName(AppXiKang.getApp().getPackageName(), "."+acti.getLocalClassName()); shortcut.putExtra(Intent.EXTRA_SHORTCUT_INTENT, new Intent(Intent.ACTION_MAIN).setComponent(comp)); //快捷方式的圖標 ShortcutIconResource iconRes = Intent.ShortcutIconResource.fromContext(AppXiKang.getApp(), R.drawable.icon); shortcut.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, iconRes); AppXiKang.getApp().sendBroadcast(shortcut);}public static void delShortcut(Activity acti){    Intent shortcut = new Intent("com.android.launcher.action.UNINSTALL_SHORTCUT");             //快捷方式的名稱     shortcut.putExtra(Intent.EXTRA_SHORTCUT_NAME, AppXiKang.getApp().getString(R.string.app_name));     String appClass = AppXiKang.getApp().getPackageName() + "." +acti.getLocalClassName();     ComponentName comp = new ComponentName(AppXiKang.getApp().getPackageName(), appClass);     shortcut.putExtra(Intent.EXTRA_SHORTCUT_INTENT, new Intent(Intent.ACTION_MAIN).setComponent(comp));     AppXiKang.getApp().sendBroadcast(shortcut);  }}

以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持武林網(wǎng)。

發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 汶上县| 内丘县| 广德县| 城固县| 漳平市| 赣榆县| 咸宁市| 汾西县| 昌邑市| 西宁市| 连城县| 济源市| 三江| 绿春县| 乌拉特前旗| 红原县| 碌曲县| 兴海县| 海口市| 鹤岗市| 卢氏县| 平阳县| 海淀区| 垣曲县| 普宁市| 平乡县| 马山县| 青神县| 新乐市| 六盘水市| 安陆市| 云阳县| 康马县| 陆川县| 新津县| 乾安县| 嘉义县| 九江县| 邢台市| 宜宾县| 长兴县|