App Shortcuts实现长按图标显示快捷入口

App Shortcuts

App Shortcuts是Android7.1上推出的新功能,可以实现点击Launcher上图标弹出快捷入口:
弹出快捷入口

使用Shortcut

使用App Shortcuts有两种形式,类似广播有动态注册和静态注册,App Shortcuts也有两种形式,分别是动态使用和静态使用。

动态使用
public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N_MR1) {
            // android 7.1
            ShortcutManager shortcutManager = getSystemService(ShortcutManager.class);
            ShortcutInfo shortcutInfo = new ShortcutInfo.Builder(this, "id1")
                    .setShortLabel("测试")
                    .setLongLabel("测试测试")
                    .setIcon(Icon.createWithResource(this, R.mipmap.ic_launcher))
                    .setIntent(new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.baidu.com")))
                    .build();
            shortcutManager.setDynamicShortcuts(Arrays.asList(shortcutInfo));
        }
    }
}

效果

  1. 通过获取ShortcutManager来动态设置Shortcut.
  2. 通过build构建一个shortcutInfo对象
  3. 调用shortcutManager#setDynamicShortcuts更新

下面列出可能会用到的API

方法作用
setDynamicShortcuts更新整个Shortcut列表
addDynamicShortcuts添加新的条目
updateShortcuts更新列表
removeDynamicShortcuts移除指定条目
removeAllDynamicShortcuts移除全部的条目
静态使用

在清单文件入口Activity添加meta标签

 <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/shortcuts" />
        </activity>
    </application>

添加xml目录,创建shortcuts xml配置文件

<?xml version="1.0" encoding="utf-8"?>
<shortcuts xmlns:android="http://schemas.android.com/apk/res/android">

    <shortcut
        android:enabled="true"
        android:icon="@mipmap/ic_launcher"
        android:shortcutId="id1"
        android:shortcutLongLabel="@string/app_name"
        android:shortcutShortLabel="@string/app_name">
        <intent
            android:action="android.intent.action_VIEW"
            android:targetPackage="com.welcom.shortcut.shortcutdemo">
        </intent>
    </shortcut>
</shortcuts>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值