ActivityCompat.requestPermissions不显示提示 [英] ActivityCompat.requestPermissions does not show prompt

查看:5401
本文介绍了ActivityCompat.requestPermissions不显示提示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试申请 ACCESS_FINE_LOCATION 权限以获取用户的当前位置。



我的日志记录表示当查询 ContextCompat.checkSelfPermission()时,我的应用程序当前没有此权限,但是当调用 ActivityCompat.requestPermissions() OnMapReadyCallback ActivityCompat。 OnRequestPermissionsResultCallback())在 FragmentActivity 中。



我设法得到 requestPermissions()函数可以在应用中的其他活动中成功运行,它只是Google地图中的一个。它放置在 Activity onCreate()方法中或 onMapReady()(它需要去的地方)。

  if(ContextCompat.checkSelfPermission(LocationActivity .this,android.Manifest.permission.ACCESS_FINE_LOCATION)!= PackageManager.PERMISSION_GRANTED){
Log.d(TAG,not granted);
final String [] permissions = new String [] {android.Manifest.permission.ACCESS_FINE_LOCATION};
if(ActivityCompat.shouldShowRequestPermissionRationale(this,android.Manifest.permission.ACCESS_FINE_LOCATION)){
Log.d(TAG,rationale);
//向用户解释为什么需要权限,然后再次请求
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage(我们需要权限)
.setCancelable(false)
.setPositiveButton(OK,new DialogInterface.OnClickListener(){$ b $ public void onClick(DialogInterface对话框,int id){
ActivityCompat.requestPermissions(LocationActivity.this,permissions,1);
}
});
AlertDialog alert = builder.create();
alert.show();

} else {
Log.d(TAG,request+ android.Manifest.permission.ACCESS_FINE_LOCATION);
//如果权限未被拒绝,请求权限
ActivityCompat.requestPermissions(LocationActivity.this,permissions,1);
}
} else {
Log.d(TAG,granted);
}

有什么想法?是否与我的Activity的类( FragmentActivity )有关,或者Google地图可能异步调用权限请求?

解决方案

在完全剥离我的类之后,它仍然无法工作,我意识到这个Activity正在使用TabHost实例化。

当我停止使用TabHost时,提示已成功显示。我想TabHosts不受新的权限提示支持 - 这是一个错误吗?



应用程序请求未显示



我最终创建了一个处理权限请求的PermissionsRequestActivity和代表我的TabHost的响应,然后退出(通过Intent extras Bundle传递所请求的权限信息)。
它将响应作为广播返回,这是由我的TabHost拾取的。

一个黑客的行为,但行得通!


I'm attempting to request ACCESS_FINE_LOCATION permissions in order to get the user's current location.

My logging indicates that my app does not currently have this permission when querying ContextCompat.checkSelfPermission(), but when calling ActivityCompat.requestPermissions() nothing is displayed.

My Google map code (implementing OnMapReadyCallback and ActivityCompat.OnRequestPermissionsResultCallback()) is in a FragmentActivity.

I have managed to get the requestPermissions() function working successfully in other Activities in the app, it's just the one with the Google map. It doesn't work when placed in the onCreate() method of the Activity, or in onMapReady() (where it needs to go).

if(ContextCompat.checkSelfPermission(LocationActivity.this, android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
        Log.d(TAG, "not granted");
        final String[] permissions = new String[] {android.Manifest.permission.ACCESS_FINE_LOCATION};
    if(ActivityCompat.shouldShowRequestPermissionRationale(this, android.Manifest.permission.ACCESS_FINE_LOCATION)) {
            Log.d(TAG, "rationale");
            // Explain to the user why permission is required, then request again
            AlertDialog.Builder builder = new AlertDialog.Builder(this);
            builder.setMessage("We need permissions")
                    .setCancelable(false)
                    .setPositiveButton("OK", new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int id) {
                            ActivityCompat.requestPermissions(LocationActivity.this, permissions, 1);
                    }
                });
        AlertDialog alert = builder.create();
        alert.show();

    } else {
        Log.d(TAG, "request" + android.Manifest.permission.ACCESS_FINE_LOCATION);
        // If permission has not been denied before, request the permission
        ActivityCompat.requestPermissions(LocationActivity.this, permissions, 1);
    }
} else {
    Log.d(TAG, "granted");
}

Any ideas? Is it something to do with my Activity's class (FragmentActivity), or possible the Google map calling the permissions request asynchronously?

解决方案

After stripping out my class completely, and it still not working, I realised that this Activity is being instantiated using a TabHost.

When I stop using the TabHost, the prompt is displayed successfully. I guess TabHosts are not supported by the new permissions prompts - is this a bug?

Same problem as App requests aren't showing up

I ended up creating a PermissionsRequestActivity which handles the permission request and response on behalf of my TabHost, then exits (pass the requested permission information in through the Intent extras Bundle).
It passes back the response to the request as a Broadcast, which is picked up by my TabHost.

Bit of a hack but works OK!

这篇关于ActivityCompat.requestPermissions不显示提示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆