androidkotlinandroid-11android-shortcutmanager

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


Samsung A10 android 11 updated, Galaxy S9 and Galaxy S10 tested on these devices but its not working

This code is only for android Oreo and above

Here is the code which I used for creating the shortcut in android programmatically. In all other devices its work perfectly but on this specific device it create the short but generate my own app shortcut not for desired.

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

Solution

  • I solved it

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

    then go to your main activity and get the intent data

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