检查是否在Android中启用了GPS

use*_*672 8 java eclipse gps android

我正在开发一个包含谷歌地图的项目.当活动加载时,我想检查GPS是否启用.所以我使用以下代码重定向到包含设置的页面.

if(!manager.isProviderEnabled( LocationManager.GPS_PROVIDER ))
        {
             AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this);
                alertDialogBuilder.setMessage("GPS is disabled in your device. Would you like to enable it?")
                .setCancelable(false)
                .setPositiveButton("Goto Settings Page To Enable GPS",
                        new DialogInterface.OnClickListener(){
                    public void onClick(DialogInterface dialog, int id){
                        Intent callGPSSettingIntent = new Intent(
                                android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS);
                        startActivity(callGPSSettingIntent);
                    }
                });
                alertDialogBuilder.setNegativeButton("Cancel",
                        new DialogInterface.OnClickListener(){
                    public void onClick(DialogInterface dialog, int id){
                        dialog.cancel();
                    }
                });
                AlertDialog alert = alertDialogBuilder.create();
                alert.show();
        }
Run Code Online (Sandbox Code Playgroud)

但问题是,如果我启用GPS并返回应用程序,我希望页面再次加载,以便加载地图.如何确保我的活动再次运行?

Mar*_*ank 0

您必须重写这些方法onResume

如果您了解活动生命周期,您会发现onPause当您激活 GPS 时您的活动已暂停 ( ),并且onResume当您返回活动时将被调用。