Android O添加桌面快捷方式的示例
手機(jī)升級(jí)到安卓O后,突然發(fā)現(xiàn)創(chuàng)建快捷方式的功能失效了,查詢一番后發(fā)現(xiàn):安卓O要使用ShortcutManager來(lái)創(chuàng)建快捷方式。
安卓N及以下版本:
Intent addShortcutIntent = new Intent("com.android.launcher.action.INSTALL_SHORTCUT");//"com.android.launcher.action.INSTALL_SHORTCUT"
// 不允許重復(fù)創(chuàng)建
addShortcutIntent.putExtra("duplicate", false);// 經(jīng)測(cè)試不是根據(jù)快捷方式的名字判斷重復(fù)的
// 應(yīng)該是根據(jù)快鏈的Intent來(lái)判斷是否重復(fù)的,即Intent.EXTRA_SHORTCUT_INTENT字段的value
// 但是名稱不同時(shí),雖然有的手機(jī)系統(tǒng)會(huì)顯示Toast提示重復(fù),仍然會(huì)建立快鏈
// 屏幕上沒(méi)有空間時(shí)會(huì)提示
// 注意:重復(fù)創(chuàng)建的行為MIUI和三星手機(jī)上不太一樣,小米上似乎不能重復(fù)創(chuàng)建快捷方式
// 名字
addShortcutIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME, "網(wǎng)絡(luò)設(shè)置");
// 圖標(biāo)
addShortcutIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE,
Intent.ShortcutIconResource.fromContext(this, R.drawable.ic_perm_data_setting_black_24dp));
// 設(shè)置關(guān)聯(lián)程序
Intent launcherIntent = new Intent(Settings.ACTION_DATA_ROAMING_SETTINGS);//設(shè)置網(wǎng)絡(luò)頁(yè)面intent
// 設(shè)置關(guān)聯(lián)程序
// Intent launcherIntent = new Intent(Intent.ACTION_MAIN);
// launcherIntent.setClass(MainActivity.this, MainActivity.class);
// launcherIntent.addCategory(Intent.CATEGORY_LAUNCHER);
addShortcutIntent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, launcherIntent);
// 發(fā)送廣播
sendBroadcast(addShortcutIntent);
安卓O:
ShortcutManager scm = (ShortcutManager) getSystemService(SHORTCUT_SERVICE);
Intent launcherIntent = new Intent(Settings.ACTION_DATA_ROAMING_SETTINGS);//設(shè)置網(wǎng)絡(luò)頁(yè)面intent
ShortcutInfo si = new ShortcutInfo.Builder(this, "dataroam")
.setIcon(Icon.createWithResource(this, R.drawable.ic_perm_data_setting_black_24dp))
.setShortLabel("網(wǎng)絡(luò)設(shè)置")
.setIntent(launcherIntent)
.build();
assert scm != null;
scm.requestPinShortcut(si, null);
那如果要兩者兼顧呢,則可以如下這樣寫(xiě):
//添加快捷方式
private void addShortcut() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
ShortcutManager scm = (ShortcutManager) getSystemService(SHORTCUT_SERVICE);
Intent launcherIntent = new Intent(Settings.ACTION_DATA_ROAMING_SETTINGS);//設(shè)置網(wǎng)絡(luò)頁(yè)面intent
ShortcutInfo si = new ShortcutInfo.Builder(this, "dataroam")
.setIcon(Icon.createWithResource(this, R.drawable.ic_perm_data_setting_black_24dp))
.setShortLabel("網(wǎng)絡(luò)設(shè)置")
.setIntent(launcherIntent)
.build();
assert scm != null;
scm.requestPinShortcut(si, null);
} else {
Intent addShortcutIntent = new Intent("com.android.launcher.action.INSTALL_SHORTCUT");//"com.android.launcher.action.INSTALL_SHORTCUT"
// 不允許重復(fù)創(chuàng)建
addShortcutIntent.putExtra("duplicate", false);// 經(jīng)測(cè)試不是根據(jù)快捷方式的名字判斷重復(fù)的
// 應(yīng)該是根據(jù)快鏈的Intent來(lái)判斷是否重復(fù)的,即Intent.EXTRA_SHORTCUT_INTENT字段的value
// 但是名稱不同時(shí),雖然有的手機(jī)系統(tǒng)會(huì)顯示Toast提示重復(fù),仍然會(huì)建立快鏈
// 屏幕上沒(méi)有空間時(shí)會(huì)提示
// 注意:重復(fù)創(chuàng)建的行為MIUI和三星手機(jī)上不太一樣,小米上似乎不能重復(fù)創(chuàng)建快捷方式
// 名字
addShortcutIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME, "網(wǎng)絡(luò)設(shè)置");
// 圖標(biāo)
addShortcutIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE,
Intent.ShortcutIconResource.fromContext(this, R.drawable.ic_perm_data_setting_black_24dp));
// 設(shè)置關(guān)聯(lián)程序
Intent launcherIntent = new Intent(Settings.ACTION_DATA_ROAMING_SETTINGS);//設(shè)置網(wǎng)絡(luò)頁(yè)面intent
// 設(shè)置關(guān)聯(lián)程序
// Intent launcherIntent = new Intent(Intent.ACTION_MAIN);
// launcherIntent.setClass(MainActivity.this, MainActivity.class);
// launcherIntent.addCategory(Intent.CATEGORY_LAUNCHER);
addShortcutIntent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, launcherIntent);
// 發(fā)送廣播
sendBroadcast(addShortcutIntent);
}
}
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Android文本視圖TextView實(shí)現(xiàn)聊天室效果
這篇文章主要介紹了Android文本視圖TextView實(shí)現(xiàn)聊天室效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-05-05
Android WebView無(wú)法加載H5頁(yè)面的常見(jiàn)問(wèn)題和解決方法
Android WebView 是一種視圖組件,使得 Android 應(yīng)用能夠顯示網(wǎng)頁(yè)內(nèi)容,它基于 Chromium,具備現(xiàn)代瀏覽器的許多功能,包括支持 HTML5、CSS3 和 JavaScript,本文給大家介紹了Android WebView無(wú)法加載H5頁(yè)面的常見(jiàn)問(wèn)題和解決方法,需要的朋友可以參考下2025-03-03
Android Toolbar自定義標(biāo)題標(biāo)題居中的實(shí)例代碼
這篇文章主要介紹了Android Toolbar自定義標(biāo)題 標(biāo)題居中的實(shí)例代碼,非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2018-08-08
Flutter快速制作一個(gè)水印組件實(shí)例詳解
這篇文章主要為大家介紹了Flutter快速制作一個(gè)水印組件實(shí)例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-01-01
Android判斷屏幕是橫屏或是豎屏的簡(jiǎn)單實(shí)現(xiàn)方法
這篇文章主要介紹了Android判斷屏幕是橫屏或是豎屏的簡(jiǎn)單實(shí)現(xiàn)方法,是Android應(yīng)用開(kāi)發(fā)中常用的功能,需要的朋友可以參考下2014-07-07
功能強(qiáng)大的登錄界面Android實(shí)現(xiàn)代碼
這篇文章主要為大家分享了功能強(qiáng)大的登錄界面Android實(shí)現(xiàn)代碼,驗(yàn)證碼制作方法,自帶一鍵刪除功能,用戶名密碼為空時(shí)抖動(dòng)提示效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-10-10
Android 中 requestWindowFeature()的應(yīng)用
本文主要介紹 Android requestWindowFeature()方法,這里對(duì) requestWindowFeature()方法進(jìn)行詳解,對(duì)應(yīng)用程序窗體顯示狀態(tài)的操作有進(jìn)一步了解,希望能幫助有需要的小伙伴2016-07-07
Android編程單元測(cè)試實(shí)例詳解(附源碼)
這篇文章主要介紹了Android編程單元測(cè)試,結(jié)合完整實(shí)例形式詳細(xì)分析了Android單元測(cè)試的具體步驟與相關(guān)技巧,并附帶完整實(shí)例代碼供讀者下載參考,需要的朋友可以參考下2015-11-11
Android Studio 運(yùn)行時(shí)出現(xiàn)的警告信息解決辦法
這篇文章主要介紹了Android Studio 運(yùn)行時(shí)出現(xiàn)的警告信息解決辦法的相關(guān)資料,需要的朋友可以參考下2017-06-06

