浅谈Android添加快捷方式ShortCut
众所周知application有4种启动方式:
点击app启动 快捷方式 通知跳转 输入命令(adb命令等)今天给大家简单介绍一下快捷方式启动的用法~
快捷方式介绍谷歌官方在Android 7.1(API 25)新增了桌面长按弹出菜单,并且在8.0(API 26)以后可以固定快捷方式至桌面上。围绕桌面快捷方式的需求也比较多,例如微信将联系人、小程序都可以添加至桌面;简书将“写文章”添加至桌面;高德将“坐标信息”添加到桌面。
快捷方式情景再现将某个应用添加到桌面
长按应用打开某一个功能
先看代码,后面我会将这些代码写成工具类供大家使用:
/** * @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):
/** * @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):
/** * 禁止使用快捷方式 * * @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):
这里以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 = {'首页', '我的', '详情', '设置'};/** * 设置默认快捷方式 * * @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中注册一下:
记得在清单文件声明哦
// 保存按钮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快捷方式的资料请关注好吧啦网其它相关文章!
相关文章: