Проверить, активировал ли пользователь GPS после запроса

Просто и понятно. Я запускаю Activity и проверяю, включен ли в телефоне модуль 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();
}

Мне нужно знать, что пользователь выбрал в настройках местоположения, когда он вернется, чтобы продолжить мою логику в коде. В основном мне нужно подождать, пока пользователь сделает выбор и вернется к моей деятельности, а также снова перепроверить статус модуля gps.

11
задан Lee Taylor 11 July 2013 в 01:29
поделиться