如何正确处理ACCESS_FINE_LOCATION的onPermissionResult? [英] How to handle onPermissionResult for ACCESS_FINE_LOCATION properly?

查看:139
本文介绍了如何正确处理ACCESS_FINE_LOCATION的onPermissionResult?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为Google定位服务编写某种 hello world 程序.我已经编码了大多数东西.这是onConnected方法:

I'm writing some sort of hello world program for Google Location Services. I already coded most of the stuff; here's the onConnected method:

public void onConnected(Bundle bundle) {

    // Construct the Location Request
    mLocationRequest = new LocationRequest();
    mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
    mLocationRequest.setInterval(30000);

    // Check if permission granted
    if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {

        // If not, ask for it.
        // On Marshmallow, display permission request dialog
        ActivityCompat.requestPermissions(
                this,
                new String[] {Manifest.permission.ACCESS_FINE_LOCATION},
                LOCATION_REQUEST_NUMBER);
    } else {

        // If already granted, request for location updates
        LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, this);
    }
}

但是当用户在对话框中点击 Allow 时,我有点困惑.

But I'm kinda stumped on what to do after the user tapped Allow on the dialog.

从我能搜集到的东西中,我应该这样做:

From what I could gather, I should do this:

public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults){
    switch (requestCode) {
        case LOCATION_REQUEST_NUMBER: {
            if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                // Permission Granted
                // Great. Now what?

                // I tried the following, but it crashes
                // LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, this);

                // I also tried the following, but it still does nothing.
                mGoogleApiClient.connect();
            } else {
                // Permission Denied.
            }
        }
    }
}

我尝试在Permission Granted上再次调用requestLocationUpdates,但这会发生:

I tried calling requestLocationUpdates again on the Permission Granted, but this happens:

java.lang.RuntimeException: Failure delivering result ResultInfo{who=@android:requestPermissions:, request=1, result=-1, data=Intent { act=android.content.pm.action.REQUEST_PERMISSIONS (has extras) }} to activity {com.example.starleaf1.locationexercise/com.example.starleaf1.locationexercise.MainActivity}: java.lang.NullPointerException: Attempt to read from field 'int com.google.android.gms.location.LocationRequest.b' on a null object reference

添加的详细信息:

我希望应用程序告诉TextView显示当前坐标.我已经这样编码了:

I want the app to tell a TextView to display the current coordinates. Which I've coded like so:

@Override
public void onLocationChanged(Location location) {
    Log.i(LOG_TAG, "Location has changed.");
    output.setText(location.toString());
}

但是,当点击 Allow 按钮时,应用程序只是坐在那里,什么也没做.必须重新启动该应用程序才能使其正常工作.

But when the Allow button is tapped, the app just sits there doing nothing. The app have to be restarted to get it to work.

推荐答案

尝试了一段时间后,我设法使其正常工作.

After experimenting for a while, I managed to get it to work.

请注意,虽然此解决方案在我的实验中有效,但我不确定 我所做的更改是否确实可以使它起作用.

Please note that while this solution works in my experiment, I am not sure if the change I did is indeed what got it to work.

我所做的是在onRequestPermissionResult中将mGoogleApiClient.connect()更改为mGoogleApiClient.reconnect().我也将其设置为Override.

What I did was changing mGoogleApiClient.connect() to mGoogleApiClient.reconnect() in the onRequestPermissionResult. I also set it to Override.

这是该方法最终的外观:

Here's how the method finally looks like:

@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
    switch (requestCode) {
        case LOCATION_REQUEST_NUMBER: {
            if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                if(mGoogleApiClient != null) {
                    mGoogleApiClient.reconnect();
                }
            } else {
                Log.i(LOG_TAG, "Permission denied by user.");
            }
        }
    }

    super.onRequestPermissionsResult(requestCode, permissions, grantResults);
}

这篇关于如何正确处理ACCESS_FINE_LOCATION的onPermissionResult?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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