Android7.1 Shortcut快捷方式运用

效果:

 

两种方式:静态和动态

静态:

1、xml目录下新建一个文件,比如命名为static_shortcuts.xml:

<?xml version="1.0" encoding="utf-8"?>
<shortcuts xmlns:tools="http://schemas.android.com/tools"
    xmlns:android="http://schemas.android.com/apk/res/android">
    <shortcut
        android:enabled="true"
        android:icon="@drawable/ic_search_black_24dp"
        android:shortcutId="search"
        android:shortcutDisabledMessage="@string/search"
        android:shortcutLongLabel="@string/search"
        android:shortcutShortLabel="@string/search"
        tools:targetApi="n_mr1" >
        <intent
            android:action="android:intent.action.VIEW"
            android:targetClass="com.xaehu.shortcut.activity.MainActivity"
            android:targetPackage="com.xaehu.shortcut" />
        <intent
            android:action="android:intent.action.VIEW"
            android:targetClass="com.xaehu.shortcut.activity.SearchActivity"
            android:targetPackage="com.xaehu.shortcut" />
        <categories android:name="android.shortcut.conversation" />
    </shortcut>

    <shortcut
        android:enabled="true"
        android:icon="@drawable/ic_widgets_black_24dp"
        android:shortcutId="all"
        android:shortcutDisabledMessage="@string/all"
        android:shortcutLongLabel="@string/all"
        android:shortcutShortLabel="@string/all"
        tools:targetApi="n_mr1" >
        <intent
            android:action="android:intent.action.VIEW"
            android:targetClass="com.xaehu.shortcut.activity.MainActivity"
            android:targetPackage="com.xaehu.xakan" />
        <intent
            android:action="android:intent.action.VIEW"
            android:targetClass="com.xaehu.shortcut.activity.AllVideoActivity"
            android:targetPackage="com.xaehu.xakan" />
        <categories android:name="android.shortcut.conversation" />
    </shortcut>

</shortcuts>

 2、AndroidManifest.xml里面启动的App那里添加一个<meta-data />:

<activity android:name=".activity.MainActivity"
    android:configChanges="orientation|screenSize|keyboardHidden|locale|uiMode"
    android:screenOrientation="portrait">
    <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/static_shortcuts" />
</activity>

就可以了。

但是实际开发可能会遇到Intent需要带数据给你要打开的页面的,那静态的方法就解决不了了,用动态的方法:

动态:

addDynamicShortcuts方法可以添加一个快捷方式,同时保留静态声明的快捷方式,如果add的快捷方式id一样会覆盖上去。

setDynamicShortcuts方法可以完全替换掉原来的。

updateShortcuts可以更新已有的快捷方式 

if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.M) {
    List<ShortcutInfoCompat> dynamicShortcuts = ShortcutManagerCompat.getDynamicShortcuts(this);
    if (dynamicShortcuts.size() == 1) {
        ShortcutInfoCompat infoCompat = dynamicShortcuts.get(0);
        infoCompat.getIntents()[1].putExtra(BaseConstant.Key_line, line);
        ShortcutManagerCompat.updateShortcuts(this,dynamicShortcuts);
    }else{
        //点击返回走的Intent
        Intent homeIntent = new Intent(this,MainActivity.class);
        homeIntent.setAction(Intent.ACTION_VIEW);
        //点击快捷方式走的Intent
        Intent intent = new Intent(this,DetailActivity.class);
        intent.setAction(Intent.ACTION_VIEW);
        intent.putExtra(BaseConstant.Key_isStar, true);
        intent.putExtra(BaseConstant.Key_line, line);

        Intent[] intents = {homeIntent,intent};
        ShortcutInfoCompat info = new ShortcutInfoCompat.Builder(this,"star")
                .setShortLabel(getString(R.string.star))
                .setLongLabel(getString(R.string.star))
                .setIcon(IconCompat.createWithResource(this,R.drawable.ic_stars_black_24dp))
                .setIntents(intents)
                .build();
                
        //如果有多个快捷方式,可以用一个list装起来传给下面的第二个参数。
        ShortcutManagerCompat.addDynamicShortcuts(this, Collections.singletonList(info));
    }

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值