纪检监察机关网站建设方案,北京海岸设计公司网站,会展设计是什么专业,开封市做网站的公司Shortcuts介绍
Android7.1#xff08;API Level 25#xff09;及以上系统可以自定义Shortcuts#xff0c;通过在桌面上长按App Icon弹出Shortcut列表#xff0c;点击某个shortcut可使用户快捷得打开App里常用的或推荐的任务。国内各个厂商基本上在安卓8.0上集成了该功能。…Shortcuts介绍
Android7.1API Level 25及以上系统可以自定义Shortcuts通过在桌面上长按App Icon弹出Shortcut列表点击某个shortcut可使用户快捷得打开App里常用的或推荐的任务。国内各个厂商基本上在安卓8.0上集成了该功能。见下图 1.1 Shortcuts的简单作用
每个Shortcut可以关联一个或多个intents每个intent启动一个指定的action 官方给出了几个可以作为shortcut的例子比如
在地图类app中指导用户到特定的位置 在社交类app中发送消息给一个朋友 在媒体类app中播放视频的下一片段 在游戏类app中下载最后保存的要点 在实际开发中我们具体想让哪些操作作为快捷方式可自行定义。
1.2 静态实现
1. 在app的AndroidManifest.xml文件中找到MainActivity即设置了action为
action android:name”android.intent.action.MAIN” /且category设置为
category android:name”android.intent.category.LAUNCHER” /的activity
为其添加meta-data…./meta-data指向定义Shortcuts的资源文件。
代码如下
activity android:name.MainActivityintent-filteraction android:nameandroid.intent.action.MAIN /category android:nameandroid.intent.category.LAUNCHER //intent-filtermeta-data android:nameandroid.app.shortcuts android:resourcexml/shortcuts/
/activity 2. 创建定义AppShortcuts的资源文件res/xml/shortcuts.xml文件
?xml version1.0 encodingutf-8?
shortcuts xmlns:androidhttp://schemas.android.com/tools!-- 第一个静态shortcut --shortcutandroid:shortcutIdstatic_oneandroid:enabledtrueandroid:iconmipmap/icon_diamondandroid:shortcutLongLabelstring/static_one_long_labelandroid:shortcutDisabledMessagestring/static_disabled_messageandroid:shortcutShortLabelstring/static_one_short_label!--一个shortcut当有多个intents与之相关联时在用户启动该shortcut时最先呈现给用户的是intent.../intent集合中最后一个intent操作事件。即这里创建了一个intent的回退栈最后一个才是被快捷方式打开的那个。--intentandroid:actionandroid.intent.action.MAINandroid:targetPackagecom.example.butterknifetestandroid:targetClasscom.example.butterknifetest.MainActivity/intentandroid:actionandroid.intent.action.MAINandroid:targetPackagecom.example.butterknifetestandroid:targetClasscom.example.butterknifetest.AndroidTestActivity/!--最后这个是要打开的intent前边的intent表示点击返回时依次回退的intent。如果不设置回退intent则按返回键会返回桌面。--intentandroid:actionandroid.intent.action.MAINandroid:targetPackagecom.example.butterknifetestandroid:targetClasscom.example.butterknifetest.TestActivit//shortcut!-- 第二个静态shortcut --shortcutandroid:shortcutIdstatic_twoandroid:enabledtrueandroid:iconmipmap/icon_starandroid:shortcutLongLabelstring/static_two_long_labelandroid:shortcutDisabledMessagestring/static_disabled_messageandroid:shortcutShortLabelstring/static_two_short_labelintentandroid:actionandroid.intent.action.MAINandroid:targetPackagecom.example.butterknifetestandroid:targetClasscom.example.butterknifetest.MainActivity/!--最后这个是要打开的intent前边的intent表示点击返回时依次回退的intent。如果不设置回退intent则按返回键会返回桌面。--intentandroid:actionandroid.intent.action.MAINandroid:targetPackagecom.example.butterknifetestandroid:targetClasscom.example.butterknifetest.TestActivity//shortcut/shortcuts
shortcut/ 标签内容只少要设置一个intent即图标指向的intent。
如果有多个最后一个是最终指向的intent之前的则认为是打开后点击返回键时依次回退的intent。 3. 属性讲解
以shortcuts元素为根可以包含多个shortcut元素每个shortcut元素标示一个shortcut。其中属性分别标示
shortcutIdshortcut唯一标识符相同的shortcutId会被覆盖。必设属性 enableshortcut是否启用true启用false是禁用若设置为false不如删除掉该快捷方式可选属性 icon显示在快捷方式左边的图标。可选属性 shortcutLongLabel当launcher的空间足够时将会显示shortcut的长文本描述不宜过长如果过长或未设置时会显示shortcutShortLabel 可选属性 shortcutShortLabel : shortcut的简要说明这项是必须的。必设属性 intent : 这里定义快捷方式被点击之后将会打开的intent 必设属性 shortcutDisabledMessage : 当你禁用了shortcut之后它将不会显示在用户长按应用图标后打开的快捷方式里但是用户可以把一个快捷方式拖拽到launcher的某个页面成为Pinned Shortcut被禁用之后这个快捷方式就会显示为灰色点击这个Pinned Shortcut则会显示一个内容为shortcutDisabledMessage的Toast。(可选属性)
1.3 动态实现 代码创建Dynamic shortcut需要使用API ShortcutManager和ShortcutInfo.Builder通过ShortcutInfo.Builder新建ShortcutInfo再通过ShortcutManager添加即可。动态shortcut可以在运行时动态改变内容无需重写部署App。下面直接看代码而不在叙述创建的具体步骤
public class MainActivity extends AppCompatActivity {Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);//要确保API Level 大于等于 25才可以创建动态shortcut否则会报异常。if (Build.VERSION.SDK_INT Build.VERSION_CODES.N_MR1) {initDynamicShortcuts();}}/*** 为App创建动态Shortcuts*/private void initDynamicShortcuts() {//①、创建动态快捷方式的第一步创建ShortcutManagerShortcutManager scManager getSystemService(ShortcutManager.class);//②、构建动态快捷方式的详细信息ShortcutInfo scInfoOne new ShortcutInfo.Builder(this, dynamic_one).setShortLabel(Dynamic Web site).setLongLabel(to open Dynamic Web Site).setIcon(Icon.createWithResource(this, R.mipmap.tool_music_icon)).setIntent(new Intent(Intent.ACTION_VIEW, Uri.parse(https://www.baidu.com))).build();ShortcutInfo scInfoTwo new ShortcutInfo.Builder(this, dynamic_two).setShortLabel(Dynamic Activity).setLongLabel(to open dynamic one activity).setIcon(Icon.createWithResource(this, R.mipmap.tool_luck_icon)).setIntents(new Intent[]{new Intent(Intent.ACTION_MAIN, Uri.EMPTY, this, MainActivity.class).setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK),//加该FLAG的目的是让MainActivity作为根activity清空已有的任务new Intent(DynamicASOneActivity.ACTION)}).build();//③、为ShortcutManager设置动态快捷方式集合scManager.setDynamicShortcuts(Arrays.asList(scInfoOne, scInfoTwo));//如果想为两个动态快捷方式进行排序可执行下面的代码ShortcutInfo dynamicWebShortcut new ShortcutInfo.Builder(this, dynamic_one).setRank(0).build();ShortcutInfo dynamicActivityShortcut new ShortcutInfo.Builder(this, dynamic_two).setRank(1).build();//④、更新快捷方式集合scManager.updateShortcuts(Arrays.asList(dynamicWebShortcut, dynamicActivityShortcut));}
} ShortcutManager API可以帮助我们实现新建、更新、移除快捷方式的操作
新建方法setDynamicShortcuts() 可以添加或替换所有的shortcut方法addDynamicShortcuts() 来添加新的shortcut到列表中超过最大个数会报异常 更新方法updateShortcuts(List shortcutInfoList) 更新已有的动态快捷方式 删除方法removeDynamicShortcuts(List shortcutIds) 根据动态快捷方式的ID删除已有的动态快捷方式方法removeAllDynamicShortcuts() 删除掉app中所有的动态快捷方式 ListShortcutInfo getDynamicShortcuts() 得到所有的动态shortcuts --------------------- 作者HL是限量版 来源CSDN 原文https://blog.csdn.net/m0_37218227/article/details/84071043 版权声明本文为博主原创文章转载请附上博文链接