7.1中支持的App shortcuts类似与iPhone上面的3D Touch,不过在Android中通过长按弹出。
该特性同时需要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