4

三星 A10 android 11 更新,Galaxy S9 和 Galaxy S10 在这些设备上进行了测试,但无法正常工作

此代码仅适用于 android Oreo 及以上版本

这是我用于以编程方式在 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()
        }
4

1 回答 1

5

我解决了

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

然后转到您的主要活动并获取意图数据

val stringExtra = intent.getStringExtra("pkgName")
    if (stringExtra != null) {
        startActivity(packageManager.getLaunchIntentForPackage(stringExtra))
        finish()
    }
于 2021-10-07T04:29:54.517 回答