Android N App Shotcuts 学习

()一:

Android 7.1 增加了很多新特性,比如多窗口,App Shotcuts….
其中,App Shoutcuts就类似于iphone手机上3D Touch功能,在应用桌面长按某个app图标,就会对应的弹出选项,使我们操作更加方便和快速;

先来张图看看安卓的App Shotcuts长什么样:

这里写图片描述

图片引用官网,长按短信图标弹出的选项。

二:
自己实践为自己的app添加App Shortcuts功能,前提条件必须是android 7.1,api 25
App ShortCuts 实现有两种:
1,xml 布局
2,代码实现

先来讲xml布局实现:
1.首先在自己的清单文件(activity 主入口)下,加入一个 标签

<meta-data android:name="android.app.shortcuts" android:resource="@xml/shortcuts" />

2.然后再res目录下新建一个xml文件夹,随后创建一个xml文件,取名就是上面的引用

<?xml version="1.0" encoding="utf-8"?>
<shortcuts xmlns:android="http://schemas.android.com/apk/res/android" >
<shortcut
    android:shortcutId="add_website" //id
    android:icon="@drawable/ic_launcher"  //图标
    android:shortcutShortLabel="@string/shortcuts"
    android:shortcutLongLabel="@string/shortcutss" //名字
    >
    <intent //意图 也就是点击以后的行为
        android:action="com.example.android.appshortcuts.ADD_WEBSITE"
        android:targetPackage="com.example.administrator.myapplication" //包名
        android:targetClass="com.example.administrator.myapplication.MainActivity" //类名
        />
</shortcut>
</shortcuts>

3.到这一步,基本上就已经完成了App Shortcuts的功能,然后打开7.1模拟器run

这里写图片描述

4.这样就可以了,如果想添加多个选项,怎么办?依然在xml文件中修改(注意,shortcutiId不要一样)

<?xml version="1.0" encoding="utf-8"?>
<shortcuts xmlns:android="http://schemas.android.com/apk/res/android" >
<shortcut
    android:shortcutId="add_website"
    android:icon="@drawable/ic_launcher"
    android:shortcutShortLabel="@string/shortcuts"
    android:shortcutLongLabel="@string/shortcutss"
    >
    <intent
        android:action="com.example.android.appshortcuts.ADD_WEBSITE"
        android:targetPackage="com.example.administrator.myapplication"
           android:targetClass="com.example.administrator.myapplication.MainActivity"
        />
    </shortcut>
<shortcut
android:shortcutId="add_website1"
android:icon="@drawable/ic_launcher"
android:shortcutShortLabel="@string/shortcuts"
android:shortcutLongLabel="@string/shortcutss"
>
<intent
    android:action="com.example.android.appshortcuts.ADD_WEBSITE"
    android:targetPackage="com.example.administrator.myapplication"
    android:targetClass="com.example.administrator.myapplication.MainActivity"
    />
</shortcut>
<shortcut
    android:shortcutId="add_website2"
    android:icon="@drawable/ic_launcher"
    android:shortcutShortLabel="@string/shortcuts"
    android:shortcutLongLabel="@string/shortcutss"
    >
    <intent
        android:action="com.example.android.appshortcuts.ADD_WEBSITE"
        android:targetPackage="com.example.administrator.myapplication"
        android:targetClass="com.example.administrator.myapplication.MainActivity"
        />
</shortcut>
</shortcuts>

如下图:

这里写图片描述

三:

如果代码实现?其实也很简单,如下

ShortcutManager shortcutManager = getSystemService(ShortcutManager.class);

ShortcutInfo shortcut = new ShortcutInfo.Builder(this, "id1")
.setShortLabel("Web site")
.setLongLabel("Open the web site")
.setIcon(Icon.createWithResource(context, R.drawable.icon_website))
.setIntent(new Intent(Intent.ACTION_VIEW,
           Uri.parse("https://www.mysite.example.com/")))
.build();

shortcutManager.setDynamicShortcuts(Arrays.asList(shortcut));

简书博客:http://www.jianshu.com/users/6edc22b4da83/latest_articles

发布了35 篇原创文章 · 获赞 73 · 访问量 15万+

猜你喜欢

转载自blog.csdn.net/qq_28268507/article/details/53556751
今日推荐