Android运行时权限给我带来问题

时间:2017-04-16 20:43:03

标签: android permissions runtime

所以有几天我一直在努力让我的应用程序在启动时询问所有必需的权限。我已将其编码为要求读取和写入外部存储,Internet和Access网络状态。当应用程序打开时,它只询问存储权限,而在settings-> apps->我的应用程序权限中,唯一的存储就是存储。任何帮助将不胜感激。

这是我的代码

package gstankdev.gstank.mobilesvn;

import android.Manifest;
import android.content.Intent;    
import android.content.pm.PackageManager;
import android.net.Uri;
import android.provider.Settings;
import android.support.annotation.NonNull; 
import android.support.design.widget.Snackbar;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;




public class MobileSVN extends AppCompatActivity{
private static final int REQUEST_PERMISSIONS = 10;
public static MobileSVN mobsvn;

protected void onCreate(Bundle savedInstanceState) {
    mobsvn = this;
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_mobile_svn);
    if (ContextCompat.checkSelfPermission(MobileSVN.this, Manifest.permission.READ_EXTERNAL_STORAGE)
            + ContextCompat.checkSelfPermission(MobileSVN.this, Manifest.permission.WRITE_EXTERNAL_STORAGE)
            + ContextCompat.checkSelfPermission(MobileSVN.this, Manifest.permission.INTERNET)
            + ContextCompat.checkSelfPermission(MobileSVN.this, Manifest.permission.ACCESS_NETWORK_STATE)
            != PackageManager.PERMISSION_GRANTED) {
        //permsGranted = false;
        if (ActivityCompat.shouldShowRequestPermissionRationale(MobileSVN.this, Manifest.permission.READ_EXTERNAL_STORAGE) ||
                ActivityCompat.shouldShowRequestPermissionRationale(MobileSVN.this, Manifest.permission.WRITE_EXTERNAL_STORAGE) ||
                ActivityCompat.shouldShowRequestPermissionRationale(MobileSVN.this, Manifest.permission.INTERNET) ||
                ActivityCompat.shouldShowRequestPermissionRationale(MobileSVN.this, Manifest.permission.ACCESS_NETWORK_STATE)) {
            Snackbar.make(findViewById(android.R.id.content), getString(R.string.app_name),
                    Snackbar.LENGTH_LONG).setAction((R.string.enable),
                    new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            ActivityCompat.requestPermissions(MobileSVN.this,
                                    new String[]{
                                            Manifest.permission.INTERNET,
                                            Manifest.permission.READ_EXTERNAL_STORAGE,
                                            Manifest.permission.WRITE_EXTERNAL_STORAGE,
                                            Manifest.permission.ACCESS_NETWORK_STATE},
                                    REQUEST_PERMISSIONS);
                        }
                    }).show();
        } else {
            ActivityCompat.requestPermissions(MobileSVN.this,
                    new String[]{
                            Manifest.permission.READ_EXTERNAL_STORAGE,
                            Manifest.permission.WRITE_EXTERNAL_STORAGE,
                            Manifest.permission.INTERNET,
                            Manifest.permission.ACCESS_NETWORK_STATE},
                    REQUEST_PERMISSIONS);
        }
    }

}
@Override
public void onRequestPermissionsResult(int requestCode,
                                       @NonNull String permissions[],
                                       @NonNull int[] grantResults) {
    switch (requestCode) {
        case REQUEST_PERMISSIONS: {
            if ((grantResults.length > 0) && (grantResults[0] +
                    grantResults[1]) == PackageManager.PERMISSION_GRANTED) {
                //do nothing
            } else {
                Snackbar.make(findViewById(android.R.id.content), R.string.app_name,
                        Snackbar.LENGTH_LONG).setAction(R.string.enable,
                        new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                Intent intent = new Intent();
                                intent.setAction(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
                                intent.addCategory(Intent.CATEGORY_DEFAULT);
                                intent.setData(Uri.parse("package:" + getPackageName()));
                                intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                                intent.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
                                intent.addFlags(Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
                                startActivity(intent);
                            }
                        }).show();
            }
        }
    }
}





public Button begin;

public void init(){
    begin = (Button)findViewById(R.id.begin);
    begin.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            startActivity(new Intent(MobileSVN.this, svnsetup.class));
        }
    });
        }








@Override
protected void onPostCreate(Bundle savedInstanceState) {
    super.onPostCreate(savedInstanceState);
    init();

}}

2 个答案:

答案 0 :(得分:1)

Android权限分为正常和危险权限。我们(开发人员)只需要在运行时检查并询问危险权限,总是从清单中授予正常权限。

您可以在此处查看危险权限的完整列表:https://developer.android.com/guide/topics/permissions/requesting.html#normal-dangerous

答案 1 :(得分:1)

  

当应用程序打开时,它只询问存储权限

因为另外两个许可:

Manifest.permission.INTERNET
Manifest.permission.ACCESS_NETWORK_STATE

被归类为PROTECTION_NORMAL。所以对于api 23,你不需要运行时权限。

选中此项以查看permissions

的列表

您只需要对分类为Dangerous permissions

的用户请求权限

像:

GET_ACCOUNTS
ACCESS_FINE_LOCATION
ACCESS_COARSE_LOCATION
READ_EXTERNAL_STORAGE
WRITE_EXTERNAL_STORAGE