How to check the Install Unknown Apps Toggle is on / off dynamically in Android

1.4k views Asked by At

I have added the below code for redirecting to the Install Unknown Apps Screen run time using Settings.ACTION_MANAGE_UNKNOWN_APP_SOURCES.

But how do I check the Toggle button is On or Off?

I need to download the APK file if Install Unknown Resouces enabled otherwise it needs to be redirected to the Permissions page that indicated in the image.

Click this image to check for more..

    package com.example.demoapp;
    
    import android.content.Intent;
    import android.content.pm.PackageManager;
    import android.net.Uri;
    import android.os.Bundle;
    import android.provider.Settings;
    import android.view.View;
    import android.widget.Button;
    
    import androidx.appcompat.app.AppCompatActivity;
    
    public class MainActivity extends AppCompatActivity {
    
        Button mButtonDownload;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            mButtonDownload = findViewById(R.id.btn_click);
    
            mButtonDownload.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    boolean appInstalled = isAppInstalled("com.example.demoapp");
                    if (appInstalled) {
                        System.out.println("App already installed.");
    
                        Intent intent = null;
                        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
                            intent = new Intent(Settings.ACTION_MANAGE_UNKNOWN_APP_SOURCES);
                            intent.setData(Uri.parse("package:" + getPackageName()));
                            startActivity(intent);
                        }
                    }
                }
            });
        }
    
        private boolean isAppInstalled(String packageName) {
            PackageManager pm = getPackageManager();
            boolean installed = false;
            try {
                pm.getPackageInfo(packageName, PackageManager.GET_ACTIVITIES);
                installed = true;
            } catch (PackageManager.NameNotFoundException e) {
                installed = false;
            }
            return installed;
        }
    }

Please suggest me in advance.

0

There are 0 answers