ActivityCompat.requestPermissions未显示提示

8

我试图请求 ACCESS_FINE_LOCATION 权限,以获取用户的当前位置。

我的日志表明,在查询 ContextCompat.checkSelfPermission() 时,我的应用目前没有此权限,但在调用 ActivityCompat.requestPermissions() 时没有显示任何内容。

我的 Google 地图代码(实现了 OnMapReadyCallbackActivityCompat.OnRequestPermissionsResultCallback())在一个 FragmentActivity 中。

我已经成功地将 requestPermissions() 函数运行到了应用中的其他 Activity 中,只是无法在包含 Google 地图的 Activity 中工作。它不起作用当放置在 ActivityonCreate() 方法或 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");
            // 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");
}

任何想法?这与我的 Activity 类 (FragmentActivity) 有关,还是可能是 Google 地图异步调用权限请求的问题吗?

我很惊讶这个可以编译通过。checkSelfPermission() 使用了 LocationActivity.this。第二个 requestPermissions() 使用了 PermissionsRequestActivity.this - CommonsWare
不好意思,是的,我必须稍微修改一下我的代码才能发帖。第二个应该是LocationActivity.this。 - Robin
我曾尝试将所有requestPermissions放在单独的Activity中,但这使得回调变得困难且不太优雅。 - Robin
值得一提的是,这个示例应用程序使用运行时权限没有问题。 - CommonsWare
你不应该在清单文件中指定finelocation吗?或者在过去的一年里安卓已经彻底发生了改变? - danny117
@CommonsWare 我可以确认你的示例在AbstractMapActivity扩展到FragmentActivity时也有效,所以我想这与地图无关,或者说... - Robin
3个回答

14

我完全删除了我的类,但仍然无法正常工作,后来我意识到这个Activity是使用TabHost实例化的。

当我停止使用TabHost时,提示成功显示。 我猜测新的权限提示不支持TabHost - 这是一个bug吗?

App requests aren't showing up一样的问题

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

有点hack但可以正常工作!


Ditto - 使用 TabHost,但它对我也没有显示。有人知道如何处理吗? - AlexVPerl
我最终创建了一个PermissionsRequestActivity,代表我的TabHost处理权限请求和响应,然后退出(通过Intent extras Bundle传递请求的权限信息)。它将请求的响应作为广播传回,由我的TabHost接收。有点像黑客技巧,但还算可以! - Robin
澄清一下,TabActivity本身可以很好地处理权限请求,但是作为选项卡托管的任何活动都不能。因此,我通过将消息发布到主活动,然后再返回到片段来解决这个问题。 - Greg Ennis

0

我在一个使用TabHost的项目中遇到了同样的问题。 基于@Robin的解决方案,我使用EventBus库将消息从子活动发送到TabActity。

EventBus: https://github.com/greenrobot/EventBus

创建事件对象:

public class MessageEvent {
    private String message;
    public MessageEvent(String message){
        this.message = message;
    }

    public String getMessage(){
        return this.message;
    }
}

在你的主 Activity 中:
private EventBus eventBus = EventBus.getDefault();
@Override
protected void onCreate(Bundle savedInstanceState) {
    eventBus.register(this);
}
@Override
protected void onDestroy() {
    eventBus.unregister(this);
    super.onDestroy();
}
@Subscribe(threadMode = ThreadMode.MAIN)
public void onMessageEvent(MessageEvent event) {
    if (event.getMessage().equals("contacts")){
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && checkSelfPermission(android.Manifest.permission.WRITE_CONTACTS) != PackageManager.PERMISSION_GRANTED) {
            ActivityCompat.requestPermissions(MainPage.this,new String[]{android.Manifest.permission.WRITE_CONTACTS}, 100 );
        }
    }
};

为您想要请求的权限设置不同的消息。 在您的子活动中,您可以发布适当的消息:

EventBus.getDefault().post(new MessageEvent("contacts"));

注意onRequestPermissionsResult回调和请求代码;)!它只能在主活动中工作。


0
请检查您是否已在Android的清单文件中添加了所请求的权限,就像Android M之前一样,只有这样才能获得预期的行为。
将权限添加到清单中,以便您可以通过ActivityCompat.requestPermissions请求它:
<uses-permission android:name="android.permission. ACCESS_FINE_LOCATION" />

谢谢,我已经解决了;在应用程序的其他地方,权限请求已经成功显示。我找到了原因,请参见我的上面的答案。 - Robin

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接