在onPause停止的LocationManager [英] onPause to stop LocationManager
问题描述
我觉得我这样做对吗?
我有这样的code可开始通过这里没有显示MyLocationListener方法,即工作找我的GPS位置,但我想停止的LocationManager的onPause,我觉得还是每当这个活动是不是最新的,但我可以'得到的,removeUpdates code来解决。
I have this code which starts looking for my GPS location via a MyLocationListener method not displayed here, that works but I want to stop the locationManager onPause, I think or whenever this activity is not current, but I can’t get the removeUpdates code to resolve.
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不会解决,我也试着本和
"MyLocationListener" wont resolve, I’ve also tried "this" and,
locationManager.removeUpdates((LocationListener) this);
这样就解决了,但是给了我一个在运行时不能暂停的错误。
Which resolves but gives me a "Cannot Pause" error at runtime.
推荐答案
我有一个类似的问题:
<一href=\"http://stackoverflow.com/questions/8539971/having-some-trouble-getting-my-gps-sensor-to-stop/8546115#8546115\">Having一些麻烦让我的GPS传感器停止
I had a similar question: Having some trouble getting my GPS sensor to stop
您可能需要定义一个LocationListener的是因为这是开始的一个和结束一个相同的。
you might need to define a LocationListener that is the same for the one that is started and the one that is ended.
尝试:
LocationListener mlocListener;
下的类名,然后用你的onCreate方法如下:
under the class name and then use the following in your onCreate method:
mlocListener = new MyLocationListener();
和使用上述mlocListener全班。
And use the above mlocListener for the whole class.
因此,有你的类看起来是这样的:
So have your class look something like this:
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();
}
这篇关于在onPause停止的LocationManager的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!