无法使用 Intent 打开应用程序设置

Unable to Open Settings of application with Intent

我正在处理 android 应用权限,如果用户永久拒绝权限,我需要将用户移至应用设置以手动启用权限。 所有代码都工作正常,但是当我启动一个 Intent 将用户移动到我的应用程序的设置时,我得到 activity not found Exception.

例外情况

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

这是我的代码

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();
    }
}

同时更新清单文件中的权限详细信息。 任何人都可以帮助解决此代码的问题。

提前致谢。

try this way ...
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",
                                    BuildConfig.APPLICATION_ID, null);
                            intent.setData(uri);
                            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                            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();
}

问题是您在

中提供“包裹”
Uri uri = Uri.fromParts("Package", getPackageName(), null);
                        

由于区分大小写,它找不到视图,请尝试按给定的方式给出 package

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