Android:当设备位置已经启用时,FusedLocationProviderClient不会调用onLocationResult()方法。
创始人
2024-10-13 11:00:39
0

要解决这个问题,可以通过以下代码示例来判断设备位置是否已启用,并在启用时调用onLocationResult()方法:

public class MainActivity extends AppCompatActivity {

    private FusedLocationProviderClient fusedLocationProviderClient;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        fusedLocationProviderClient = LocationServices.getFusedLocationProviderClient(this);

        checkLocationSettings();
    }

    private void checkLocationSettings() {
        LocationSettingsRequest.Builder builder = new LocationSettingsRequest.Builder()
                .addLocationRequest(createLocationRequest());

        SettingsClient settingsClient = LocationServices.getSettingsClient(this);
        Task task = settingsClient.checkLocationSettings(builder.build());

        task.addOnSuccessListener(this, new OnSuccessListener() {
            @Override
            public void onSuccess(LocationSettingsResponse locationSettingsResponse) {
                // 设备位置已启用,可以开始获取位置信息
                startLocationUpdates();
            }
        });

        task.addOnFailureListener(this, new OnFailureListener() {
            @Override
            public void onFailure(@NonNull Exception e) {
                if (e instanceof ResolvableApiException) {
                    try {
                        // 设备位置未启用,显示对话框提示用户打开位置设置
                        ResolvableApiException resolvable = (ResolvableApiException) e;
                        resolvable.startResolutionForResult(MainActivity.this, REQUEST_CHECK_SETTINGS);
                    } catch (IntentSender.SendIntentException sendEx) {
                        // Ignore the error
                    }
                }
            }
        });
    }

    private void startLocationUpdates() {
        fusedLocationProviderClient.requestLocationUpdates(createLocationRequest(), new LocationCallback() {
            @Override
            public void onLocationResult(LocationResult locationResult) {
                // 在此处理位置更新
            }
        }, null);
    }

    private LocationRequest createLocationRequest() {
        LocationRequest locationRequest = new LocationRequest();
        locationRequest.setInterval(10000);
        locationRequest.setFastestInterval(5000);
        locationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
        return locationRequest;
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == REQUEST_CHECK_SETTINGS) {
            if (resultCode == RESULT_OK) {
                // 用户已打开位置设置,可以开始获取位置信息
                startLocationUpdates();
            }
        }
    }
}

在上面的代码中,首先通过checkLocationSettings()方法来检查设备位置设置。如果设备位置已启用,则直接调用startLocationUpdates()方法开始获取位置信息。如果设备位置未启用,则通过ResolvableApiException启动一个对话框,提示用户打开位置设置。用户在打开位置设置后,会触发onActivityResult()方法,然后再次调用startLocationUpdates()方法开始获取位置信息。

请注意,在startLocationUpdates()方法中,使用requestLocationUpdates()方法注册了一个LocationCallback对象,该对象中的onLocationResult()方法将在位置更新时被调用,您可以在该方法中处理位置信息。

相关内容

热门资讯

一秒答解!wepoker辅助器... 一秒答解!wepoker辅助器如何使用,hhpoker有后台操控吗,教你教程(有挂辅助)是一款可以让...
今日头条!wepoker怎么挂... 今日头条!wepoker怎么挂飞机,竞技联盟辅助,普及教程(有挂技巧);亲真的是有正版授权,小编(透...
玩家必备科普!wejoker内... 玩家必备科普!wejoker内置辅助,wepoker透视最简单三个步骤,攻略教程(有挂教程);支持2...
总算了解!德扑圈透视,德普之星... 总算了解!德扑圈透视,德普之星透视辅助软件激活码,揭秘教程(有挂攻略);支持2-10人实时对战,虚拟...
盘点几款!德州圈脚本,hhpo... 盘点几款!德州圈脚本,hhpoker德州机器人,详细教程(有挂透明)关于hhpoker德州机器人机制...
今日焦点!hhpoker有没有... 今日焦点!hhpoker有没有作弊挂,pokerworld破解版下载,玩家教你(有挂方法)是由北京得...
揭秘!wpk透视插件,德普之星... 揭秘!wpk透视插件,德普之星透视辅助,2025新版技巧(有挂透明);玩家必备必赢加哟《136704...
玩家必备攻略!hhpoker辅... 玩家必备攻略!hhpoker辅助器视频,wpk插件辅助,wepoke教程(有挂攻略)科技教程也叫必备...
玩家实测!aapoker ai... 玩家实测!aapoker ai插件,poker红龙辅助,专业教程(有挂教程);最新版2024是一款经...
玩家必看教程!wepoker软... 1、玩家必看教程!wepoker软件安装包,epoker免费透视脚本,解密教程(有挂神器)2、进入游...