在服务上使用SharedPreferences的问题(服务上不存在getPreferences)

Nul*_*ion 28 service android sharedpreferences

我有一些我希望从服务访问的共享首选项(纬度,经度),它不是来自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) {}
}
Run Code Online (Sandbox Code Playgroud)

我收到错误就行了 settings = this.getPreferences(MODE_WORLD_WRITEABLE);

Com*_*are 45

如果您只使用一个SharedPreferences用于您的应用程序,请将所有代码通过PreferenceManager.getDefaultSharedPreferences().

  • @AndroidUser99:出了什么问题是你没有阅读文档.http://developer.android.com/reference/android/preference/PreferenceManager.html#getDefaultSharedPreferences(android.content.Context) (12认同)
  • @AndroidUser99:更具体地说,`getDefaultSharedPreferences()`将`Context`(例如,你的`Service`)作为参数. (5认同)
  • @RuudLenders:恕我直言,应用程序本身可能不需要多个`SharedPreferences`,除非它有自己的多用户概念(与 4.2+ 平板电脑上的 Android 多个帐户分开)。但是,库应该使用单独的“SharedPreferences”,这样它们就不会意外地与包含该库的应用程序使用的“SharedPreferences”发生冲突。 (2认同)

Til*_*ill 19

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

解:

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