我有一些我希望从服务访问的共享首选项(纬度,经度),它不是来自Activity的子类.
特别是,当我尝试访问getPreferences时,此功能在服务上不存在.我的代码发布在下面
我的目标是允许在我的服务中使用这些共享首选项.有什么建议/例子可以帮助我吗?
public class MyService extends Service implements Runnable {
LocationManager mLocationManager;
Location mLocation;
MyLocationListener mLocationListener;
Location currentLocation = null;
static SharedPreferences settings;
static SharedPreferences.Editor configEditor;
public IBinder onBind(Intent intent) {
return null;
}
public void onCreate() {
settings = this.getPreferences(MODE_WORLD_WRITEABLE);
configEditor = settings.edit();
writeSignalGPS();
}
private void setCurrentLocation(Location loc) {
currentLocation = loc;
}
private void writeSignalGPS() {
Thread thread = new Thread(this);
thread.start();
}
@Override
public void run() {
mLocationManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
if (mLocationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
Looper.prepare();
mLocationListener …Run Code Online (Sandbox Code Playgroud) 我将一些值存储到从窗口小部件启动的活动的共享首选项中.如果我从同一小部件启动的服务中检索该值,则不是更新的小部件.我在共享偏好中获得了之前的值.即使我在共享首选项xml中检查该值,我在那里看到更新的那个.为什么会这样.我知道小部件和活动是两个过程,是什么原因?
SharedPreferences preferences = getSharedPreferences("preferences_target_value", Context.MODE_PRIVATE);
String targetValue = preferences.getString("preferences_target_value", "0");
System.out.println("targetValue "+targetValue);`
Run Code Online (Sandbox Code Playgroud)