18

I need to find whether the current Android device allows to install apps from sources other than the market. This option appears in the settings UI under Applications named "Unknown sources".

The setting basically allows you to download and install APKs that did not originate from the Google app market.

How do I check whether this flag is on or off using code?

4 Answers 4

27

Here is the code that uses the mentioned setting:

boolean isNonPlayAppAllowed = Settings.Secure.getInt(getContentResolver(), Settings.Secure.INSTALL_NON_MARKET_APPS) == 1;

Also showing the setting to user might me useful:

if (!isNonPlayAppAllowed) {
    startActivity(new Intent(android.provider.Settings.ACTION_SECURITY_SETTINGS));
}
4
  • Where to Add this code in an application? In main Activity or anywhere where else?
    – NetStarter
    Jun 10, 2013 at 11:04
  • The question was finding "Unknown sources" instalation flag. The variable isNonPlayAppAllowed is that flag. It's defined with all static methods, so you can define/use it anywhere in your code. You just need getContentResover(), meaning you just need context.
    – MSquare
    Jun 11, 2013 at 13:27
  • Doc Says: INSTALL_NON_MARKET_APPS constant was deprecated in API level 17. Starting from Build.VERSION_CODES.O, apps should use PackageManager.canRequestPackageInstalls() Jul 5, 2018 at 8:51
  • 3
    @HusnainAslam This was bothering me too, but I found this report by Commonsware (commonsware.com/blog/2014/10/17/…) where he says that on API level 21, the Settings.Secure.INSTALL_NON_MARKET_APPS was UNdeprecated, so I am using it again. (Note the class, it is from Settings.Secure, not Settings.Global) Jul 18, 2018 at 11:59
12

This setting is called "INSTALL_NON_MARKET_APP" (click for documentation).

(I basically already typed the question when I found the answer hidden deep within documentation, with a different name, so I decided to post the question and self answer, since it's not trivial.)

4
    boolean allow = false;
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
         allow = this.getPackageManager().canRequestPackageInstalls();
        } else {
        try {
         allow = Settings.Secure.getInt(getContentResolver(), Settings.Secure.INSTALL_NON_MARKET_APPS) == 1;
            } catch (Settings.SettingNotFoundException e) {
              e.printStackTrace();
              }
       }
    
    if (!allow)
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
       startActivity(new Intent(Settings.ACTION_MANAGE_UNKNOWN_APP_SOURCES));
       } else {
       startActivity(new Intent(Settings.ACTION_SECURITY_SETTINGS));
       }
1

you can request the package manager to tell you

 applicationContext.packageManager.canRequestPackageInstalls()

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.