android n mr1,Android N_MR1(7.1) App Shortcuts简单使用

7.1中支持的App shortcuts类似与iPhone上面的3D Touch,不过在Android中通过长按弹出。

5c6a879039a10a4d8d7e0e4ae7dd6b66.png

该特性同时需要Launcher支持,比如Pixel Launcher、Nova Launcher等等

You can publish two different types of shortcuts for your app:

Static shortcuts are defined in a resource file that is packaged into an APK. Therefore, you must wait until you update your entire app to change the details of these static shortcuts.

Dynamic shortcuts are published at runtime using the ShortcutManager API. During runtime, your app can publish, update, and remove its dynamic shortcuts.

You can publish up to five shortcuts (static shortcuts and dynamic shortcuts combined) at a time for your app. Some launcher apps, however, don’t show every shortcut you’ve created for your app.

1. 实现静态shortcut

实现静态的shortcut只需要两步:

1. 在res/xml/中创建资源文件

这里创建了三个shortcut,如最开始的图所示

属性

解释

android:shortcutId

shortcut的ID

android:enabled

shortcut是否允许使用,可通过代码进行更新

android:icon

shortcut的图标

android:shortcutShortLabel

短名称,在长名称显示不下时显示这个

android:shortcutLongLabel

长名称,字符数控制在10个之内(关于10这个数字,不知道在哪看到的,忘了)

intent中android:action

android.intent.action.VIEW这个属性应该是固定的

android:targetPackage

目标Activity所在的包的包名

android:targetClass

目标Activity的全名

android:data

可以用来携带数据

categories中android:name

固定使用android.shortcut.conversation

2. 在AndroidManifest.xml中配置

在你的AndroidManifest.xml中,找到一个同时有android.intent.action.MAIN和android.intent.category.LAUNCHER属性的Activity,这个Activity一般是第一个Activity。

在配置文件中添加meta-data属性

meta-data中android:name=”android.app.shortcuts”也是固定的,只需要将android:resource换成上面的xml路径即可。

通过上述两个步奏,已经可以显示出App Shortcuts了。

2. 如何区分哪个shortcut调用的Activity

我们在1中谈到过,shortcut中有一个intent,而intent中有一个data属性。

通过这个属性我们可以在多个shortcut指向同一个Activity时区分是哪个shortcut来打开的Activity。

在1中,三个shortcut中data分别为:

android:data="content://yorek.com.solitaire/level_hard"

android:data="content://yorek.com.solitaire/level_normal"

android:data="content://yorek.com.solitaire/level_easy"

所调用的Activity均是android:targetClass="yorek.com.solitaire.ui.SplashActivity"

在SplashActivity中,可以通过UriMatcher来区分,具体如下:

public class SplashActivity extends AppCompatActivity {

private static final String AUTHORITY = "yorek.com.solitaire";

private static final int LEVEL_HARD_CONTENT_CODE = GameController.LEVEL_HARD;

private static final int LEVEL_NORMAL_CONTENT_CODE = GameController.LEVEL_NORMAL;

private static final int LEVEL_EASY_CONTENT_CODE = GameController.LEVEL_EASY;

private static final UriMatcher sUriMatcher = new UriMatcher(UriMatcher.NO_MATCH);

static {

sUriMatcher.addURI(AUTHORITY, "level_hard", LEVEL_HARD_CONTENT_CODE);

sUriMatcher.addURI(AUTHORITY, "level_normal", LEVEL_NORMAL_CONTENT_CODE);

sUriMatcher.addURI(AUTHORITY, "level_easy", LEVEL_EASY_CONTENT_CODE);

}

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

if (getIntent().getData() != null) {

int level = sUriMatcher.match(getIntent().getData());

beginGameWithLevel(level);

}

......

}

......

}

该办法的原理就是在addURI方法中将AUTHORITY与path(level_hard、level_normal等)组合,然后等match时与传入的URI比较。如果相等则返回addURI中第三个传入的CODE。 这样通过在shortcuts.xml中配置不同的data,然后在目标Activity中判断Uri来区分不用的shortcut

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值