美文网首页
AndroidShortCuts

AndroidShortCuts

作者: 崽子猪 | 来源:发表于2019-06-12 15:43 被阅读0次

先附属一波效果图吧

长按手机应用弹出 点击按钮跳转界面

静态创建界面

第一步
进入AndroidManifest中找到 然后再Activity中加入
<meta-data
android:name="android.app.shortcuts"
android:resource="@xml/shortcuts" />

<application
        .........
        <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中写
我们直接运行是不可以的,这里我们需要将这个文件生成v25目录下的 Alt+Enter代码提示会帮我们生成

生成v25包
  • android:icon="@drawable/add" ---> 显示图
  • 三个String类型需要自己到String文件下声明
  • android:data ---> 显示传过去的值
  • shortcutShortLabel ---> 显示默认文字
  • android:targetClass / android:targetPackage 都需要换成自己的包名类名
  • android:targetClass ---> 选择跳转到哪个界面
    具体参数配置说明如下:


    参数配置
<?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/add"
        android:shortcutDisabledMessage="@string/static_disabled_message" 
        android:shortcutId="staticId"
        android:shortcutLongLabel="@string/static_shortcut_long_label"
        android:shortcutShortLabel="@string/static_shortcut_short_label">
        <intent
            android:action="android.intent.action.VIEW"
            android:data="content://baidu.com/祁威皓跳转Come On"
            android:targetClass="com.baidu.androidshortcuts.TestActivity"
            android:targetPackage="com.baidu.androidshortcuts" />
        <categories android:name="android.shortcut.conversation" />
    </shortcut>
</shortcuts>

第三布 在新建的Activity中获取传输过来的值

if (getIntent().getData() != null && TextUtils.equals(getIntent().getAction(), Intent.ACTION_VIEW)) {
            Uri uri = getIntent().getData();
            List<String> pathSegments = uri.getPathSegments();
            if (pathSegments != null && pathSegments.size() > 0) {
                tv.setText(pathSegments.get(0));
            }
}

动态创建界面

动态快捷方式提供向指向应用内特定的跳转或数据传递,这些跳转和数据可能会在应用执行中发生变化。
此时需要借助 ShortcutManager 提供的 API 来完成动态快捷方式的相应操作

  • 创建: 使用 setDynamicShortcuts() 重新定义动态快捷方式的完整列表
  • 添加: 使用 addDynamicShortcut() 来扩充现有的动态快捷方式列表
  • 更新: 使用 updateShortcuts() 方法进行更新现有的动态快捷方式列表
  • 删除: 使用 removeDynamicShortcuts() 移除一组动态快捷方式,或者使用 removeAllDynamicShortcuts() 移除所有动态快捷方式

以创建为例,其他差不多,自行尝试,具体的操作可参考下面的代码

1. 创建 ShortcutInfo 对象

@TargetApi(Build.VERSION_CODES.N_MR1)
   private ShortcutInfo createShortcutInfo1() {
     return new ShortcutInfo.Builder(this, ID_DYNAMIC_1)
       .setShortLabel(getString(R.string.dynamic_shortcut_short_label1))
       .setLongLabel(getString(R.string.dynamic_shortcut_long_label1))
       .setIcon(Icon.createWithResource(this, R.drawable.add))
       .setIntent(new Intent(Intent.ACTION_VIEW, Uri.parse("http://xiaweizi.cn/")))
       .build();
} 

**2. 调用 setDynamicShortcuts() 覆盖掉之前的,重新设置新的动态快捷方式列表 **

private void setDynamicShortcuts() {
 if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.N_MR1) {
   ShortcutManager shortcutManager = getSystemService(ShortcutManager.class);
   List<ShortcutInfo> shortcutInfo = new ArrayList<>();
   shortcutInfo.add(createShortcutInfo1());
   shortcutInfo.add(createShortcutInfo2());
   if (shortcutManager != null) {
     shortcutManager.setDynamicShortcuts(shortcutInfo);
   }
 }
}

** 3. 可以配置 label 的字体颜色**

@TargetApi(Build.VERSION_CODES.N_MR1)
private ShortcutInfo createShortcutInfo2() {
 Intent intent = new Intent(this, TestActivity.class);
 intent.setAction(Intent.ACTION_VIEW);
 intent.putExtra("key", "fromDynamicShortcut");
 ForegroundColorSpan colorSpan = new ForegroundColorSpan(Color.BLUE);
 String label = getResources().getString(R.string.dynamic_shortcut_short_label2);
 SpannableStringBuilder colouredLabel = new SpannableStringBuilder(label);
 colouredLabel.setSpan(colorSpan, 0, label.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
 return new ShortcutInfo.Builder(this, ID_DYNAMIC_2)
   .setShortLabel(colouredLabel)
   .setLongLabel(getString(R.string.dynamic_shortcut_long_label2))
   .setIcon(Icon.createWithResource(this, R.drawable.link))
   .setIntent(intent)
   .build();
} 

-------------------------------持续更新------------------------------------------

相关文章

  • AndroidShortCuts

    先附属一波效果图吧 静态创建界面 第一步进入AndroidManifest中找到 然后再Activity中加入 第...

网友评论

      本文标题:AndroidShortCuts

      本文链接:https://www.haomeiwen.com/subject/yrelfctx.html