android7.1新特性 App Shortcuts 图标快捷启动
2016-11-04 22:40
369 查看
前几天看了下谷歌的发布会,在介绍7.1的适合展示了图标快捷启动的功能,类似于苹果的3d touch ,好酷炫,于是决定自己动手研究一波在动手开发之前呢先将AS升级到最新版,SDK更新到API25,因为只有API25才能使用这个功能。首先介绍一下核心的几个类,1 ShortcutManager 该类是图标信息管理者,主要负责添加,更新或去除图标上的某个条目2 Shortcutinfo 该类是图标条目信息类,整个类代表一个条目,该类可以设置条目信息,包括图标,标题,主体内容,隐藏内容3 Shortcutinfo.Binder 该类用来创建一个Shortcut条目,返回Shortcutinfo对象我们再来看看几个XML属性:android:shortcutId 条目信息的IDandroid:enabled 条目信息是否显示,默认是显示的android:icon 条目信息的图标android:shortcutShortLabel 条目信息的标题(有正文的时候不会显示)android:shortcutLongLabel 条目信息的正文android:shortcutDisabledMessage 当android:enabled 为false的时候显示的内容intent 用户点击后跳转的intent对象,强制要求有android:action 属性OK,了解完上面的属性以后,我们开始动手实现下吧,1,在AndroidMainfest.xml文件下,在主程序入口的<activity>标签线新加入一个<meta-data>标签,用来设置图标条目的信息,其内容如下:
<meta-data android:name="android.app.shortcuts" android:resource="@xml/shortcuts" />
2 在res目录下新建一个xml文件夹,在文件夹下新建一个shortcuts的xml文件,该文件用来静态设置条目的信息
内容如下:
<?xml version="1.0" encoding="utf-8"?> <shortcuts xmlns:android="http://schemas.android.com/apk/res/android"> <shortcut android:shortcutId="compose" android:enabled="true" android:icon="@mipmap/ic_launcher" android:shortcutShortLabel="@string/compose_shortcut_short_label1" android:shortcutLongLabel="@string/compose_shortcut_long_label1" android:shortcutDisabledMessage="@string/compose_disabled_message1"> <intent android:action="android.intent.action.VIEW" android:targetPackage="com.example.wenxi.myapplication" android:targetClass="com.example.wenxi.myapplication.MainActivity" /> <!-- If your shortcut is associated with multiple intents, include them here. The last intent in the list is what the user sees when they launch this shortcut. --> <categories android:name="android.shortcut.conversation" /> </shortcut> <!-- Specify more shortcuts here. --> </shortcuts>
OK,简单两步,就完成了最简单的demo,当然这还不够,前面我说过,这是静态的,要是需求里面要动态更新条目信息呢,这样我们就不能够用xml去做了,我们需要用代码实现上面的功能
首先,初始化ShortcutManager
ShortcutManager shortcutManager = getSystemService(ShortcutManager.class);//返回shortcutManager 对象其次,我们需要通过Shortcutinfo.Binder去创建 Shortcutinfo 对象ShortcutInfo shortcut = new ShortcutInfo.Builder(this, "id1")该方法中传入的参数是当前Activity的上下文和条目的ID拿到 shortcut,对象以后,我们就可以设置条目信息了.setShortLabel("Web site")//设置标题 .setLongLabel("Open"+String.valueOf(index)+"")//设置 .setIcon(Icon.createWithResource(context, R.mipmap.ic_launcher)) //.setActivity(new ComponentName("com.example.wenxi.myapplication","com.example.wenxi.myapplication.MainActivity")) .setIntent(new Intent(Intent.ACTION_VIEW, Uri.parse("https://www.baidu.com/")))//设置点击跳转的intent对象.build();//创建这里需要注意的是,动态设置必须要设置intent对象,否则会报空指针异常,还有就是不知道是不是因为bug,setActivity并不起作用定义一个list,将shortcut添加到list里面private List<ShortcutInfo> list=new ArrayList();list.add(shortcut);最后,通过ShortcutManager添加信息条目或者删除条目:主要有下面三个方法:[code]setDynamicShortcuts(List)//将添加一个list[/code][code]updateShortcuts(List)// 更新某一个List[/code]removeDynamicShortcuts(List) //删除某个list[/code][code]removeAllDynamicShortcuts().//删除全部信息[/code]注:可以有多个list信息集合本文采用shortcutManager.setDynamicShortcuts(list);运行结果如图:
源码下载地址:http://download.csdn.net/detail/qq_25817651/9673658
相关文章推荐
- Android7.1新特性:快捷方式Shortcuts详解
- Android7.1图标快捷方式--AppShortcuts
- App Shortcuts 快捷方式 Android7.1 的"3D Touch"
- Android 快捷启动图标
- Android 7.1 新特性:快捷方式 Shortcuts 详解
- Android APP自动生成桌面快捷图标 启动方式有差异
- Android 快捷启动图标
- 探索Android 7.1 app快捷方式(App Shortcuts)
- android 隐藏应用图标,用快捷方式做启动入口,实现伪动态改变图标
- 关于android创建快捷方式会启动两个应用的问题(一)
- Android安装后出现两个启动图标
- Android应用启动后自动创建桌面快捷方式
- Android桌面快捷方式图标生成与删除,使用Intent与launcher交互
- Android应用启动后自动创建桌面快捷方式
- Android 4.0 Launcher2源码分析——桌面快捷图标的拖拽
- Android应用启动后自动创建桌面快捷方式
- Android应用启动后自动创建桌面快捷方式
- 关于android创建快捷方式会启动两个应用的问题
- Android共享Activity,并设置无主入口启动图标的方式。
- 关于android创建快捷方式会启动两个应用的问题(二)