在运行时请求权限,Android M +

时间:2017-03-05 13:10:12

标签: android permissions runtime-permissions

首先,我知道这是一个重复的问题,但我已经完成了其他类似问题的答案,并且无法通过任何这些解决方案找到成功。

我开发了一款适用于我的测试设备的应用程序,这是一款运行Android L(5.0.1)的三星S4,但我希望这个应用程序也适用于较新版本的Android。

我了解Android M +的请求权限已更改,因此必须在运行时询问,但是当我尝试实现此权限时,对话框永远不会出现,因此永远不会要求所需的权限。

我请求的权限是ACCESS_FINE_LOCATION

以下是我的清单文件中的相关代码:

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

如果尚未批准我要求许可的地方:

if (ActivityCompat.checkSelfPermission(MapsActivity.this, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) {

    startGeofenceMonitoring();

} else {
    askForPermission(android.Manifest.permission.ACCESS_FINE_LOCATION,LOCATION);
}

askForPermission函数(稍微修改了Google的例子):

private void askForPermission(String permission, Integer requestCode) {
    if (ContextCompat.checkSelfPermission(MapsActivity.this, permission) != PackageManager.PERMISSION_GRANTED) {

        // Should we show an explanation?
        if (ActivityCompat.shouldShowRequestPermissionRationale(MapsActivity.this, permission)) {

            //This is called if user has denied the permission before
            //In this case I am just asking the permission again
            ActivityCompat.requestPermissions(MapsActivity.this, new String[]{permission}, requestCode);

        } else {

            Log.d(TAG, "askForPermission: " + permission);

            ActivityCompat.requestPermissions(MapsActivity.this, new String[]{permission}, requestCode);
        }
    } else {
        Toast.makeText(this, "" + permission + " is already granted.", Toast.LENGTH_SHORT).show();
    }
}

onRequestPermissionsResult函数:

@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
    super.onRequestPermissionsResult(requestCode, permissions, grantResults);

    Log.d(TAG, "onRequestPermissionsResult: " + requestCode);

        switch (requestCode) {
            case 1:
                if (grantResults.length > 0
                        && grantResults[0] == PackageManager.PERMISSION_GRANTED) {

                    Log.d(TAG, "onRequestPermissionsResult: request");

                    Toast.makeText(this, "Permission granted", Toast.LENGTH_SHORT).show();

                    startGeofenceMonitoring();
                }
                break;
        }
}

正如我所说,询问权限的对话框永远不会出现,但我的控制台告诉我ActivityCompat.requestPermissions(MapsActivity.this, new String[]{permission}, requestCode);被调用,经过大量研究后我仍然不确定为什么它不是'工作。我认为唯一可能的原因是此活动是显示Google地图地图的片段活动。我确实试图在另一个活动中请求权限,但完全相同的事情发生。

感谢。

修改

所以我刚刚在一个全新的项目中测试了我发布的代码,它运行得很好并显示了对话框。我现在真的很困惑是什么阻止了这个对话框在我当前的项目中显示。

3 个答案:

答案 0 :(得分:1)

final private int REQUEST_CODE_ASK_PERMISSIONS = 123;
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
private void requestPermission() {
    if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED ) {
        ActivityCompat
                .requestPermissions(MainActivity.this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, REQUEST_CODE_ASK_PERMISSIONS);
    }
}
@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
    switch (requestCode) {
        case REQUEST_CODE_ASK_PERMISSIONS:
            if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                // Permission Granted
                Toast.makeText(MainActivity.this, "Permission Granted", Toast.LENGTH_SHORT)
                        .show();
            } else {
                // Permission Denied
                Toast.makeText(MainActivity.this, "Permission Denied", Toast.LENGTH_SHORT)
                        .show();
            }
            break;
        default:
            super.onRequestPermissionsResult(requestCode, permissions, grantResults);
    }
}

答案 1 :(得分:0)

我设法解决了它!

在我的应用程序中,我使用了不同的活动来控制选项卡以及向用户显示哪些活动。这是第一个要显示的活动,我认为它是整个计划中显示的唯一活动,但只显示其他活动的内容。因此,当我请求MapsActivity的权限时,它没有显示,因为MapsActivity不是实际存在的那个。

我的解释可能有误,所以这里是我的TabBarActivity的代码:

    TabHost tabHost = getTabHost();
    TabHost.TabSpec spec;
    Intent intent;

    intent = new Intent().setClass(this, SecondActivity.class);
    spec = tabHost.newTabSpec("Second").setIndicator("Profile")
            .setContent(intent);
    tabHost.addTab(spec);

    intent = new Intent().setClass(this, MapsActivity.class);
    spec = tabHost.newTabSpec("First").setIndicator("Location")
            .setContent(intent);
    tabHost.addTab(spec);

    intent = new Intent().setClass(this, ThirdActivity.class);
    spec = tabHost.newTabSpec("Third").setIndicator("Settings")
            .setContent(intent);
    tabHost.addTab(spec);

答案 2 :(得分:0)

以下代码对我有用。

public static final int MY_PERMISSION_REQUEST_READ_EXTERNAL_STORAGE=1;

 public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
    View view=inflater.inflate(R.layout.activity_tracks,container,false);

    getPermission();
        ...
          }

 public void getPermission(){
        if(ContextCompat.checkSelfPermission(getActivity(),Manifest.permission.READ_EXTERNAL_STORAGE)!=PackageManager.PERMISSION_GRANTED){
            if(ActivityCompat.shouldShowRequestPermissionRationale(getActivity(),Manifest.permission.READ_EXTERNAL_STORAGE)){

            }else{
                ActivityCompat.requestPermissions(getActivity(),new String[] {Manifest.permission.READ_EXTERNAL_STORAGE},MY_PERMISSION_REQUEST_READ_EXTERNAL_STORAGE);
            }
        }else{
            getSongDetails();
        }
    }


 @Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
    switch (requestCode){
        case -1:
            //hello
            break;
        case MY_PERMISSION_REQUEST_READ_EXTERNAL_STORAGE:
            if(grantResults.length>0 && grantResults[0]==PackageManager.PERMISSION_GRANTED){
                getPermission();
            }else{
                Toast.makeText(getActivity(),"You've denied permission",Toast.LENGTH_LONG).show();
            }
            return;
        default:
            super.onRequestPermissionsResult(requestCode, permissions, grantResults);
    }

}