【问题标题】:Checking if GPS is enabled in android检查是否在android中启用了GPS
【发布时间】:2014-09-10 07:47:20
【问题描述】:

我正在开发一个包含谷歌地图的项目。当活动加载时,我想检查 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();
        }

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

【问题讨论】:

  • 你应该自动启用gps然后开始活动
  • 不询问用户就启用是不谨慎的

标签: java android eclipse gps


【解决方案1】:

您必须重写方法onResume

如果您知道 Activity 生命周期,您会看到当您去激活 GPS 时您的 Activity 已暂停 (onPause),当您返回 Activity 时会调用 onResume

【讨论】:

    【解决方案2】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-08-27
      • 2020-09-26
      • 1970-01-01
      • 1970-01-01
      • 2011-06-18
      • 2011-09-29
      • 1970-01-01
      相关资源
      最近更新 更多