'How to open app after installing manually


Installing App Manually


 val install = Intent(Intent.ACTION_VIEW)
                    install.setDataAndType(
                        uri,
                        APP_INSTALL_PATH
                    )
                    //                        install.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)

                    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
                        install.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
                        install.putExtra(Intent.EXTRA_NOT_UNKNOWN_SOURCE, true)
                    }
                    startActivity(install)

Handling Broadcast Receiver


val intentFilter = IntentFilter()
intentFilter.addAction(Intent.ACTION_PACKAGE_ADDED)
intentFilter.addAction(Intent.ACTION_PACKAGE_REMOVED)
intentFilter.addAction(Intent.ACTION_PACKAGE_FULLY_REMOVED)
intentFilter.addAction(Intent.ACTION_PACKAGE_INSTALL)
intentFilter.addAction(Intent.ACTION_PACKAGE_REPLACED)
intentFilter.addAction(Intent.ACTION_MY_PACKAGE_REPLACED)
intentFilter.addDataScheme("package")
registerReceiver(restartAppReceiver, intentFilter)
  private val restartAppReceiver = object : BroadcastReceiver() {
        override fun onReceive(context: Context?, intent: Intent?) {
            Toast.makeText(this@LoginActivity, getString(R.string.sign_out), Toast.LENGTH_LONG)
                .show()
            //start activity
            val i = Intent()
            Log.i("App_started", "Yes")
            i.setClassName(packageName, packageName+".screen.activity.LoginActivity")
            i.flags = Intent.FLAG_ACTIVITY_NEW_TASK
            context?.startActivity(i)
        }
    }

Problem Facing


Here I'm not able to receive anything on receiver when app gets installed manually. I need to open the app automatically when it gets installed manually without any action from users end.



Solution 1:[1]

thats simple: you can't. user must open it manually. starting Android 10 you can't start any Activity from BroadcastReceiver. besides that: most of your IntentFilter entries are no-op (why are you calling addDataScheme)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1