簡(jiǎn)介
當(dāng)我們?cè)陂L(zhǎng)按Android應(yīng)用的桌面圖標(biāo)時(shí),一般回彈出一個(gè)列表,上面一般有應(yīng)用信息、卸載應(yīng)用等功能,并且部分應(yīng)用在這里還添加了自己的快捷方式,今天主要介紹如何添加自定義的快捷方式。
長(zhǎng)按桌面顯示的快捷方式在Android中叫Shortcut,它有兩種創(chuàng)建方式,分為靜態(tài)創(chuàng)建和動(dòng)態(tài)創(chuàng)建,并且跟Widget一樣,也有對(duì)應(yīng)的管理類(lèi)來(lái)進(jìn)行管理。廢話不多說(shuō),直接開(kāi)始
靜態(tài)創(chuàng)建
第一步
靜態(tài)創(chuàng)建分兩個(gè)步驟,第一個(gè)是在Manifest文件中指定快捷方式配置文件的位置,首先找到Launcher的activity標(biāo)簽,添加meta-data標(biāo)簽如下:
<activity
android:name=".MainActivity"
android:exported="true">
<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>
第二步
在resources資源目錄下創(chuàng)建xml文件夾,并在文件夾中創(chuàng)建名為shortcuts的xml文件,其中內(nèi)容如下:我們創(chuàng)建了兩個(gè)標(biāo)簽
<?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_round"
android:shortcutDisabledMessage="@string/disable_hint_message"
android:shortcutId="shortcut1"
android:shortcutLongLabel="@string/long_label_one"
android:shortcutShortLabel="@string/short_label_one">
<intent
android:action="android.intent.action.VIEW"
android:targetClass="com.example.helloworld.RecyclerViewTestActivity"
android:targetPackage="com.example.helloworld">
<extra
android:name="key1"
android:value="value1" />
</intent>
</shortcut>
<shortcut
android:enabled="true"
android:icon="@mipmap/ic_launcher_round"
android:shortcutDisabledMessage="@string/disable_hint_message"
android:shortcutId="shortcut2"
android:shortcutLongLabel="@string/long_label_two"
android:shortcutShortLabel="@string/short_label_two">
<intent
android:action="android.intent.action.VIEW"
android:targetClass="com.example.helloworld.TestViewPagerActivity"
android:targetPackage="com.example.helloworld">
<extra
android:name="key2"
android:value="value2" />
</intent>
</shortcut>
</shortcuts>
<string name="short_label_one">名稱(chēng)一</string>
<string name="short_label_two">名稱(chēng)二</string>
<string name="long_label_one">一個(gè)很長(zhǎng)很長(zhǎng)的長(zhǎng)到你無(wú)法相信的名稱(chēng)一</string>
<string name="long_label_two">一個(gè)略長(zhǎng)略長(zhǎng)的長(zhǎng)到你可以想象的名稱(chēng)二</string>
<string name="disable_hint_message">你確認(rèn)要禁用該快捷方式嗎?</string>
- enabled 是否開(kāi)啟
- icon 圖標(biāo)
- shortcutDisabledMessage 關(guān)閉該快捷方式提示的信息
- shortcutId 唯一標(biāo)識(shí)
- shortcutLongLabel 長(zhǎng)標(biāo)簽,優(yōu)先展示
- shortcutShortLabel 短標(biāo)簽,長(zhǎng)標(biāo)簽展示不下會(huì)展示短標(biāo)簽
- intent 跳轉(zhuǎn)至Activity的Intent
注意事項(xiàng)
通過(guò)xml創(chuàng)建的靜態(tài)ShortCuts不可以通過(guò)代碼動(dòng)態(tài)修改與刪除,只能通過(guò)xml去修改。
動(dòng)態(tài)創(chuàng)建
使用ShortCuts相關(guān)的類(lèi)以及方法即可快速實(shí)現(xiàn)動(dòng)態(tài)快捷方式的添加、更改、刪除等操作。文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-420990.html
object ShortCutManager {
fun createShortCuts(context: Context) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N_MR1) {
ShortcutManagerCompat.removeAllDynamicShortcuts(context)
val shortCut = ShortcutInfoCompat.Builder(context, "shortCutId")
.setShortLabel("搜索一下")
.setLongLabel("搜索一下下")
.setIcon(IconCompat.createWithResource(context, R.mipmap.ic_launcher))
.setIntent(Intent(context,MainActivity::class.java).apply {
action = Intent.ACTION_VIEW
})
.build()
ShortcutManagerCompat.pushDynamicShortcut(context, shortCut)
}
}
}
再合適的地方調(diào)用上面的方法即可實(shí)現(xiàn)添加ShortCuts,在添加之前刪除了所有的動(dòng)態(tài)快捷方式,但是并不會(huì)刪除通過(guò)xml配置的靜態(tài)快捷方式。文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-420990.html
到了這里,關(guān)于Android桌面長(zhǎng)按圖標(biāo)快捷方式——Shortcuts的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!