无法通过 Intent 打开应用程序的设置

问题描述

我正在处理 android 应用程序权限,如果用户永久拒绝权限,我需要将用户移动到应用程序设置以手动启用权限。 所有代码都工作正常,但是当我开始意图将用户移动到我的应用程序的设置时,我发现活动未找到异常。

这里是例外

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",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,123);
                    }
                }
            }
        });
    }


    @Override
    public void onRequestPermissionsResult(int requestCode,@NonNull String[] permissions,@NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode,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",null);

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...