拥抱Android O,Android固定快捷方式App Shortcuts

由来

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

这个小工能的添加可谓是非常方便的,在我们日常的应用场景中,有时候我们可能想用QQ发一条空间动态。

    A[QQ] -->|常规方式| B[动态]
    B --> C[右上角+号按钮]
    C --> D[发说说]
    A -->|App Shortcuts| E[快捷菜单]
    E --> D

App Shortcuts的增加,无疑简化了用户的操作,提升了用户体验度。

下面我们就用一个Demo来介绍下如何定制我们的App Shortcuts:


首先新建项目,找到AndroidManifest.xml在主Activity添加:

<?xml version="1.0" encoding="utf-8"?>
<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>
            <!--这里添加shortcuts配置文件-->
            <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" />
        <!-- 如果这里的intent配置了多个的话,那么点击此项shortcut的话就会依次启动,当点击back的时候也是依次从栈顶退出页面 -->
    </shortcut>
    <!-- 更多的shortcuts在这里添加. -->
</shortcuts>

配置好了\res\xml\shortcuts1.xml文件后,这时候我们不用做其他的操作,直接运行就可以看到效果啦。
shortcuts.png

这时候当我们点击某个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);

效果:
shortcuts2.png

参考:https://developer.android.google.cn/guide/topics/ui/shortcuts.html
原文:http://blog.csdn.net/qq_27512671/article/details/78662718/
转载请注明出处,未经允许不得转载。

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

痕迹丶

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值