如何在下面的代码中添加Nougat Permission?

时间:2017-12-09 12:39:50

标签: java android android-permissions

此代码在所有版本的7.0以下版本的android上运行,但不会在Nougat上运行。当我按下按钮时,它不会进行下一个活动?请告诉我们如何在下面的代码中添加Nougat权限?

currentlocation.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
        currentlocation.startAnimation(animTranslate);
        if (Build.VERSION.SDK_INT > Build.VERSION_CODES.M) {
            if (ActivityCompat.checkSelfPermission(getApplicationContext(), android.Manifest.permission.READ_EXTERNAL_STORAGE)
                    != PackageManager.PERMISSION_GRANTED) {
                ActivityCompat.requestPermissions(Front.this, new String[]{android.Manifest.permission.READ_EXTERNAL_STORAGE}, REQUEST_PERMISSON_SORAGE);

            }
        } else {
            if (interstitialAd.isLoaded()) {
                interstitialAd.show();
            }
            Intent iu = new Intent(Front.this, MainActivity.class);
            startActivity(iu);
        }
    }
});
@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
    super.onRequestPermissionsResult(requestCode, permissions, grantResults);
    if (requestCode == REQUEST_PERMISSON_SORAGE
            && grantResults.length > 0
            && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
        Intent i = new Intent(Front.this, MainActivity.class);
        startActivity(i);
        //finish();
    } else {
        Toast.makeText(this, "Please Allow permision to use App .", Toast.LENGTH_SHORT).show();
        finish();
    }
}

这是我的清单显示添加的权限:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> 
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /> 
<uses-permission android:name="android.permission.INTERNET" /> 
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> 
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" /> 
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> 
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />

1 个答案:

答案 0 :(得分:0)

首先,权限系统从API 23及更高版本开始,因此代码应为:

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {

其次,您没有请求更多危险权限:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> 
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /> ---> You did request this
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" /> 
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> 

您只需READ_EXTERNAL_STORAGE。请求其他人。像使用READ_EXTERNAL_STORAGE

一样将它们添加到String数组中