找不到处理意图“应用程序设置”的活动

4
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && ContextCompat.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {

        Intent i = new Intent(Settings.ACTION_APPLICATION_SETTINGS, Uri.parse("package:" + getPackageName()));
        finish();
        startActivity(i);
        return;

    }

我试图检查是否为sdk版本marshmallow或以上,但每次尝试运行时总是导致以下错误

12-26 08:21:16.982 6330-6330/com.example.asus.retrofittest3 E/AndroidRuntime: FATAL EXCEPTION: main
                                                                          Process: com.example.asus.retrofittest3, PID: 6330
                                                                          java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.asus.retrofittest3/com.example.asus.retrofittest3.HomeActivity}: android.content.ActivityNotFoundException: No Activity found to handle Intent { act=android.settings.APPLICATION_SETTINGS dat=package:com.example.asus.retrofittest3 }
                                                                              at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2706)
                                                                              at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2767)
                                                                              at android.app.ActivityThread.-wrap12(ActivityThread.java)
                                                                              at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1514)
                                                                              at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                              at android.os.Looper.loop(Looper.java:163)
                                                                              at android.app.ActivityThread.main(ActivityThread.java:6205)
                                                                              at java.lang.reflect.Method.invoke(Native Method)
                                                                              at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:904)
                                                                              at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:794)
                                                                           Caused by: android.content.ActivityNotFoundException: No Activity found to handle Intent { act=android.settings.APPLICATION_SETTINGS dat=package:com.example.asus.retrofittest3 }
                                                                              at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1816)
                                                                              at android.app.Instrumentation.execStartActivity(Instrumentation.java:1525)
                                                                              at android.app.Activity.startActivityForResult(Activity.java:4402)
                                                                              at android.support.v4.app.BaseFragmentActivityApi16.startActivityForResult(BaseFragmentActivityApi16.java:54)
                                                                              at android.support.v4.app.FragmentActivity.startActivityForResult(FragmentActivity.java:67)
                                                                              at android.app.Activity.startActivityForResult(Activity.java:4360)
                                                                              at android.support.v4.app.FragmentActivity.startActivityForResult(FragmentActivity.java:720)
                                                                              at android.app.Activity.startActivity(Activity.java:4699)
                                                                              at android.app.Activity.startActivity(Activity.java:4667)
                                                                              at com.example.asus.retrofittest3.HomeActivity.onCreate(HomeActivity.java:49)
                                                                              at android.app.Activity.performCreate(Activity.java:6864)
                                                                              at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1119)
                                                                              at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2659)
                                                                              at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2767) 
                                                                              at android.app.ActivityThread.-wrap12(ActivityThread.java) 
                                                                              at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1514) 
                                                                              at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                                              at android.os.Looper.loop(Looper.java:163) 
                                                                              at android.app.ActivityThread.main(ActivityThread.java:6205) 
                                                                              at java.lang.reflect.Method.invoke(Native Method) 
                                                                              at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:904) 
                                                                              at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:794

我不太确定出了什么问题,已经尝试了一些stackoverflow上的建议,但还是没能解决它。

3个回答

7

您需要进行设置的操作是:ACTION_APPLICATION_SETTINGSActivity Action:显示设置以允许配置与应用程序相关的设置。),但您没有权限。请将其更改为ACTION_APPLICATION_DETAILS_SETTINGSActivity Action:显示有关特定应用程序的详细信息屏幕。)。

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && ContextCompat.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
    Intent i = new Intent(Settings.ACTION_APPLICATION_SETTINGS, Uri.parse("package:" + getPackageName()));
    finish();
    startActivity(i);
    return;
}

目标:

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && ContextCompat.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
    Intent i = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS, Uri.parse("package:" + getPackageName()));
    finish();
    startActivity(i);
    return;
}

1
嗯,也许可以像这样尝试?
 Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS,
        Uri.fromParts("package", getPackageName(), null));

0

这里的工作方式如下

    fun Activity.appSettings(): Intent {
      return Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS).also {
        val uri = Uri.parse("package:$packageName")
        it.flags = Intent.FLAG_ACTIVITY_NEW_TASK
        it.data = uri
     }
  }

然后通过上下文在任何地方调用它。
 val localContext = LocalContext.current as ComponentActivity

 localContext.appSettings().apply {
    localContext.startActivity(this)
}

作为扩展功能,它完美地运行了


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接