'How to check unknown source is enabled or not on Android 7(Nougat)?

I'm trying to install an APK on Android 7 Nougat(API 24) now. and i faced a weird issue. the Unknown source system pop-up appears in all Android version. when installing an APK, if i haven't enabled the unknown source before, the Android system asks me to enable it or not. until now, it's fine. but the problem is, right after enabling the unknown source, in all Android version, they show me the install system pop-up again but Android 7.

this is my code.

private fun startInstallAPKInNougat(apkFile: File) {
        val apkUri = getURIFromFile(requireContext(), apkFile)
        val intent = Intent(Intent.ACTION_VIEW).apply {
            setDataAndType(apkUri, "application/vnd.android.package-archive")
            flags = Intent.FLAG_ACTIVITY_CLEAR_TASK or Intent.FLAG_ACTIVITY_NEW_TASK
            addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
        }
        requireActivity().startActivity(intent)
        requireActivity().finish()
    }

why?
In previous version of Android 7 or greater than 7, it even works well. i mean Kitkat, Lollipop, Mashmellow and Oreo, Pie and so on..

So, i want to know how i can check the unknown source is enabled or not to install the APK. the process that i want is...

  1. check the unknown source whether it's enabled or not in Android 7.
  2. If it's not enabled, ask user to enable it.
  3. right after enabling, ask user to install it once again.

or if there is automatic way, please tell me. :(



Solution 1:[1]

Okay, i solved this picky one. it's only 2steps.

  1. use Settings.Secure.getInt(context.contentResolver, Settings.Secure.INSTALL_NON_MARKET_APPS) == 0. if it's true, then the unknown source is not enabled now.
fun installAPKInNougat(apkFile: File) {
        val isNotAllowed = Settings.Secure.getInt(context.contentResolver, Settings.Secure.INSTALL_NON_MARKET_APPS) == 0
        if (isNotAllowed) {
            val intent = Intent(Settings.ACTION_SECURITY_SETTINGS)
            startActivityForResult(intent, UNKNOWN_SOURCE_INTENT_REQUEST_CODE)
        } else {
            val apkUri = getURIFromFile(requireContext(), apkFile)
            val intent = Intent(Intent.ACTION_VIEW).apply {
                setDataAndType(apkUri, "application/vnd.android.package-archive")
                flags = Intent.FLAG_ACTIVITY_CLEAR_TASK or Intent.FLAG_ACTIVITY_NEW_TASK
                addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
            }
            requireActivity().startActivity(intent)
            requireActivity().finish()
        }
    }
  1. you have to override onActivityResult or you can use registerforactivityResult. and this case, i used onActivityResult.
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)

        if (resultCode == Activity.RESULT_CANCELED) {
            if (Build.VERSION.SDK_INT == Build.VERSION_CODES.N || Build.VERSION.SDK_INT == Build.VERSION_CODES.N_MR1) {
                val isAllowed = Settings.Secure.getInt(context.contentResolver, Settings.Secure.INSTALL_NON_MARKET_APPS) == 1
                if (isAllowed) {
                    installAPK()
                } else {
                    // do something if it's not allowed
                }
            }
        }
    }

note that you can see Settings.Secure.INSTALL_NON_MARKET_APPS is deprecated. but i tested it and it's fine. it worked well. so you can use it.

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 CodingBruceLee