Android AppShortcuts 桌面启动图标长安弹出框 类似支付宝 出现扫一扫 付款

使用到的Android原生框架 AppShortcuts。桌面快捷 按钮

 

 有两种方式实现第一中在manifest配置静态方式。

使用:在启动类加入如下meta-adta

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

在xml文件夹创建shortcuts 文件:

每一个shortcut为弹出框的一个item。最大可加4个(google说是5个)

icon :item图标

shortcutId:item id 不可重复  如果重复只会显示第一个。

shortcutLongLabel: item文本

intent:点击目标intent

         targetClass :目标类路径

         targetPackage:目标应用包名(可以不是自己的应用只要安装了能找到对应类就可以跳转) 

<?xml version="1.0" encoding="utf-8"?>
<shortcuts xmlns:android="http://schemas.android.com/apk/res/android">

    <shortcut
        android:enabled="true"
        android:icon="@mipmap/del"
        android:shortcutDisabledMessage="@string/static_message"
        android:shortcutId="static1"
        android:shortcutLongLabel="@string/static_long_label_1"
        android:shortcutShortLabel="@string/static_short_label_1">
        <intent
            android:action="android.intent.action.VIEW"
            android:targetClass="com.example.myapplication.StaticTestActivity"
            android:targetPackage="com.example.myapplication" />
    </shortcut>

    <shortcut
        android:enabled="true"
        android:icon="@mipmap/close"
        android:shortcutDisabledMessage="@string/static_message"
        android:shortcutId="static2"
        android:shortcutLongLabel="@string/static_long_label_2"
        android:shortcutShortLabel="@string/static_short_label_2">
        <intent
            android:action="android.intent.action.VIEW"
            android:targetClass="com.example.myapplication.StaticTestActivity"
            android:targetPackage="com.example.myapplication" />
    </shortcut>

</shortcuts>

需要把显示内容设置到string.xml文件。否则报错:属性解析失败

<string name="static_message">asdf</string>
<string name="static_long_label_1">qwe</string>
<string name="static_short_label_1">qwe1</string>
<string name="static_long_label_2">qwe2</string>
<string name="static_short_label_2">qwe3</string>

第二种在代码中动态配置。

猜你喜欢

转载自blog.csdn.net/qq_36355271/article/details/105968305
今日推荐