浅谈Android添加快捷方式ShortCut

这篇文章主要介绍了浅谈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 list = new ArrayList(); 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 list = new ArrayList<>(); for (int i = 0; i 

在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快捷方式的资料请关注html中文网其它相关文章!

以上就是浅谈Android添加快捷方式ShortCut的详细内容,更多请关注0133技术站其它相关文章!

赞(0) 打赏
未经允许不得转载:0133技术站首页 » 移动