Android應(yīng)用創(chuàng)建桌面快捷方式代碼
android的快捷方式比較簡(jiǎn)單,就是發(fā)一個(gè)系統(tǒng)的廣播,然后為快捷方式設(shè)置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"); //不能重復(fù)創(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);//圖標(biāo) //點(diǎn)擊快捷方式后操作Intent,快捷方式建立后,再次啟動(dòng)該程序 Intent intent = new Intent(AppXiKang.getApp(), AppStart.class); //設(shè)置快捷方式的標(biāo)題 addShortCut.putExtra(Intent.EXTRA_SHORTCUT_NAME, title); //設(shè)置快捷方式的圖標(biāo) addShortCut.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, icon); //設(shè)置快捷方式對(duì)應(yīng)的Intent addShortCut.putExtra(Intent.EXTRA_SHORTCUT_INTENT, intent); //發(fā)送廣播添加快捷方式 AppXiKang.getApp().sendBroadcast(addShortCut); } }
AppXiKange.getApp(),是獲取Activity對(duì)象。
注意,要在清單文件中設(shè)置權(quán)限
這樣在希望增加快捷方式的時(shí)候,就可以給用戶一個(gè)alertdialog,提示,然后引用。就可以了。
市場(chǎng)上也有很多應(yīng)用是在應(yīng)用安裝的時(shí)候直接創(chuàng)建快捷方式。不過(guò)這樣的實(shí)現(xiàn)不是很友好。不建議使用。
下面上個(gè)完整的代碼演示,使用的方法和上面的稍有不同:
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); //不允許重復(fù)創(chuàng)建 //指定當(dāng)前的Activity為快捷方式啟動(dòng)的對(duì)象: 如 //com.everest.video.VideoPlayer //注意: ComponentName的第二個(gè)參數(shù)必須加上點(diǎn)號(hào)(.),否則快捷方式無(wú)法啟動(dòng)相應(yīng)程序 ComponentName comp = new ComponentName(AppXiKang.getApp().getPackageName(), "."+acti.getLocalClassName()); shortcut.putExtra(Intent.EXTRA_SHORTCUT_INTENT, new Intent(Intent.ACTION_MAIN).setComponent(comp)); //快捷方式的圖標(biāo) 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); } }
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
解決Android V7后自定義Toolbar、ActionBar左側(cè)有空白問(wèn)題
這篇文章主要介紹的Android V7后自定義Toolbar、ActionBar左側(cè)有空白問(wèn)題的解決方法,需要的朋友可以參考下2017-04-04Android實(shí)現(xiàn)圖片添加陰影效果的2種方法
這篇文章主要介紹了Android實(shí)現(xiàn)圖片添加陰影效果的2種方法,第一種方法是自定義drawable,第二種方式就是自定義view,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-11-11Android中的Bmob移動(dòng)后端云服務(wù)器功能
這里介紹一個(gè)移動(dòng)后端云服務(wù)器平臺(tái)bmob,這不僅可以實(shí)現(xiàn)云數(shù)據(jù)庫(kù)儲(chǔ)存,還可以獲取手機(jī)驗(yàn)證等,隨時(shí)隨地都很輕松,下面寫(xiě)一個(gè)小demo,實(shí)現(xiàn)一個(gè)登陸注冊(cè)功能,認(rèn)識(shí)增刪查改2018-01-01解決Android-RecyclerView列表倒計(jì)時(shí)錯(cuò)亂問(wèn)題
這篇文章主要介紹了解決Android-RecyclerView列表倒計(jì)時(shí)錯(cuò)亂問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-08-08Android UI控件RatingBar實(shí)現(xiàn)自定義星星評(píng)分效果
這篇文章主要為大家詳細(xì)介紹了Android UI控件RatingBar實(shí)現(xiàn)自定義星星評(píng)分效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-02-02仿iPhone風(fēng)格對(duì)話框(附件包含例子/jar包/jar包源碼)
這個(gè)對(duì)框完全繼承、仿照AlertDialog,只是實(shí)現(xiàn)了自定義效果;另外,沒(méi)有實(shí)現(xiàn)setIcon,因?yàn)閕phone中的對(duì)話框多數(shù)都沒(méi)有圖標(biāo);附件包含例子、jar包、jar包源碼2013-01-01Android Studio 報(bào)錯(cuò)“app:processDebugResources"解決方法
這篇文章主要介紹了Android Studio 報(bào)錯(cuò)“app:processDebugResources"解決方法的相關(guān)資料,需要的朋友可以參考下2017-07-07Android中通知Notification使用實(shí)例(振動(dòng)、燈光、聲音)
這篇文章主要介紹了Android中通知Notification使用實(shí)例,實(shí)現(xiàn)振動(dòng),燈光,聲音等效果,感興趣的小伙伴們可以參考一下2016-01-01