Samsung A10 android 11 如何从我的应用程序以编程方式创建其他应用程序固定的快捷方式

Samsung A10 android 11 how to create other apps pinned shortcut programmatically from my app

Samsung A10 android 11 已更新,Galaxy S9 和 Galaxy S10 在这些设备上经过测试但无法正常工作

此代码仅适用于android奥利奥及以上

这是我用于在 android 中以编程方式创建快捷方式的代码。在所有其他设备中,它的工作完美,但在这个特定的设备上,它创建了短但生成了我自己的应用程序快捷方式,而不是我想要的。

val shortcutIntent = finalPackageName?.let {
            context?.packageManager!!.getLaunchIntentForPackage(
                it
            )
        }
        val shortcutManager: ShortcutManager? = context?.getSystemService(ShortcutManager::class.java)
        if (shortcutManager != null) {
            if (shortcutManager.isRequestPinShortcutSupported) {
                val shortcut = ShortcutInfo.Builder(context, "unique_id")
                    .setShortLabel(finalAppName)
                    .setLongLabel("Open the Android Docu")
                    .setIcon(Icon.createWithBitmap(finalBitmap))
                    .setIntent(shortcutIntent!!)
                    .build()

                ((activity) as MainActivity).registerReceiver(object : BroadcastReceiver() {
                    override fun onReceive(context: Context, intent: Intent) {
                        findNavController().navigate(R.id.resultFragment)
                        context.unregisterReceiver(this)
                    }
                }, IntentFilter("test_action"))

                val intent = Intent("test_action")
                val pendingIntent = PendingIntent.getBroadcast(context, 123, intent, 0)
                shortcutManager.requestPinShortcut(shortcut, pendingIntent.intentSender)
            } else
                Toast.makeText(
                    context,
                    "Pinned shortcuts are not supported!",
                    Toast.LENGTH_SHORT
                ).show()
        }

我解决了

if (ShortcutManagerCompat.isRequestPinShortcutSupported(context)) {
    val intent = Intent(context, MainActivity::class.java)
    intent.action = "android.intent.action.MAIN"
    intent.putExtra("appName", originalAppName)
    intent.putExtra("pkgName", finalPackageName)
    val build: ShortcutInfoCompat =
      ShortcutInfoCompat.Builder(context, "uniqueId")
            .setIntent(intent).setShortLabel(
            finalAppName
        ).setIcon(IconCompat.createWithBitmap(finalBitmap)).build()
    val shortcutManager =
    context.getSystemService(ShortcutManager::class.java)
    //context is required when call from the fragment 
    context.registerReceiver(object : BroadcastReceiver() {
        override fun onReceive(context: Context, intent: Intent) {
            //this method is called when shortcut is created
            Log.d("intent", intent.data.toString())
        }
    }, IntentFilter("test_action"))

    val receiverIntent = Intent("test_action")
    val pendingIntent =
        PendingIntent.getBroadcast(context, 123, receiverIntent, 0)
    ShortcutManagerCompat.requestPinShortcut(
        context,
        build,
        pendingIntent.intentSender
    )
    return
}
Toast.makeText(
    context,
    "launcher does not support short cut icon",
    Toast.LENGTH_SHORT
).show()

然后转到您的主 activity 并获取意图数据

val stringExtra = intent.getStringExtra("pkgName")
    if (stringExtra != null) {
        startActivity(packageManager.getLaunchIntentForPackage(stringExtra))
        finish()
    }