我的应用程序在Android服务中使用LocationListener来更新频繁位置。应用程序具有一些与共享首选项相关的位置过滤器相关数据要求是尽可能频繁地获取位置更新。 我从侦听器的onLocationChanged中的Shared Preference中检索数据。 这是我的代码是否重复使用Android Shared Preference存储会影响性能?
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之外放置过滤器变量,但是当服务重新启动时,值丢失并设置为零。
我只想知道是不是好的做法? 我需要使用其他选项吗?
如果该值丢失,只需将if(sharedPreferences == null)初始化即可。 –