三星A10安卓11如何创建其他应用程序固定快捷方式从我的应用程序



三星A10安卓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()
}

我解决了

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

相关内容

最新更新