我有一些共享首选项(纬度、经度),我想从服务访问这些首选项,该服务不是 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 = 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()得到它。

其他提示

实际上,大多数人可能会遇到这样的问题:在 API >=11 的设备上,默认情况下不再为多进程使用设置共享首选项。

解决方案:

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
    prefs = getSharedPreferences(PREFS, 0 | Context.MODE_MULTI_PROCESS);
} else {
    prefs = getSharedPreferences(PREFS, 0);
}

小晚来帮助你的,但我希望这可以帮助别人的未来。 这是你的问题:

    public void onCreate() {
       settings = this.getPreferences(MODE_WORLD_WRITEABLE);
       configEditor = settings.edit();
       writeSignalGPS();
    }

因为只在创建服务时检索共享偏好文件,该文件被适当地从不由服务更新,因此,数据从来不与应用程序共享。写入共享偏好或检索可能已经改变的任何数据之前,确保以检索共享偏好文件(重装)再次如:

    private Handler handler = new Handler() {
    @Override
    public void handleMessage(Message msg) {
        if (currentLocation!=null) {
            settings = this.getPreferences(MODE_WORLD_WRITEABLE);
            configEditor = settings.edit();
            configEditor.putString("mylatitude", ""+currentLocation.getLatitude());
            configEditor.putString("mylongitude", ""+currentLocation.getLongitude());
            configEditor.commit();
        }
    }

然后在应用程序中:

     settings = this.getPreferences(MODE_WORLD_WRITEABLE);
     String myLat = setttings.getString("mylatitude","");

此外,在Android 3.0的任何有这样一个场景,一个服务和活动共享共享偏好设置,你应该使用:

     settings = this.getPreferences(MODE_MULTI_PROCESS);

如果您已声明的 SharedPreferences 为:

private static final String PREFS_NAME = "UserData"; 
private static final String PREFS_VALUE1 = "value1"; 

然后使用以下代码来获取值:

SharedPreferences preferences = context.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE);
value1 = preferences.getString(PREFS_VALUE1, "0");

在相同的方式,甚至可以保存值SharedPreferences。

有关谁碰到这样的人......我有一个类似的问题...问题的真正原因是,我们正在试图让/使用不完全初始化的情况下。我能正常使用sharedPreferences我的构造为我IntentService之外。

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top