检查GPS是否在android中启用



我正在做一个包含谷歌地图的项目。当活动加载时,我想检查GPS是否启用。因此,我使用以下代码重定向到包含settings的页面。

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();
        }

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

您必须重写onResume方法。

如果你知道活动的生命周期,你看到你的活动是暂停(onPause)当你去激活你的GPS,和onResume将被调用当你返回到你的活动

把android:noHistory="true"放到当前类的manifest文件中。它不会留下任何痕迹。清理堆栈

最新更新