Search code examples
androidandroid-intentandroid-permissionsandroid-settings

Unable to Open Settings of application with Intent


I'm dealing with android app permissions in which if the user denied permission permanently I need to move the user to app Settings to enable the permission manually. All the code working fine but when I start an Intent to move the user to Settings of my app I'm getting activity not found Exception.

Here is the Exception

android.content.ActivityNotFoundException: No Activity found to handle Intent { act=android.settings.APPLICATION_DETAILS_SETTINGS dat=Package:com.e.permisions }

This is my code

package com.e.permisions;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;

import android.Manifest;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Bundle;
import android.provider.Settings;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;


public class MainActivity extends AppCompatActivity {
    Button request;
    String[] permissions = {Manifest.permission.READ_EXTERNAL_STORAGE};

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // Initilising buttons
        request = findViewById(R.id.request);


        request.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (ContextCompat.checkSelfPermission(getApplicationContext(), Manifest.permission.READ_EXTERNAL_STORAGE)
                        == PackageManager.PERMISSION_GRANTED) {
                    //Todo: Code to do what if permissions granted...
                    Toast.makeText(MainActivity.this, "Permissions Granted", Toast.LENGTH_SHORT).show();
                } else {
                    if (ActivityCompat.shouldShowRequestPermissionRationale(MainActivity.this, Manifest.permission.READ_EXTERNAL_STORAGE)) {
                        //Todo: usually we should show alert why should need these permissions...
                        new AlertDialog.Builder(MainActivity.this)
                                .setTitle("Why these Permissions?")
                                .setMessage("For App functionality we need these permissions..")
                                .setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                                    @Override
                                    public void onClick(DialogInterface dialogInterface, int i) {
                                        dialogInterface.dismiss();
                                    }
                                })
                                .setPositiveButton("OK", new DialogInterface.OnClickListener() {
                                    @Override
                                    public void onClick(DialogInterface dialogInterface, int i) {
                                        //Todo: request permissions when click ok
                                        ActivityCompat.requestPermissions(MainActivity.this, permissions, 123);
                                    }
                                })
                                .setCancelable(false)
                                .create().show();
                    } else {
                        //Todo: request permissions
                        ActivityCompat.requestPermissions(MainActivity.this, permissions, 123);
                    }
                }
            }
        });
    }


    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        if (requestCode == 123) {
            if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                //Todo: code to do what if permissions are Granted...
                Toast.makeText(this, "Permissions are Granted", Toast.LENGTH_SHORT).show();
            } else if (!ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.READ_EXTERNAL_STORAGE)) {
                //Todo: if permissions Denied permanently by user..
                gotoSettings();
            } else {
                //Todo: what to do if denied
                Toast.makeText(this, "Permissions Denied", Toast.LENGTH_SHORT).show();
            }
        }
    }

    private void gotoSettings() {
        new AlertDialog.Builder(this)
                .setTitle("Go to Settings")
                .setMessage("enable the permission")
                .setPositiveButton("Ok", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        Toast.makeText(MainActivity.this, "Moving to Settings", Toast.LENGTH_SHORT).show();
                        try {
                            Intent intent = new Intent();
                            intent.setAction(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
                            Uri uri = Uri.fromParts("Package", getPackageName(), null);
                            intent.setData(uri);
                            startActivity(intent);
                        } catch (Exception e) {
                            Toast.makeText(MainActivity.this, "failed to open Settings\n" + e, Toast.LENGTH_LONG).show();
                            Log.d("error", e.toString());
                        }

                    }
                }).create().show();
    }
}

Also updates permission details in the manifest file. Anyone can help what's going wrong with this code.

Thanks in advance.


Solution

  • The issue is you are giving "Package" in

    Uri uri = Uri.fromParts("Package", getPackageName(), null);
                            
    

    Because of the case sensitivity it is not finding the view, Try giving package as given

    Uri uri = Uri.fromParts("package", getPackageName(), null);