我想我这样做对吗?
我有这个代码,它通过一个MyLocationListener方法开始寻找我的GPS位置,这里没有显示,它可以工作,但我想要停止locationManager onPause,或者当这个活动不是最新的时候,但是我不能得到removeUpdates代码来解决。
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, MINIMUM_TIME_BETWEEN_UPDATES, MINIMUM_DISTANCE_CHANGE_FOR_UPDATES, new MyLocationListener());然后,
@Override
public void onPause()
{
super.onPause();
locationManager.removeUpdates(MyLocationListener);
}“MyLocationListener”解决不了问题,我也试过“this”,
locationManager.removeUpdates((LocationListener) this);它解决了问题,但在运行时给出了一个“Cannot Pause”错误。
发布于 2011-12-18 08:07:31
我有一个类似的问题:Having some trouble getting my GPS sensor to stop
您可能需要定义一个与启动和结束的LocationListener相同的and。
尝试:
LocationListener mlocListener; 在类名下,然后在onCreate方法中使用以下内容:
mlocListener = new MyLocationListener();并对整个类使用上面的mlocListener。
所以让你的类看起来像这样:
public class SomeClass extends Activity {
LocationManager mlocManager;
LocationListener mlocListener;
@Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.screenlayout);
mlocManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
mlocListener = new MyLocationListener();
mlocManager.requestLocationUpdates( LocationManager.GPS_PROVIDER, 0, 0, mlocListener);
}
@Override
public void onPause(){
mlocManager.removeUpdates(mlocListener);
super.onPause();
} 发布于 2012-05-25 18:32:53
您可以在Activity类中实现LocationListener接口:
public class MyActivity extends Activity implements LocationListener {
private LocationManager mLocMgr;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mLocMgr = (LocationManager) getSystemService(LOCATION_SERVICE);
mLocMgr.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000, 100, this);
}
@Override
public void onLocationChanged(Location location) {}
@Override
public void onProviderDisabled(String arg0) {}
@Override
public void onProviderEnabled(String provider) {}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {}
@Override
public void onPause() {
super.onPause();
mLocMgr.removeUpdates(this);
}
}发布于 2011-12-18 08:22:23
我认为您只需要切换超级onPause调用和removeUpdates调用的顺序即可。
@Override
public void onPause()
{
locationManager.removeUpdates(MyLocationListener);
super.onPause();
}https://stackoverflow.com/questions/8548859
复制相似问题