【问题标题】:Android App dynamic Shortcut for multi flavour app多风格应用的 Android 应用动态快捷方式
【发布时间】:2017-09-12 08:50:21
【问题描述】:

能否请您帮助配置 应用程序包 以用于 Android 中的动态快捷方式

应用程序代码通过 build.gradle 使用 product flavoring 进行 branding

productFlavors {
    branda {
        applicationId "com.bb.branda"
        ...
    }
    brandb {
        applicationId "com.bb.brandb"
        ...
    }
    ...
    ...

}

对于静态快捷方式,可以在shortcuts.xml 文件中的“targetPackage”标签下进行配置。

例如。 android:targetPackage="com.bb.branda"

<shortcuts xmlns:android="http://schemas.android.com/apk/res/android" >
<shortcut
    android:shortcutId="contact_us"
    android:enabled="true"
    android:icon="@drawable/home_contactus_icon"
    android:shortcutShortLabel="@string/label.contactus"
    android:shortcutLongLabel="@string/label.contactus"
    >
    <intent
        android:action="contactus"
        android:targetPackage="com.bb.brand"
        android:targetClass="com.bb.launcher.LauncherActivity"
        />
    <categories android:name="android.shortcut.conversation" />
</shortcut>

对于动态快捷方式,下面的代码 sn-p 哪里可以做同样的事情?

 if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.N_MR1) {

        ShortcutManager shortcutManager = ctx.getSystemService(ShortcutManager.class);

        ShortcutInfo shortcut = new ShortcutInfo.Builder(ctx, shortcutId)
                .setShortLabel(sTitle)
                .setLongLabel(sTitle)
                .setIcon(Icon.createWithResource(ctx, resID))
                .setIntent(new Intent(sIntentAction))
                .build();

        shortcutManager.addDynamicShortcuts(Arrays.asList(shortcut));
    }

提前致谢。

【问题讨论】:

    标签: android shortcut android-7.1-nougat android-8.0-oreo


    【解决方案1】:

    如果你想要实现的是为intent指定包,你可以通过以下方式创建intent:

        Intent intent = new Intent(sIntentAction);
        intent.setComponent(new ComponentName("your.package.id", YourActivity.class.getName()));
        ShortcutInfo shortcut = new ShortcutInfo.Builder(ctx, shortcutId)
                .setShortLabel(sTitle)
                .setLongLabel(sTitle)
                .setIcon(Icon.createWithResource(ctx, resID))
                .setIntent(intent)
                .build();    
    

    【讨论】:

    • 您能否尝试通过解释您的代码如何解决问题来改进您的答案?
    猜你喜欢
    • 1970-01-01
    • 2015-05-18
    • 1970-01-01
    • 2012-06-14
    • 1970-01-01
    • 2021-04-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多