拥抱Android O,Android固定快捷方式App Shortcuts
由来
在新发布的Android 8.0功能和API中,Android 8.0 引入了对在应用启动器图标上显示通知标志的支持。通知标志可反映某个应用是否存在与其关联、并且用户尚未予以清除也未对其采取行动的通知。通知标志也称为通知点。简而言之呢,就是在Android 8.0+加入了类似于IOS的3DTouch的功能。下面就是他的效果。

这个小工能的添加可谓是非常方便的,在我们日常的应用场景中,有时候我们可能想用QQ发一条空间动态。
A[QQ] -->|常规方式| B[动态]B --> C[右上角+号按钮]C --> D[发说说]A -->|App Shortcuts| E[快捷菜单]E --> D
App Shortcuts的增加,无疑简化了用户的操作,提升了用户体验度。
下面我们就用一个Demo来介绍下如何定制我们的App Shortcuts:
首先新建项目,找到AndroidManifest.xml在主Activity添加:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"package="com.example.shortcutsdemo"><application
android:allowBackup="true"android:icon="@mipmap/ic_launcher"android:label="@string/app_name"android:roundIcon="@mipmap/ic_launcher_round"android:supportsRtl="true"android:theme="@style/AppTheme"><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/shortcuts1" />activity>application>manifest>
然后在res/xml/下新建shortcuts1.xml文件:
<shortcuts xmlns:android="http://schemas.android.com/apk/res/android"><shortcut
android:enabled="true"android:icon="@mipmap/ic_launcher_round"android:shortcutDisabledMessage="@string/shortcut1"android:shortcutId="compose1"android:shortcutLongLabel="@string/shortcut1"android:shortcutShortLabel="@string/shortcut1"><intent
android:action="android.intent.action.MAIN"android:data="快捷方式1"android:targetClass="com.example.shortcutsdemo.MainActivity"android:targetPackage="com.example.shortcutsdemo" /><categories android:name="android.shortcut.conversation" />shortcut><shortcut
android:enabled="true"android:icon="@mipmap/ic_launcher_round"android:shortcutDisabledMessage="@string/shortcut2"android:shortcutId="compose2"android:shortcutLongLabel="@string/shortcut2"android:shortcutShortLabel="@string/shortcut2"><intent
android:action="android.intent.action.MAIN"android:data="快捷方式2"android:targetClass="com.example.shortcutsdemo.MainActivity"android:targetPackage="com.example.shortcutsdemo" /><categories android:name="android.shortcut.conversation" />shortcut><shortcut
android:enabled="true"android:icon="@mipmap/ic_launcher_round"android:shortcutDisabledMessage="@string/shortcut3"android:shortcutId="compose3"android:shortcutLongLabel="@string/shortcut3"android:shortcutShortLabel="@string/shortcut3"><intent
android:action="android.intent.action.MAIN"android:data="快捷方式3"android:targetClass="com.example.shortcutsdemo.MainActivity"android:targetPackage="com.example.shortcutsdemo" /><intent
android:action="android.intent.action.MAIN"android:data="快捷方式3"android:targetClass="com.example.shortcutsdemo.MainActivity"android:targetPackage="com.example.shortcutsdemo" /><categories android:name="android.shortcut.conversation" />shortcut>
shortcuts>
配置好了\res\xml\shortcuts1.xml文件后,这时候我们不用做其他的操作,直接运行就可以看到效果啦。

这时候当我们点击某个item,就会跳转到我们设置的对应的activity。
但这时候我们并无法区分它是用户以别的页面跳转过来的还是通过点击我们刚刚设置的shortcuts跳转过来的,这时候怎么办呢,我们可以通过判断当前页面的intent来区分。
@Override
protected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);TextView view = findViewById(R.id.text);view.setText(getIntent().getDataString());
}
这是我想到的一个方法,感觉应该还会有更好的方法。
以上就是静态的添加shortcuts的方法,另外还有动态添加shortcuts的方法:
ShortcutManager shortcutManager = null;
ShortcutInfo shortcut = null;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N_MR1) {shortcutManager = getSystemService(ShortcutManager.class);shortcut = new ShortcutInfo.Builder(this, "id1").setShortLabel("Web site").setLongLabel("Open the web site").setIcon(Icon.createWithResource(this, R.mipmap.ic_launcher)).setIntent(new Intent(Intent.ACTION_VIEW,Uri.parse("https://www.mysite.example.com/"))).build();shortcutManager.setDynamicShortcuts(Arrays.asList(shortcut));
}
使部分shortcut失效或激活shortcut:
shortcutManager.enableShortcuts(shortcutIds);
shortcutManager.disableShortcuts(shortcutIds);
效果:

参考:https://developer.android.google.cn/guide/topics/ui/shortcuts.html
原文:http://blog.csdn.net/qq_27512671/article/details/78662718/
转载请注明出处,未经允许不得转载。
本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场,不承担相关法律责任。如若转载,请注明出处。 如若内容造成侵权/违法违规/事实不符,请点击【内容举报】进行投诉反馈!
