安卓长按应用图标弹出快捷方式(shortcut)

一、静态方式

1.1、在res/xml/目录下创建一个新的xml文件,这里我们命名为:shortcuts.xml。

代码:

<?xml version="1.0" encoding="utf-8"?>
<shortcuts xmlns:android="http://schemas.android.com/apk/res/android">
<shortcut
android:shortcutId="static1"
android:enabled="false"
android:icon="@drawable/launch"
android:shortcutDisabledMessage="@string/nav_btn_login"
android:shortcutLongLabel="@string/nav_btn_login"
android:shortcutShortLabel="@string/nav_btn_login">
<intent
android:action="android.intent.action.VIEW"
android:targetClass="com.secondui.activity.TestActivity"
android:targetPackage="com.myProject" />
<categories android:name="android.shortcut.conversation"/>
</shortcut>
<shortcut
android:shortcutId="static2"
android:enabled="true"
android:icon="@drawable/launch"
android:shortcutDisabledMessage="@string/nav_btn_login"
android:shortcutLongLabel="@string/nav_btn_login"
android:shortcutShortLabel="@string/nav_btn_login">
<intent
android:action="android.intent.action.VIEW"
android:targetClass="com.secondui.activity.LoginActivity"
android:targetPackage="com.myProject" />
        <categories android:name="android.shortcut.conversation"/>
</shortcut>
</shortcuts>

代码说明:
     1、shortcutId 一个唯一的id,不能重复。

     2、enabled 表示这个shortcut是否可用。不可用时,就不会显示这个快捷方式。
     3、shortcutShortLabel 这里是配置的短名称。如果长名称显示不下, 就显示短名称。这里必须使用@string的方式,不能直接写字符串。
     4、shortcutLongLabel 这里是配置的长名称。launcher会优先选择长名称显示。这里必须使用@string的方式,不能直接写字符串。

     5、shortcutDisabledMessage 这个配置是在我们选择一个不可用的shortcut时给用户的一个提示。没有意义,因为不可用时就不显示这个快捷方式。这里必须使用@string的方式,不能直接写字符串。

     6、intent 这里表示我们点击shortcut时要做什么。targetPackage是你的应用的包名;targetClass是我们要跳转的目标类;这里要注意的是android:action一定要配置, 否则会崩溃。
     7、categories 这个东西目前位置官方只给提供了android.shortcut.conversation。

 

1.2、在AndroidManifest.xml中启动的activity中添加meta-data。

<meta-data
android:name="android.app.shortcuts"
android:resource="@xml/shortcuts" />

  特别说明:只有在具有如下配置的activity中才能添加meta-data来使用快捷方式。

<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>

二、动态方式
2.1、主要的类ShortcutManager,其方法如下。
    动态发布: setDynamicShortcuts(), addDynamicShortcuts(List);
    动态更新: updateShortcuts(List);
    动态删除: removeDynamicShortcuts(List), removeAllDynamicShortcuts();
示例代码:
@RequiresApi(api = Build.VERSION_CODES.N_MR1)
private void qiTest(){
ShortcutManager shortcutManager=getSystemService(ShortcutManager.class);
//使用Dynamic Shortcuts
List<ShortcutInfo> shortcutInfoList=new ArrayList<>();
//扫一扫
Intent intent = new Intent(this, TestActivity.class);
intent.setAction(Intent.ACTION_VIEW);
ShortcutInfo info = new ShortcutInfo.Builder(this, TestActivity.class.getName())
.setShortLabel("扫一扫")
.setLongLabel("扫一扫")
.setIcon(Icon.createWithResource(this,R.drawable.launch))
.setIntent(intent)
.build();
shortcutInfoList.add(info);
//登录
Intent intent2 = new Intent(this, LoginActivity.class);
intent2.setAction(Intent.ACTION_VIEW);
ShortcutInfo info2 = new ShortcutInfo.Builder(this, LoginActivity.class.getName())
.setShortLabel("登录了")
.setLongLabel("来登录")
.setIcon(Icon.createWithResource(this,R.drawable.launch))
.setIntents(new Intent[]{new Intent(this, StartActivity.class).setAction(Intent.ACTION_VIEW),intent2})
.build();
shortcutInfoList.add(info2);
shortcutManager.setDynamicShortcuts(shortcutInfoList);
//移除某Pinning Shortcuts
for (ShortcutInfo shortcutInfo: shortcutManager.getPinnedShortcuts()) {
if (shortcutInfo.getId().equals(TestActivity.class.getName()))shortcutManager.disableShortcuts(Arrays.asList(shortcutInfo.getId()),"已移除!");
}
//移除某Dynamic Shortcuts
shortcutManager.removeDynamicShortcuts(Arrays.asList(TestActivity.class.getName()));
}
特别说明:1、setIntents可以设置多个intent,从而实现back stack的效果,数组最后一个对象是目标类。只设置一个intent,返回后直接回退到手机桌面。
     2、Pinning Shortcuts的操作只有用户有权限,开发者不可操作,因此这里的移除的效果是快捷方式的图标会被置灰,同时可以给用户一个友好的提示语。
     3、disableShortcuts方法被执行时不仅仅移除了Pinning Shortcuts而且会移除Dynamic Shortcuts,所以不需要再调用removeDynamicShortcuts方法。

    

 

 

 

posted @ 2021-08-09 13:44  Prime_T  阅读(1344)  评论(0编辑  收藏  举报