淺談Android添加快捷方式ShortCut
眾所周知application有4種啟動(dòng)方式:
- 點(diǎn)擊app啟動(dòng)
- 快捷方式
- 通知跳轉(zhuǎn)
- 輸入命令(adb命令等)
今天給大家簡單介紹一下快捷方式啟動(dòng)的用法~
快捷方式介紹
谷歌官方在Android 7.1(API 25)新增了桌面長按彈出菜單,并且在8.0(API 26)以后可以固定快捷方式至桌面上。圍繞桌面快捷方式的需求也比較多,例如微信將聯(lián)系人、小程序都可以添加至桌面;簡書將“寫文章”添加至桌面;高德將“坐標(biāo)信息”添加到桌面。
快捷方式情景再現(xiàn)
將某個(gè)應(yīng)用添加到桌面

長按應(yīng)用打開某一個(gè)功能

快捷方式使用
將某個(gè)應(yīng)用添加到桌面
先看代碼,后面我會(huì)將這些代碼寫成工具類供大家使用:
/**
* @param context 當(dāng)前content
* @param targetClass 快捷圖標(biāo)打開的界面
* @param backClass 打開后按返回鍵返回的界面
* @param shortCutId shortCut 唯一id
* @param shortCutIcon 桌面上顯示的圖標(biāo)
*/
public void AddShortCut(Context context, Class targetClass, Class backClass, int shortCutId, int shortCutIcon) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
ShortcutManager shortcutManager = (ShortcutManager) context.getSystemService(Context.SHORTCUT_SERVICE);
if (shortcutManager != null && shortcutManager.isRequestPinShortcutSupported()) {
Intent shortcutInfoIntent = new Intent(context, targetClass);
shortcutInfoIntent.setAction(Intent.ACTION_VIEW);
ShortcutInfo info = new ShortcutInfo.Builder(context, "id" + shortCutId)
.setIcon(Icon.createWithResource(context, shortCutIcon)).
setShortLabel(titles[shortCutId]).setIntent(shortcutInfoIntent).build();
PendingIntent shortcutCallbackIntent = PendingIntent.getBroadcast(context, 0, new Intent(context, backClass), PendingIntent.FLAG_UPDATE_CURRENT);
shortcutManager.requestPinShortcut(info, shortcutCallbackIntent.getIntentSender());
}
} else {
Toast.makeText(context, "設(shè)備不支持在桌面創(chuàng)建快捷圖標(biāo)!", Toast.LENGTH_LONG).show();
}
}
測(cè)試:
shortUtil.AddShortCut(
this,
MainActivity::class.java,
MainActivity::class.java,
2,
R.drawable.ic_launcher_background
)
效果圖(1.1):

修改快捷方式:
/**
* @param context 上下文
* @param cls 要跳轉(zhuǎn)的頁面
* @param shortCutId shortCut 唯一id
* @param shortCutIcon 桌面上顯示的圖標(biāo)
* @param shortCutLabel 桌面圖標(biāo)下方顯示的文字
*/
public void updItem(Context context, Class<?> cls, int shortCutId, int shortCutIcon, String shortCutLabel) {
Intent intent = new Intent(context, cls);
intent.setAction(Intent.ACTION_VIEW);
intent.putExtra("msg", titles[shortCutId]);
ShortcutInfo info = null;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N_MR1) {
info = new ShortcutInfo.Builder(context, "id" + shortCutId)
.setIcon(Icon.createWithResource(context, shortCutIcon))
.setShortLabel(shortCutLabel)
.setIntent(intent)
.build();
sm.updateShortcuts(Arrays.asList(info));
}
}
測(cè)試:
shortUtil.updItem(
this,
ModifyActivity::class.java,
2,
R.drawable.ic_launcher_background,
"修改快捷方式成功"
)
效果圖(1.2):

禁用快捷方式:
/**
* 禁止使用快捷方式
*
* @param index 禁止使用下標(biāo)
*/
public void remove(int index) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N_MR1) {
sm.removeAllDynamicShortcuts();
List<String> list = new ArrayList<String>();
list.add("id" + index);
sm.disableShortcuts(list);
}
}
測(cè)試:
shortUtil.remove(2)
效果圖(1.3):

長按應(yīng)用打開某一個(gè)功能:
這里以Fragment舉例:
先來看看最終的效果:

主要代碼:
private static int[] icons = {R.drawable.ic_launcher_background, R.drawable.ic_launcher_foreground, R.drawable.ic_launcher_background, R.drawable.ic_launcher_foreground,};
private static String[] titles = {"首頁", "我的", "詳情", "設(shè)置"};
/**
* 設(shè)置默認(rèn)快捷方式
*
* @param context 上下文
* @param ast 跳轉(zhuǎn)頁面
*/
public void setShortCuts(Context context, Class ast) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N_MR1) {
ArrayList<ShortcutInfo> list = new ArrayList<>();
for (int i = 0; i < titles.length; i++) {
Intent intent = new Intent(context, ast);
intent.setAction(Intent.ACTION_VIEW);
intent.putExtra("msg", titles[i]);
intent.putExtra(SHORTCUT_TAB_INDEX, i);
intent.addCategory("android.intent.category.LAUNCHER");
ShortcutInfo build = new ShortcutInfo.Builder(context, "id" + i)
.setShortLabel(titles[i])
.setLongLabel(titles[i])
.setIcon(Icon.createWithResource(context, icons[i]))
.setIntent(intent)
.build();
list.add(build);
}
sm.setDynamicShortcuts(list);
} else {
Toast.makeText(context, "該設(shè)備不支持快捷方式", Toast.LENGTH_SHORT).show();
}
}
在Application中注冊(cè)一下:
記得在清單文件聲明哦
// 保存按鈕
val radiolist = listOf(radioButton1, radioButton2, radioButton3, radioButton4)
//快捷方式打開
initShort {
val arg0 = intent?.extras?.getInt(ShortCutUtil.SHORTCUT_TAB_INDEX)
if (arg0 != null) {
val let = arg0.let {
radioGroup.getChildAt(it)
}
val bun = Bundle()
bun["title"] = (let as RadioButton).text as String
//傳值
blankFragment.setArguments(bun)
radiolist[arg0].isChecked = true
}
}
private fun initShort(arg0: () -> Unit) {
arg0.invoke()
}
private operator fun Bundle.set(key: String, value: String) {
this.putString(key, value)
}
注意:這段代碼使用kotlin寫的,因?yàn)閯倓?chuàng)建項(xiàng)目的時(shí)候只能創(chuàng)建kotlin,我就懶的改了
Fragment代碼很簡單,通過Arguments獲取到值賦值到TextView上即可這里就不貼代碼了
以上就是淺談Android添加快捷方式ShortCut的詳細(xì)內(nèi)容,更多關(guān)于Android快捷方式的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
android使用ViewPager實(shí)現(xiàn)圖片自動(dòng)切換
這篇文章主要為大家詳細(xì)介紹了android使用ViewPager實(shí)現(xiàn)圖片自動(dòng)切換,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-02-02
Android編程實(shí)現(xiàn)ListView內(nèi)容無限循環(huán)顯示的方法
這篇文章主要介紹了Android編程實(shí)現(xiàn)ListView內(nèi)容無限循環(huán)顯示的方法,通過繼承Adapter類實(shí)現(xiàn)ListView中的數(shù)據(jù)無限循環(huán)顯示功能,需要的朋友可以參考下2017-06-06
Android 獲取應(yīng)用簽名的實(shí)現(xiàn)
本文主要講下在android中如何獲取應(yīng)用簽名,也方便平時(shí)用來區(qū)分一個(gè)應(yīng)用是不是原包應(yīng)用,具有一定的參考價(jià)值,感興趣的可以了解一下2016-02-02
Android?Activity生命周期調(diào)用的理解
大家好。本篇文章主要講的是Android?Activity生命周期調(diào)用的理解,感興趣的同學(xué)趕快來看一看吧,對(duì)你有幫助的話記得收藏一下,方便下次瀏覽2021-12-12
Android實(shí)時(shí)文件夾創(chuàng)建方法
這篇文章主要介紹了Android實(shí)時(shí)文件夾創(chuàng)建方法,涉及基于Activity實(shí)現(xiàn)文件實(shí)時(shí)查詢的相關(guān)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-09-09
android studio 安裝完成ButterKnife插件卻無法使用(解決方案)
這篇文章主要介紹了android studio 安裝完成ButterKnife插件卻無法使用問題,本文通過圖文并茂的形式給大家分享解決方法,對(duì)大家有非常好的幫助,需要的朋友可以參考下2020-03-03
使用CMake構(gòu)建OpenCV項(xiàng)目過程解析
這篇文章主要介紹了使用CMake構(gòu)建OpenCV項(xiàng)目過程解析,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-11-11
Android判斷設(shè)備網(wǎng)絡(luò)連接狀態(tài)及判斷連接方式的方法
這篇文章主要介紹了Android判斷設(shè)備網(wǎng)絡(luò)連接狀態(tài)及判斷連接方式的方法,涉及Android針對(duì)網(wǎng)絡(luò)連接的相關(guān)判定技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-10-10
Android基于TextView實(shí)現(xiàn)的跑馬燈效果實(shí)例
這篇文章主要介紹了Android基于TextView實(shí)現(xiàn)的跑馬燈效果,以完整實(shí)例形式分析了Android使用TextView通過屬性設(shè)置及功能代碼實(shí)現(xiàn)跑馬燈效果的相關(guān)技巧,需要的朋友可以參考下2016-02-02

