安卓系统:如何停用GPS



我正在开发一款android应用程序,我对android开发还很陌生。目前,我有问题找到一种方法来停用设备上的GPS。

我想做的是当用户点击按钮时停用GPS。据我所知,唯一的方法是使用LocationManager中的removeUpdates方法进行购买。但我的问题是,这个方法等待LocationListiner作为参数。但我不知道如何获取LocationListiner:/

这是我的代码:

以下代码是onCreate of my MainActivity 的一部分

 Button save = (Button) findViewById(R.id.save);
    save.setOnClickListener(new View.OnClickListener() {
        public void onClick(View view) {
            // Startet die Abfrage der GPS Position
            final LocationManager mLocMan = GetMyLocation();
            final AlertDialog alertDialog = new AlertDialog.Builder(MainActivity.this).create(); //Read Update
            alertDialog.setCanceledOnTouchOutside(false);
            alertDialog.setTitle("GPS Signal");
            alertDialog.setMessage("Some message");
            alertDialog.setButton("Cancel", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int which) {
                    dialog.cancel();
                    mLocMan.removeUpdates();
                }
            });
            alertDialog.show(); 
        }
    });
private LocationManager GetMyLocation(){
    LocationManager locationManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
    GetLocation getMyLocation = new GetLocation();
    locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, getMyLocation);
    return locationManager;
}

也许有人给了我一个提示。目前,我在android系统上仍然有一些问题:/

您需要在某个地方实现LocationListener,以便能够通过onLocationChanged(Location Location)接收更新。

然后,你只需要使用locationManager.removeUpdates(MyLocationListener);

您可以使用该活动来实现侦听器,然后locationManager.removeUpdates(此);

public class MyActivity extends Activity{
...
LocationManager manager;
GetLocation listener;
@Override
    protected void onCreate() {
manager = (LocationManager)context.getSystemService(Context.LOCATION_SERVICE);
listener = new GetLocation();
@Override
    protected void onResume() {
        manager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, listener);
    }
    @Override
    protected void onPause() {
        manager.removeUpdates(listener);
    }

最新更新