Android 7.1 Shortcuts功能

来源:互联网 发布:万网域名绑定非80端口 编辑:程序博客网 时间:2024/05/22 00:20

Android 7.1发布已久,是时候来了解一下Android 7.1新特性了,接下来将讲述Android 7.1的Shortcuts(快捷方式)。

创建快捷方式的方法有两种:静态方式与动态方式

1、静态方式创建

1.1 创建一个res/xml/shortcuts.xml布局

<?xml version="1.0" encoding="utf-8"?><shortcuts    xmlns:android="http://schemas.android.com/apk/res/android">    <shortcut        android:shortcutId="short_1"        android:enabled="true"        android:shortcutShortLabel="@string/shortcut_short_1"        android:shortcutLongLabel="@string/shortcut_long_1"        android:shortcutDisabledMessage="@string/shortcut_disabled_1">        <intent            android:action="android.intent.action.View"            android:targetPackage="com.app.helloworld"            android:targetClass="com.app.helloworld.SecActivity"/>    </shortcut>    <shortcut        android:shortcutId="short_2"        android:enabled="true"        android:shortcutShortLabel="@string/shortcut_short_2"        android:shortcutLongLabel="@string/shortcut_long_2"        android:shortcutDisabledMessage="@string/shortcut_disabled_2">        <intent            android:action="android.intent.action.Main"            android:targetPackage="com.app.helloworld"            android:targetClass="com.app.helloworld.MainActivity"/>        <intent            android:action="android.intent.action.View"            android:targetPackage="com.app.helloworld"            android:targetClass="com.app.helloworld.ThirdActivity"/>            />    </shortcut></shortcuts>

1.2 在AndroidManifest.xml文件中申明shortcut位置,特别注意下面代码申明需放在第一个Activity的申明中,否则无效:

  <activity android:name=".MainActivity">      <intent-filter>        <action android:name="android.intent.action.MAIN" />        <category android:name="android.intent.category.LAUNCHER" />      </intent-filter>      <meta-data        android:name="android.app.shortcuts"        android:resource="@xml/shortcuts" />    </activity>

如此,就大功告成了!!!

2、动态态方式创建

        // Shortcut管理类        ShortcutManager manager = getSystemService(ShortcutManager.class);        Intent intent1 = new Intent(this,SecActivity.class);        intent1.setAction("android.intent.action.View");        /*         * 默认显示LongLabel         * LongLabel文本过长时,显示ShortLabel         * Shortcut不能用时,显示DisabledMessage         * Rank下标,用于排序         */        ShortcutInfo shortcut_1 = new ShortcutInfo.Builder(this,"shortcut_1")                .setShortLabel("Short 1")                .setLongLabel("Short Long 1")                .setDisabledMessage("Short Disabled Message")                .setIntent(intent1)                .setRank(0)                .build();        Intent intent2 = new Intent(this,ThirdActivity.class);        intent2.setAction("android.intent.action.View");        ShortcutInfo shortcut_2 = new ShortcutInfo.Builder(this,"shortcut_2")                .setShortLabel("Short 2")                .setLongLabel("Short Long 2")                .setDisabledMessage("Short Disabled Message 2")                .setIntent(intent2)                .setRank(1)                .build();        manager.addDynamicShortcuts(Arrays.asList(shortcut_1,shortcut_2));

image

源码地址:http://download.csdn.net/download/y472360651/9819418

0 0
原创粉丝点击