7.1的时候出现了一个长按图标出现的功能列表的效果,类似于2015年ios的3dTouch功能.奈何国产,大家懂得,到目前用了一加五才可以感受这个功能的好处.
其中App Shortcuts是指在桌面长按app图标而出现的快捷方式, 可以为你的app的关键功能添加更快速的入口而不用先打开app,点击快捷方式可以访问应用功能, 并且这种快捷方式也可以被拖拽到桌面单独放置, 变成单独的桌面快捷方式.
每一个应用目前最多可以有5个shortcuts(静态 + 动态)
应用添加App Shortcuts是Android 7.1(API 25)的API, 所以只能在Android 7.1的设备上显示, 同时需要launcher支持, 比如Pixel launcher(Pixel设备的默认launcher), Now launcher(Nexus设备上的launcher)现在就支持, 其他launcher也可以提供支持.
<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>
<shortcuts xmlns:android="http://schemas.android.com/apk/res/android">
<shortcut
android:icon="@drawable/add"
android:shortcutId="add_website"
android:shortcutLongLabel="@string/add_new_website"
android:shortcutShortLabel="@string/add_new_website_short">
<intent
android:action="cn.yky.test.ADD_WEBSITE"
android:targetClass="cn.yky.test.MainActivity"
android:targetPackage="cn.yky.test" />
shortcut>
shortcuts>
注意,shortcutLongLabel和shortcutShortLabel,不可以直接引用文字,不然会报错.!!!谁加谁知道.经过以上的步骤之后,就可以看到最开始的效果图了!.
动态的shortcuts可以在用户使用app的过程中构建, 更新, 或者删除.
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N_MR1) {
mShortcutManager = getSystemService(ShortcutManager.class);
getNewShortcutInfo();
}
/**
* 动态添加三个
*/
@RequiresApi(api = Build.VERSION_CODES.N_MR1)
private void getNewShortcutInfo() {
ShortcutInfo shortcut = new ShortcutInfo.Builder(this, "id1")
.setShortLabel("Web site")
.setLongLabel("第一个")
.setIcon(Icon.createWithResource(this, R.drawable.link))
.setIntent(new Intent(Intent.ACTION_VIEW,
Uri.parse("https://www.baidu.com/")))
.build();
ShortcutInfo shortcut2 = new ShortcutInfo.Builder(this, "id2")
.setShortLabel("Web site")
.setLongLabel("第二个")
.setIcon(Icon.createWithResource(this, R.drawable.link))
.setIntent(new Intent(Intent.ACTION_VIEW,
Uri.parse("https://www.csdn.com/")))
.build();
ShortcutInfo shortcut3 = new ShortcutInfo.Builder(this, "id3")
.setShortLabel("Web site")
.setLongLabel("第三个")
.setIcon(Icon.createWithResource(this, R.drawable.link))
.setIntent(new Intent(Intent.ACTION_VIEW,
Uri.parse("https://www.github.com/")))
.build();
mShortcutManager.setDynamicShortcuts(Arrays.asList(shortcut, shortcut2, shortcut3));
}
依次类推,就可以动态增删改我们的快捷方式了.虽然你想用,但是国产手机估计还得一年以内的时间能普及到吧.
本人技术有限,还有很多不完美的地方,欢迎指出.(写作不易,谢谢您的star支持)