我在更高版本的android

时间:2017-03-22 05:49:46

标签: android android-permissions runtime-permissions

我在更高版本的android中遇到权限检查问题。我正在检查所有权限,但6.0版本仍然崩溃。

这是我的java代码:

  if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.M  &&
               checkSelfPermission(android.Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED)
    {
        requestPermissions(new String[]{android.Manifest.permission.GET_ACCOUNTS}, 0);
        requestPermissions(new String[]{android.Manifest.permission.INTERNET}, 0);
        requestPermissions(new String[]{android.Manifest.permission.ACCESS_NETWORK_STATE}, 0);
        requestPermissions(new String[]{android.Manifest.permission.WRITE_EXTERNAL_STORAGE}, 0);
        requestPermissions(new String[]{android.Manifest.permission.CALL_PHONE}, 0);
        requestPermissions(new String[]{android.Manifest.permission.WAKE_LOCK}, 0);
        requestPermissions(new String[]{android.Manifest.permission.ACCESS_COARSE_LOCATION}, 0);
        requestPermissions(new String[]{android.Manifest.permission.ACCESS_FINE_LOCATION}, 0);
    }
    else
    {
        jump_to_nextActivity();
    }


}

public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults)
{
    //super.onRequestPermissionsResult(requestCode, permissions, grantResults);
    for (int i = 0, len = permissions.length; i < len; i++)
    {
        String permission = permissions[i];
        if (grantResults[i] == PackageManager.PERMISSION_DENIED)
        {
            flag = false;
        }
    }
    if(flag==true)
    {
        Intent i = new Intent(Splash.this,Home.class);
        i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        //i.putExtra("EXIT", true);
        startActivity(i);
        Splash.this.finish();
    }
}

这是我的Android Manifest:

     <permission
    android:name="android.OMMSoftware.Navsahydri.permission.MAPS_RECEIVE"
    android:protectionLevel="signature"/>
<uses-permission android:name="android.OMMSoftware.Navsahydri.permission.MAPS_RECEIVE"/>
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="fractal5.mythirdapp.permission.MAPS_RECEIVE" />

应用程序在较低版本上运行,但在6.0上测试时崩溃,请帮忙。

这是我的Logcat:

致命的例外:主要                                                    处理:android.inclusiveit.growbizz,PID:16919                                                    java.lang.RuntimeException:传递结果ResultInfo {who = @android:requestPermissions:,request = 0,result = -1,data = Intent {act = android.content.pm.action.REQUEST_PERMISSIONS(has extras)}}失败activity {android.inclusiveit.growbizz / android.OMMSoftware.Navsahydri.Splash}:java.lang.NullPointerException:尝试在空对象引用上调用虚方法'boolean java.lang.Boolean.booleanValue()'                                                        在android.app.ActivityThread.deliverResults(ActivityThread.java:3949)                                                        在android.app.ActivityThread.handleSendResult(ActivityThread.java:3992)                                                        在android.app.ActivityThread.-wrap16(ActivityThread.java)                                                        在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1543)                                                        在android.os.Handler.dispatchMessage(Handler.java:111)                                                        在android.os.Looper.loop(Looper.java:207)                                                        在android.app.ActivityThread.main(ActivityThread.java:5776)                                                        at java.lang.reflect.Method.invoke(Native Method)                                                        在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:789)                                                        在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:679)                                                     引发者:java.lang.NullPointerException:尝试在空对象引用上调用虚方法'boolean java.lang.Boolean.booleanValue()'                                                        在android.OMMSoftware.Navsahydri.Splash.onRequestPermissionsResult(Splash.java:54)                                                        在android.app.Activity.dispatchRequestPermissionsResult(Activity.java:6969)                                                        在android.app.Activity.dispatchActivityResult(Activity.java:6848)                                                        在android.app.ActivityThread.deliverResults(ActivityThread.java:3945)                                                        在android.app.ActivityThread.handleSendResult(ActivityThread.java:3992)                                                        在android.app.ActivityThread.-wrap16(ActivityThread.java)                                                        在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1543)                                                        在android.os.Handler.dispatchMessage(Handler.java:111)                                                        在android.os.Looper.loop(Looper.java:207)                                                        在android.app.ActivityThread.main(ActivityThread.java:5776)                                                        at java.lang.reflect.Method.invoke(Native Method)                                                        在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:789)                                                        在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:679)

5 个答案:

答案 0 :(得分:0)

您需要提及分类为&#34; Dangerous&#34;通过Android文档单独在Manifest中。这是一个例子:

<permission
    android:name="android.permission.ACCESS_COARSE_LOCATION"
    android:protectionLevel="dangerous" />

您可以查看正常和危险权限的完整列表here

答案 1 :(得分:0)

在这里,您只检查READ_EXTERNAL_STORAGE权限,请在清单中声明所有危险权限。比如ACCESS_FINE_LOCATION,ACCESS_COARSE_LOCATION

答案 2 :(得分:0)

可能的错误原因是,您尚未向manifest.xml文件声明所有必需的权限。

  

请注意,如果您要请求未声明的权限   manifest.xml,它将始终由系统iteself返回为denied

还有一件事,为什么要一次检查所有权限? 相反,您可以在运行时检查权限,而您的功能需要系统授予权限。

此外,您无需提供android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.M 条件 -
ContextCompat.checkSelfPermission()方法,即使在较低的api版本中也会相应地返回结果。

答案 3 :(得分:0)

错误在您的清单权限上。

<uses-permission android:name="android.OMMSoftware.Navsahydri.permission.MAPS_RECEIVE"/>
<uses-permission android:name="fractal5.mythirdapp.permission.MAPS_RECEIVE" />

由于Android无法识别这些权限,因此请将其删除。

答案 4 :(得分:0)

当Android发布Marshmallow版本时,它引入了Runtime权限作为其功能之一。该功能包括在访问位置,相机,图库,联系人等时获取用户的许可。因此,每次用户尝试访问上述任何一项时,让我们在我们的案例中考虑相机,它会要求获得许可。弹出窗口将显示需要访问相机的权限,无论是允许还是拒绝。如果您拒绝并选中复选框,请勿再次询问,用户将无法在整个应用程序中访问相机。用户需要再次安装弹出窗口。用户可以通过两种方式授予权限:通过设置或弹出窗口。为了使用户更友好,我们在我们的应用中插入了

例如,您正在检查Location的权限,请在mapReady方法

中使用此代码
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M)
        {
            if (MapLayoutActivity.this.checkSelfPermission(Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED)
            {
                MapLayoutActivity.this.requestPermissions(new String[]{Manifest.permission.ACCESS_COARSE_LOCATION}, PERMISSION_STORAGE);
                return;
            }

            if (MapLayoutActivity.this.checkSelfPermission(Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED)
            {
                MapLayoutActivity.this.requestPermissions(new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, PERMISSION_STORAGE);
                return;
            }
        }