Android程序開(kāi)發(fā)之手機(jī)APP創(chuàng)建桌面快捷方式
預(yù)覽效果圖:

需要權(quán)限:
<uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT" />
配置文件:AndroidManifest.xml
<activity android:name="com.myself.news.activity.GuideActivity" android:label="@string/title_activity_guide" > <intent-filter> <action android:name="com.myself.news.ACTION_HOME" /> <category android:name="android.intent.category.DEFAULT" /> </intent-filter> </activity>
在應(yīng)用的閃屏頁(yè)面Activity的 oncreate方法調(diào)用 installShortcut();
代碼:
// 創(chuàng)建快捷方式
// com.android.launcher.permission.INSTALL_SHORTCUT
private void installShortcut() {
// 判斷有沒(méi)有創(chuàng)建過(guò)快捷方式
boolean isCreated = SharedPreferencesUtils.getBoolean(this,
GlobalConstantsUtils.PREF_IS_SHORTCUT_INTALLED, false);
// 判斷是否已經(jīng)創(chuàng)建過(guò)
if (!isCreated) {
// 發(fā)廣播
Intent intent = new Intent();
intent.setAction("com.android.launcher.action.INSTALL_SHORTCUT");
// 圖標(biāo)
// 根據(jù)資源文件id生成Bitmap對(duì)象
intent.putExtra(Intent.EXTRA_SHORTCUT_ICON, BitmapFactory
.decodeResource(getResources(), R.drawable.ic_launcher));
// 名稱(chēng)
intent.putExtra(Intent.EXTRA_SHORTCUT_NAME, "手機(jī)安全衛(wèi)士");
// 動(dòng)作
Intent actionIntent = new Intent();
// 跳到主頁(yè)面
actionIntent.setAction(GlobalConstantsUtils.ACTION_HOME);
intent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, actionIntent);
sendBroadcast(intent);
// 標(biāo)記已經(jīng)創(chuàng)建過(guò)快捷方式,下次不再創(chuàng)建
SharedPreferencesUtils.setBoolean(this,
GlobalConstantsUtils.PREF_IS_SHORTCUT_INTALLED, true);
}
}
常量工具類(lèi)GlobalConstantsUtils:
public static final String PREF_IS_SHORTCUT_INTALLED = "is_shortcut_intalled";// 是否已經(jīng)創(chuàng)建快捷方式 public static final String ACTION_HOME = "com.myself.news.ACTION_HOME";// 跳轉(zhuǎn)到主頁(yè)面的ACTION
相關(guān)文章
Android啟動(dòng)初始化方案App StartUp的應(yīng)用詳解
這篇文章主要介紹了Android啟動(dòng)初始化方案App StartUp的使用方法,StartUp是為了App的啟動(dòng)提供的一套簡(jiǎn)單、高效的初始化方案,下面我們來(lái)詳細(xì)了解2022-09-09
Android開(kāi)發(fā)之圖形圖像與動(dòng)畫(huà)(一)Paint和Canvas類(lèi)學(xué)習(xí)
Paint類(lèi)代表畫(huà)筆,用來(lái)描述圖形的顏色和風(fēng)格,如線寬,顏色,透明度和填充效果等信息;Canvas類(lèi)代表畫(huà)布,通過(guò)該類(lèi)提供的構(gòu)造方法,可以繪制各種圖形;感興趣的朋友可以了解下啊,希望本文對(duì)你有所幫助2013-01-01
RecyclerVIew實(shí)現(xiàn)懸浮吸頂效果
這篇文章主要為大家詳細(xì)介紹了RecyclerVIew實(shí)現(xiàn)懸浮吸頂效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-09-09
Android Recyclerview實(shí)現(xiàn)多選,單選,全選,反選,批量刪除的功能
本篇文章主要介紹了Android Recyclerview 實(shí)現(xiàn)多選,單選,全選,反選,批量刪除的功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-06-06
懸浮對(duì)話框Android代碼實(shí)現(xiàn)
這篇文章主要為大家詳細(xì)介紹了懸浮對(duì)話框Android代碼實(shí)現(xiàn),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-08-08
Android畫(huà)板開(kāi)發(fā)之基本畫(huà)筆功能
這篇文章主要為大家詳細(xì)介紹了Android畫(huà)板開(kāi)發(fā)之基本畫(huà)筆功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-12-12

