app 快捷入口

https://developer.android.google.cn/guide/topics/ui/shortcuts/creating-shortcuts#gsi-library


图1

静态方式

<activity android:name=".activity.SplashActivity" android:theme="@style/AppTheme.NoActionBarAndNoTitle">

<intent-filter>

intent 过滤器为 android.intent.action.MAIN 操作和 android.intent.category.LAUNCHER 类别的 Activity。

<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>

shortcuts.xml

<?xml version="1.0" encoding="utf-8"?>

<shortcuts xmlns:android="http://schemas.android.com/apk/res/android">

<shortcut

 android:enabled="true"    

这用于确定用户是否能够与支持的启动器中的快捷方式进行交互。 android:enabled 的默认值为 true。 如果您将其设置为 false,则还应设置 android:shortcutDisabledMessage,用于说明停用该快捷方式的原因。如果您认为自己不需要提供此类消息,最简单的做法就是从 XML 文件中完全移除该快捷方式。

 android:icon="@mipmap/system_message"    图标

 android:shortcutDisabledMessage="@string/str_setting"    

这是当用户尝试启动已停用的快捷方式时出现在支持的启动器中的消息。此消息应向用户解释快捷方式现在停用的原因。 如果 android:enabled 为 true,则此属性的值无效。

 android:shortcutId="goCreatePrescriptions"      

这是一个字符串字面量,表示 ShortcutManager 对象对其执行操作时的快捷方式。注意:不能将此属性的值设置为资源字符串(例如 @string/foo)。

 android:shortcutLongLabel="@string/str_setting"

 android:shortcutShortLabel="@string/str_setting"> 

 <intent

 android:action="android.intent.action.VIEW"                   action 必须写

 android:targetClass="com.ccyl2021.www.activity.mine.Setting.SettingActivity"      目标类

 android:targetPackage="com.ccyl2021.www" >

 </intent>

 <categories android:name="android.intent.category.DEFAULT" />

 </shortcut> 

</shortcuts>

动态方式  

当你去打开某个 activity时 可以通过ShortcutManagerCompat  去添加 快捷菜单的信息; 这种操作完全可以根据大数据等手段分析出用户的行为习惯去动态添加。

val shortIntent = Intent(context, PatientListActivity::class.java).also { intent ->

intent.action = Intent.ACTION_VIEW;

intent.putExtra(EXTRA_KEY_PATIENT_LIST_TYPE, inquiryNotYetType)

intent.putExtra(EXTRA_KEY_SWITCH, "")

}

if (ShortcutManagerCompat.getDynamicShortcuts(it).size >0)return //如果 已经存在就不去添加了

val shortcut = ShortcutInfoCompat.Builder(it, "id1")

.setShortLabel(resources.getString(R.string.str_create_prs))

.setLongLabel(resources.getString(R.string.str_create_prs))

.setIcon(IconCompat.createWithResource(it, R.mipmap.create_prescription))

.setIntent(shortIntent)

.build()

ShortcutManagerCompat.addDynamicShortcuts(it, mutableListOf(shortcut))


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

推荐阅读更多精彩内容