检查用户是否在提示后启用了GPS

Ali*_*lin 11 gps android location locationmanager

干净利落.我开始一项活动并检查手机是否启用了GPS模块.如果未启用,我会通过对话框提示用户,并询问他是否要手动启用它.在是,我激活位置设置.现在用户可以根据需要启用它,但我需要检查他做了什么.

try {
    isGPSEnabled = locationManager
            .isProviderEnabled(LocationManager.GPS_PROVIDER);
} catch (Exception ex) {}
if (isGPSEnabled) {
    locationManager.requestLocationUpdates(
            LocationManager.GPS_PROVIDER, 0, 0, locationListenerGps);
} else {
    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    builder.setMessage(
            "Your GPS module is disabled. Would you like to enable it ?")
            .setCancelable(false)
            .setPositiveButton("Yes",
                    new DialogInterface.OnClickListener() {

                        public void onClick(DialogInterface dialog,
                                int id) {
                            // Sent user to GPS settings screen
                            final ComponentName toLaunch = new ComponentName(
                                    "com.android.settings",
                                    "com.android.settings.SecuritySettings");
                            final Intent intent = new Intent(
                                    Settings.ACTION_LOCATION_SOURCE_SETTINGS);
                            intent.addCategory(Intent.CATEGORY_LAUNCHER);
                            intent.setComponent(toLaunch);
                            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                            startActivityForResult(intent, 1);
                            dialog.dismiss();
                        }
                    })
            .setNegativeButton("No",
                    new DialogInterface.OnClickListener() {

                        public void onClick(DialogInterface dialog,
                                int id) {
                            dialog.cancel();
                        }
                    });
    AlertDialog alert = builder.create();
    alert.show();
}
Run Code Online (Sandbox Code Playgroud)

我需要知道用户在位置设置中选择了什么,当他回来以便继续我的代码逻辑时.基本上我需要等待用户做出选择并返回我的活动,并再次重新检查gps模块的状态.

Mar*_*son 20

为什么不直接检查LocationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)你的onActivityResult()方法?当用户返回时,应该调用此方法,因为您调用了startActivityForResult().如果用户已启用GPS,则isProviderEnabled()现在应返回不同的结果.

或者,始终在onResume方法中进行GPS检查.如果从位置设置的用户的回报,并没有启用GPS,然后将它们简单,直到GPS接收相同的提示启用.

或者我错过了什么?