色综合图-色综合图片-色综合图片二区150p-色综合图区-玖玖国产精品视频-玖玖香蕉视频

您的位置:首頁技術文章
文章詳情頁

淺談Android添加快捷方式ShortCut

瀏覽:34日期:2022-09-19 17:01:48

眾所周知application有4種啟動方式:

點擊app啟動 快捷方式 通知跳轉 輸入命令(adb命令等)

今天給大家簡單介紹一下快捷方式啟動的用法~

快捷方式介紹

谷歌官方在Android 7.1(API 25)新增了桌面長按彈出菜單,并且在8.0(API 26)以后可以固定快捷方式至桌面上。圍繞桌面快捷方式的需求也比較多,例如微信將聯系人、小程序都可以添加至桌面;簡書將“寫文章”添加至桌面;高德將“坐標信息”添加到桌面。

快捷方式情景再現

將某個應用添加到桌面

淺談Android添加快捷方式ShortCut

長按應用打開某一個功能

淺談Android添加快捷方式ShortCut

快捷方式使用將某個應用添加到桌面

先看代碼,后面我會將這些代碼寫成工具類供大家使用:

/** * @param context 當前content * @param targetClass 快捷圖標打開的界面 * @param backClass 打開后按返回鍵返回的界面 * @param shortCutId shortCut 唯一id * @param shortCutIcon 桌面上顯示的圖標 */ 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, '設備不支持在桌面創建快捷圖標!', Toast.LENGTH_LONG).show();} }

測試:

shortUtil.AddShortCut( this, MainActivity::class.java, MainActivity::class.java, 2, R.drawable.ic_launcher_background)

效果圖(1.1):

淺談Android添加快捷方式ShortCut

修改快捷方式:

/** * @param context 上下文 * @param cls 要跳轉的頁面 * @param shortCutId shortCut 唯一id * @param shortCutIcon 桌面上顯示的圖標 * @param shortCutLabel 桌面圖標下方顯示的文字 */ 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));} }

測試:

shortUtil.updItem( this, ModifyActivity::class.java, 2, R.drawable.ic_launcher_background, '修改快捷方式成功')

效果圖(1.2):

淺談Android添加快捷方式ShortCut

禁用快捷方式:

/** * 禁止使用快捷方式 * * @param index 禁止使用下標 */ 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);} }

測試:

shortUtil.remove(2)

效果圖(1.3):

淺談Android添加快捷方式ShortCut

長按應用打開某一個功能:

這里以Fragment舉例:

先來看看最終的效果:

淺談Android添加快捷方式ShortCut

主要代碼:

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 = {'首頁', '我的', '詳情', '設置'};/** * 設置默認快捷方式 * * @param context 上下文 * @param ast 跳轉頁面 */ 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, '該設備不支持快捷方式', Toast.LENGTH_SHORT).show();} }

在Application中注冊一下:

淺談Android添加快捷方式ShortCut 

記得在清單文件聲明哦

// 保存按鈕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寫的,因為剛創建項目的時候只能創建kotlin,我就懶的改了

Fragment代碼很簡單,通過Arguments獲取到值賦值到TextView上即可這里就不貼代碼了

以上就是淺談Android添加快捷方式ShortCut的詳細內容,更多關于Android快捷方式的資料請關注好吧啦網其它相關文章!

標簽: Android
相關文章:
主站蜘蛛池模板: 久久88香港三级 | 香蕉伊人网 | 久久高清影院 | 男人又粗又硬桶女人免费 | 波多野结衣在线看片 | a毛片在线看片免费 | 中文字幕一区二区在线观看 | 美女黄色在线观看 | 午夜神马视频 | 91香蕉国产线观看免 | 在线观看国产一区二区三区 | aaaaaa级特色特黄的毛片 | 日韩在线二区全免费 | 国产精品无码久久久久 | 欧美一区二区三区国产精品 | 国产欧美日韩三级 | 超级碰碰碰视频视频在线视频 | 成人资源在线 | 欧美xx一片| 国产欧美成人 | 成年女人午夜免费视频 | 欧美日韩看看2015永久免费 | 久久精品在线 | 天天躁天天碰天天看 | 日本欧美一区二区三区不卡视频 | 亚洲第一网站在线观看 | 91精品在线国产 | 欧美巨大另类极品videohd | 久久久亚洲国产精品主播 | 国产精品一一在线观看 | 波多野结衣中文在线播放 | 视频一区二区三区自拍 | 欧美日韩一区二区在线观看 | 久久香蕉国产视频 | 欧美精品v欧洲精品 | 免费观看成年人视频 | www.日本在线视频 | 国产l精品国产亚洲区久久 国产tv在线 | 中文字幕亚洲精品第一区 | 亚洲国产福利精品一区二区 | 国产第一页在线观看 |