Shortcut

Android7.1(API25)开始推出新的功能Shortcuts,通过ShortcutManager进行管理

1.Static Shortcuts

静态注册即利用xml进行注册:

在res/xm下新建shortcuts.xml,其中的属性


shortcutId 一个唯一的id

enabled, 表示这个shortcut是否可用

shortcutShortLabel, 这里是配置的短名称, 下面还会有长名称, 如果长名称显示不下, 就显示短名称

shortcutLongLabel, 这里是配置的长名称, launcher会优先选择长名称显示

shortcutDisabledMessage, 这个配置是在我们选择一个不可用的shortcut时给用户的一个提示

intent, 这里表示我们点击shortcut时要干嘛, targetPackage是指定一个目标应用的包名, targetClass是我们要跳转的目标类, 这里要注意的是android:action一定要配置, 否则会崩溃

categories, 这个东西目前位置官方只给提供了android.shortcut.conversation

能配置shortcuts的activity必须要有action是android.intent.action.MAIN和category是android.intent.category.LAUNCHER! 

最后在Activity的Manifest中<meta-data>android:name"android.app.shortcuts" android:resourse="@xml/shortcuts"</meta-data>

2.Dynamic Shortcuts

动态添加shortCut

private void addDynamicShortcut() {

ShortcutManager  shortcutManager = (ShortcutManager) getSystemService(SHORTCUT_SERVICE);

    //最大添加的个数,即多了会添加不上

    int maxAddSize = shortcutManager.getMaxShortcutCountPerActivity();

    Intent intent =new Intent(this, MainActivity.class);

    intent.setAction("cn.wx.test");

    ShortcutInfo shortcutInfo =new ShortcutInfo.Builder(this, "id123")

.setShortLabel("ShortLabel")

.setLongLabel("LongLabel")

.setIcon(Icon.createWithResource(this, R.mipmap.ic_launcher))

.setIntent(intent)

.build();

    List infoList =new ArrayList<>();

    infoList.add(shortcutInfo);

    //shortcutManager.addDynamicShortcuts(Arrays.asList(shortcutInfo));

    shortcutManager.setDynamicShortcuts(infoList);

}

//DynamicShortcut是可以拖到桌面的,此时就是pinShortcut,此时移除可以通过

private void removeDynamicShortcut() {

ShortcutManager  shortcutManager = (ShortcutManager) getSystemService(SHORTCUT_SERVICE);

    List infoList = shortcutManager.getPinnedShortcuts();

    for (ShortcutInfo shortcutInfo : infoList) {

if("id123".equals(shortcutInfo.getId())) {

shortcutManager.disableShortcuts(Arrays.asList(shortcutInfo.getId(), "暂不支持此功能"));

        }

}

shortcutManager.removeDynamicShortcuts(Arrays.asList("id123"));

}

private void updateDynamicShortcut(String id ) {

ShortcutManager  shortcutManager = (ShortcutManager) getSystemService(SHORTCUT_SERVICE);

    Intent intent =new Intent(this, MainActivity.class);

    intent.setAction("cn.wx.test");

    ShortcutInfo shortcutInfo =new ShortcutInfo.Builder(this, id)

.setShortLabel("ShortLabel_new")

.setLongLabel("LongLabel_new")

.setIcon(Icon.createWithResource(this, R.mipmap.ic_launcher))

.setIntent(intent)

.build();

    shortcutManager.updateShortcuts(Arrays.asList(shortcutInfo));

}

API 26上,ShortcutManager新增了对Pinned Shortcuts(固定快捷方式) 的管理。

增加了isRequestPinShortcutSupported、requestPinShortcut、createShortcutResultIntent三个方法。  

isRequestPinShortcutSupported()判断是否支持添加pinned shortcut。

requestPinShortcut()请求创建固定快捷方式。


例如:

private void addPinnedShortcut() {

ShortcutManager shortcutManager = (ShortcutManager) getSystemService(SHORTCUT_SERVICE);

    if(shortcutManager.isRequestPinShortcutSupported()) {

Intent intent =new Intent(Intent.ACTION_MAIN);

        intent.addCategory(Intent.CATEGORY_LAUNCHER);

        ComponentName component =new ComponentName(getPackageName(), MainActivity.class.getName());

        intent.setComponent(component);

        ShortcutInfo info =new ShortcutInfo.Builder(this, mId)

.setIcon(Icon.createWithResource(this, R.mipmap.ic_launcher))

.setShortLabel("My shortcut")

.setIntent(intent)

.build();

        Intent reIntent =new Intent(this, MyBroadcastReceiver.class);

        reIntent.setAction("wx_add_shortcut");

        PendingIntent pendingIntent = PendingIntent.getBroadcast(this,0 , reIntent, PendingIntent.FLAG_UPDATE_CURRENT);

        shortcutManager.requestPinShortcut(info, pendingIntent.getIntentSender());

    }

}

private class MyBroadcastReceiverextends BroadcastReceiver {

@Override

    public void onReceive(Context context, Intent intent) {

Log.e("wx", "onReceive");

    }

}

//请求后会通过ShortcutManager发出Action(android.content.pm.action.CONFIRM_PIN_SHORTCUT),桌面会适配此Action启动Activity,确定添加成功后回调发送广播到传入的接受器。

getPinnedShortcuts();可以返回当前应用已经添加shortcut。

当然启动shortcut只有默认launcher次可以启动,系统里有做是否默认launcher的判断,其他应用没有权限启动。

当然还有其他方式,因为launcher的其他实质也是通过shortcutManager做个控制,最终调用的也是startActivity,所以如果你能把shortcutInfo对象存储的相关Intent信息拼接起来也可以启动。

例如常用的微信快捷方式启动可以通过如下方式:

Intent intent =new Intent();

intent.setAction("com.tencent.mm.action.BIZSHORTCUT");

intent.setPackage("com.tencent.mm");

intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);

intent.putExtra("LauncherUI.From.Biz.Shortcut", true);

intent.putExtra("app_shortcut_custom_id", "shortcut_500ac29cc3993fc28dc387c28bc289c3ab48c28559c38a0c70013cc3b1");

intent.putExtra("LauncherUI.Shortcut.Username", "shortcut_500ac29cc3993fc28dc387c28bc289c3ab48c28559c38a0c70013cc3b1");

startActivity(intent);

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • #Android 基础知识点总结 ---------- ##1.adb - android debug bridg...
    Mythqian阅读 8,877评论 2 11
  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 175,327评论 25 709
  • What's Run Loops? Why Run Loops? 命令行式执行 Event驱动
    wyymaomi阅读 1,436评论 0 0
  • 我总是在回忆里勿相忘 又会在臆想中偷笑 我不知道繁华人事 是否我们都少了一份真心 是那一份 对爱的人真心相望相守 ...
    人间青凡阅读 1,781评论 0 0
  • 是谁踩碎了一地金色 任由将枯的叶跳舞 是谁碾压了一簇洁白 任由将开的花哭泣 你在脚踏车上臆想 不经意间让悲伤流露 ...
    惘然一書阅读 1,787评论 0 1