使用共享preferences在服务问题(GET preferences上的服务不存在) [英] Problems using SharedPreferences on a Service (getPreferences doesn't exist on a service)

查看:148
本文介绍了使用共享preferences在服务问题(GET preferences上的服务不存在)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些共享preferences(经度,纬度),我想从一个服务访问,不是从活动的子类。

I have some shared preferences (latitude, longitude) that I want to access from a service, that is not subclassed from Activity.

尤其是,当我尝试访问的get preferences,此功能不会对服务存在的。我的code为下面贴

In particular, when I try to access getPreferences, this function doesn't exist's on a service. My code is posted below

我的目标是让写这些共享preferences用我的服务。任何建议/例子,可以帮助我一起去吗?

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) {}
}

我上线的误差设置= this.get preferences(MODE_WORLD_WRITEABLE);

推荐答案

如果你只使用一个共享preferences 为你的应用程序中,有 所有您code通过 preferenceManager.getDefaultShared preferences得到它()

If you are only using one SharedPreferences for your application, have all your code get it via PreferenceManager.getDefaultSharedPreferences().

这篇关于使用共享preferences在服务问题(GET preferences上的服务不存在)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆