Wednesday, 6 October 2021

Samsung A10 android 11 does not create app shortcut programmatically

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)
        val uid: UUID? = UUID.randomUUID()
        if (shortcutManager != null) {
            if (shortcutManager.isRequestPinShortcutSupported) {
                val shortcut = ShortcutInfo.Builder(context, uid.toString())
                    .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()
        }

Thanks in advance :)



from Samsung A10 android 11 does not create app shortcut programmatically

No comments:

Post a Comment