Android: 如何关闭 GPS
Android: How to deactivate GPS
正在开发 android 应用程序,我对 android 开发还很陌生。
目前我无法找到一种方法来停用设备上的 GPS。
我想做的是在用户单击按钮时停用 GPS。据我所知,唯一的方法是使用 LocationManager 中的 removeUpdates 方法进行购买。但我的问题是 atm 这个方法等待 LocationListiner 作为参数。但我不知道如何获取 LocationListiner :/
这是我的代码:
以下代码是我的MainActivity的onCreate的一部分
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);
您可以使用 activity 实现侦听器,然后
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);
}
正在开发 android 应用程序,我对 android 开发还很陌生。 目前我无法找到一种方法来停用设备上的 GPS。
我想做的是在用户单击按钮时停用 GPS。据我所知,唯一的方法是使用 LocationManager 中的 removeUpdates 方法进行购买。但我的问题是 atm 这个方法等待 LocationListiner 作为参数。但我不知道如何获取 LocationListiner :/
这是我的代码:
以下代码是我的MainActivity的onCreate的一部分
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);
您可以使用 activity 实现侦听器,然后 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);
}