gpt4 book ai didi

android - onPause 停止 LocationManager

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:41:54 26 4
gpt4 key购买 nike

我觉得我这样做对吗?

我有这段代码,它通过此处未显示的 MyLocationListener 方法开始寻找我的 GPS 位置,它可以工作,但我想在暂停时停止 locationManager,我想或每当此 Activity 不是当前 Activity 时,但我无法获得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);

这解决了但在运行时给我一个“无法暂停”错误。

最佳答案

我有一个类似的问题: Having some trouble getting my GPS sensor to stop

您可能需要为开始的和结束的定义一个相同的 LocationListener。

尝试:

LocationListener mlocListener; 

在类名下,然后在您的 onCreate 方法中使用以下内容:

mlocListener = new MyLocationListener();

并为整个类使用上面的 mlocListener。

让你的类(class)看起来像这样:

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();
}

关于android - onPause 停止 LocationManager,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8548859/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com