我的应用程序在Android服务中使用LocationListener获取频繁的位置更新。应用程序有一些与位置过滤器相关的数据存储在共享偏好中。要求尽可能频繁地获取位置更新。 我在监听器的onLocationChanged方法中从共享偏好中检索数据。 以下是我的代码:
public class MyLocationListener implements LocationListener {
public void onLocationChanged(final Location loc) {
sharedPreferences = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
int filterOne = sharedPreferences.getInt("filter_data",100);
------
------
//code to process location with filter
------
------
}
}
以上代码中使用了sharedPreference,但是在代码中它被反复使用。
我已经尝试将筛选变量放在onLocationChanged外面,但是当服务重新启动时,这些值会丢失并被设置为零。
我只想知道这是否是一个好的实践方式?我是否应该使用其他选项?