Android如何在按下返回按钮到主屏幕后关闭gps图标?

时间:2014-02-17 08:20:34

标签: android gps

我有这个问题,我在我的应用程序中使用GPS来获取经度和纬度,当我想要返回主页时图标GPS始终从我的移动设备顶部显示,我如何在返回主屏幕后关闭它< / p>

enter image description here 我的代码。

public class doctorlocation extends Activity implements LocationListener {
    protected LocationManager locationManager;
    protected LocationListener locationListener;
    protected Context context;
    String lat;
    String provider;
    protected double latitude,longitude; 
    protected boolean gps_enabled,network_enabled;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.doctorlocation);
        locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
        locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this);   
    }

        @Override
    public void onLocationChanged(Location location) {
     //txtLat = (TextView) findViewById(R.id.textview1);

    //txtLat.setText("Latitude:" + location.getLatitude() + ", Longitude:" + location.getLongitude());
    String str = "Latitude: "+location.getLatitude()+" \nLongitude: "+location.getLongitude();
    Toast.makeText(getBaseContext(), str, Toast.LENGTH_LONG).show();

    latitude=location.getLatitude();

    longitude=location.getLongitude();

    }

    @Override
    public void onProviderDisabled(String provider) {
     Log.d("Latitude","disable");

        Toast.makeText(getBaseContext(), "Gps turned off ", Toast.LENGTH_LONG).show();

    }

    @Override
    public void onProviderEnabled(String provider) {
     Log.d("Latitude","enable");

    Toast.makeText(getBaseContext(), "Gps turned on ", Toast.LENGTH_LONG).show();

    }

    @Override
    public void onStatusChanged(String provider, int status, Bundle extras) {
    Log.d("Latitude","status");
    }

      }

4 个答案:

答案 0 :(得分:3)

onPause()的{​​{1}}上使用此代码。

Activity

答案 1 :(得分:1)

启用和禁用GPS取决于用户。您可以向他显示一个对话框,通知他关于禁用GPS的信息。在这种情况下,在对话框上保留两个按钮 - 一个用于“设置”,另一个用于“确定”或“取消”。

public static void promptForGPS(
        final Activity activity)
    {
        final AlertDialog.Builder builder =
            new AlertDialog.Builder(activity);
        final String action = Settings.ACTION_LOCATION_SOURCE_SETTINGS;
        final String message = "Disable GPS....Message here"
            + " Click OK to go to"
            + " location services settings to let you do so.";

        builder.setMessage(message)
            .setPositiveButton("OK",
                new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface d, int id) {
                        activity.startActivity(new Intent(action));
                        d.dismiss();
                    }
            })
            .setNegativeButton("Cancel",
                new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface d, int id) {
                        d.cancel();
                    }
            });
        builder.create().show();
    }    

答案 2 :(得分:0)

我找到的最好的是

onResume()
{
     // Request for GPS Location
     //location.requestLocationUpdates()
}

onPause()
{
     // Remove location
     // location.removeUpdates()
}

通过调用removeUpdates()方法,只要您的Activity不在forground中,它就会停止GPS。通过这种方式,您可以停止GPS以及停止耗尽电池。

答案 3 :(得分:0)

当您的应用进入后台时,您需要告诉LocationManager您不再需要位置更新

locationManager.removeUpdates(this);

在你的onPause()方法中。

你也应该将你的电话转移到

locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this);   

到onResume();这样一旦您重新进入应用程序,位置更新将恢复。