ActivityCompat.requestPermissions不显示提示

时间:2016-05-10 13:50:15

标签: java android google-maps android-fragments android-permissions

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

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

我的Google地图代码(实施OnMapReadyCallbackActivityCompat.OnRequestPermissionsResultCallback())位于FragmentActivity

我设法让requestPermissions()功能在应用中的其他活动中成功运行,它只是带有Google地图的功能。放置在onCreate()的{​​{1}}方法或Activity(需要去的地方)时,它不起作用。

onMapReady()

有什么想法吗?这与我的Activity类(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"); } )有关,还是可能是异步调用权限请求的Google地图?

4 个答案:

答案 0 :(得分:13)

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

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

App requests aren't showing up

相同的问题

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

有点破解,但工作正常!

答案 1 :(得分:0)

检查您是否已经在Android的清单文件中添加了所请求的权限,就像Android M之前一样,只有这样您才能获得预期的行为。

将权限添加到您的清单,以便您可以通过该请求进行申请     ActivityCompat.requestPermissions:

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

答案 2 :(得分:0)

我将分享适合我的代码。在我想要查看提示的受保护的void onCreate(Bundle savedInstanceState){}方法中,我包含了这段代码:

    /* Check whether the app has the ACCESS_FINE_LOCATION permission and whether the app op that corresponds to
     * this permission is allowed. The return value is an int: The permission check result which is either
     * PERMISSION_GRANTED or PERMISSION_DENIED or PERMISSION_DENIED_APP_OP.
     * Source: https://developer.android.com/reference/android/support/v4/content/PermissionChecker.html
     * While testing, the return value is -1 when the "Your location" permission for the App is OFF, and 1 when it is ON.
     */
    int permissionCheck = ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION);
    // The "Your location" permission for the App is OFF.
    if (permissionCheck == -1){
        /* This message will appear: "Allow [Name of my App] to access this device's location?"
         * "[Name of my Activity]._instance" is the activity.
         */
        ActivityCompat.requestPermissions([Name of my Activity]._instance, new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, REQUEST_CODE_ACCESS_FINE_LOCATION);
    }else{
        // The "Your location" permission for the App is ON.
        if (permissionCheck == 0){
        }
    }

在protected void onCreate(Bundle savedInstanceState){}方法之前,我创建了以下常量和方法:

public static final int REQUEST_CODE_ACCESS_FINE_LOCATION = 1; // For implementation of permission requests for Android 6.0 with API Level 23.

// Code from "Handle the permissions request response" at https://developer.android.com/training/permissions/requesting.html.
@Override
public void onRequestPermissionsResult(int requestCode,
                                       String permissions[], int[] grantResults) {
    switch (requestCode) {
        case REQUEST_CODE_ACCESS_FINE_LOCATION: {
            // If request is cancelled, the result arrays are empty.
            if (grantResults.length > 0
                    && grantResults[0] == PackageManager.PERMISSION_GRANTED) {

                // permission was granted, yay! Do the
                // location-related task you need to do.                

            } else {

                // permission denied, boo! Disable the
                // functionality that depends on this permission.
            }
            return;
        }

        // other 'case' lines to check for other
        // permissions this app might request
    }
}

答案 3 :(得分: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;
    }
}

在您的主要活动中:

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回调和请求代码;)!它只适用于主要活动。