在服务上使用 SharedPreferences 的问题(服务上不存在 getPreferences)
问题说明
我有一些共享首选项(纬度、经度),我想从服务访问这些首选项,这些首选项不是从 Activity 子类化的.
I have some shared preferences (latitude, longitude) that I want to access from a service, that is not subclassed from Activity.
特别是,当我尝试访问 getPreferences 时,服务上不存在此功能.我的代码贴在下面
In particular, when I try to access getPreferences, this function doesn't exist's on a service. My code is posted below
我的目标是允许在我的服务中写入这些共享首选项.有什么建议/例子可以帮助我吗?
My goal here is to allow WRITE these shared preferences with my service. Any suggestions/examples that can help me along?
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 = new MyLocationListener();
mLocationManager.requestLocationUpdates(
LocationManager.GPS_PROVIDER, 1000, 0, mLocationListener);
Looper.loop();
//Looper.myLooper().quit();
} else {
Toast.makeText(getBaseContext(),
getResources().getString(R.string.gps_signal_not_found),
Toast.LENGTH_LONG).show();
}
}
private Handler handler = new Handler() {
@Override
public void handleMessage(Message msg) {
if (currentLocation!=null) {
configEditor.putString("mylatitude", "" currentLocation.getLatitude());
configEditor.putString("mylongitude", "" currentLocation.getLongitude());
configEditor.commit();
}
}
};
private class MyLocationListener implements LocationListener {
@Override
public void onLocationChanged(Location loc) {
if (loc != null) {
Toast.makeText(getBaseContext(),
getResources().getString(R.string.gps_signal_found),
Toast.LENGTH_LONG).show();
setCurrentLocation(loc);
handler.sendEmptyMessage(0);
}
}
@Override
public void onProviderDisabled(String provider) {}
@Override
public void onProviderEnabled(String provider) {}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {}
}
我在 settings = this.getPreferences(MODE_WORLD_WRITEABLE);
正确答案
如果您的应用程序只使用一个 SharedPreferences
,请全部 您的代码通过 PreferenceManager.getDefaultSharedPreferences()
获取.
If you are only using one SharedPreferences
for your application, have all your code get it via PreferenceManager.getDefaultSharedPreferences()
.
这篇好文章是转载于:学新通技术网
- 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
- 本站站名: 学新通技术网
- 本文地址: /reply/detail/tanhcfjche
-
YouTube API 不能在 iOS (iPhone/iPad) 工作,但在桌面浏览器工作正常?
it1352 07-30 -
iPhone,一张图像叠加到另一张图像上以创建要保存的新图像?(水印)
it1352 07-17 -
保持在后台运行的 iPhone 应用程序完全可操作
it1352 07-25 -
使用 iPhone 进行移动设备管理
it1352 07-23 -
在android同时打开手电筒和前置摄像头
it1352 09-28 -
扫描 NFC 标签时是否可以启动应用程序?
it1352 08-02 -
检查邮件是否发送成功
it1352 07-25 -
Android微调工具-删除当前选择
it1352 06-20 -
希伯来语的空格句子标记化错误
it1352 06-22 -
Android App 和三星 Galaxy S4 不兼容
it1352 07-20