-3

I need to get ComplexPreferences within a Service

Code

timer.scheduleAtFixedRate(new TimerTask() {
            @Override
            public void run() {

                sendRequestToServer();
            }

            private void sendRequestToServer() {

// How to get context here to initialize ctx ?

                        ComplexPreferences complexPreferences = 
ComplexPreferences.getComplexPreferences( ctx , "App_Settings", 0);

}

How to do it?

Full code of class

public class gps_service extends Service {

    private static final String TAG = "MyService";
    private LocationListener listener;
    private LocationManager locationManager;
    private Timer timer = new Timer();
    private  DLocation dLocation;
    private final Object lock = new Object();

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        Log.i("LocalService", "Received start id " + startId + ": " + intent);
        // We want this service to continue running until it is explicitly
        // stopped, so return sticky.
        return START_STICKY;
    }

    @Override
    public void onCreate() {
        super.onCreate(); 
        timer.scheduleAtFixedRate(new TimerTask() {
            @Override
            public void run() {

                sendRequestToServer();
            }

            private void sendRequestToServer() {

                synchronized (lock) {
                    try{

                        if(dLocation == null) return;

                        ComplexPreferences complexPreferences = ComplexPreferences.getComplexPreferences( context , "App_Settings", 0);
                        AppSettings appSettings = complexPreferences.getObject("App_Settings", AppSettings.class);
                        if(appSettings != null)
                        {

                        }

                        DLocation tempDL = dLocation;

                        LocationItem locationItem = new LocationItem();
                        locationItem.DeviceID = tempDL.DeviceID;
                        locationItem.Latitude = tempDL.Latitude;
                        locationItem.Longitude = tempDL.Longitude;
                        locationItem.TimeOfRequest = tempDL.TimeOfRequest;

                        Gson gson = new Gson();
                        String requestObject =  gson.toJson(locationItem);
                        String url = "http://XXXXXXX";

                        makeRequest(url, requestObject);
                    }
                    catch (Exception ex)
                    {

                    }
                }
            }
        }, 0, 1*60*1000); //1 Minutes
NoWar
  • 36,338
  • 80
  • 323
  • 498

1 Answers1

2

Try this

ComplexPreferences complexPreferences = ComplexPreferences.getComplexPreferences( YourService.this , "App_Settings", 0);

Or this

Context context = this;
ComplexPreferences complexPreferences = ComplexPreferences.getComplexPreferences( context, "App_Settings", 0);

EDIT

public class gps_service extends Service {

    private static final String TAG = "MyService";
    private LocationListener listener;
    private LocationManager locationManager;
    private Timer timer = new Timer();
    private  DLocation dLocation;
    private final Object lock = new Object();
    Context context ;
    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        Log.i("LocalService", "Received start id " + startId + ": " + intent);
        // We want this service to continue running until it is explicitly
        // stopped, so return sticky.
        return START_STICKY;
    }

    @Override
    public void onCreate() {
        super.onCreate(); 
        context = this;
        timer.scheduleAtFixedRate(new TimerTask() {
            @Override
            public void run() {

                sendRequestToServer();
            }

            private void sendRequestToServer() {

                synchronized (lock) {
                    try{

                        if(dLocation == null) return;

                        ComplexPreferences complexPreferences = ComplexPreferences.getComplexPreferences( context , "App_Settings", 0);
                        AppSettings appSettings = complexPreferences.getObject("App_Settings", AppSettings.class);
                        if(appSettings != null)
                        {

                        }

                        DLocation tempDL = dLocation;

                        LocationItem locationItem = new LocationItem();
                        locationItem.DeviceID = tempDL.DeviceID;
                        locationItem.Latitude = tempDL.Latitude;
                        locationItem.Longitude = tempDL.Longitude;
                        locationItem.TimeOfRequest = tempDL.TimeOfRequest;

                        Gson gson = new Gson();
                        String requestObject =  gson.toJson(locationItem);
                        String url = "http://XXXXXXX";

                        makeRequest(url, requestObject);
                    }
                    catch (Exception ex)
                    {

                    }
                }
            }
        }, 0, 1*60*1000); //1 Minutes
Ratilal Chopda
  • 4,162
  • 4
  • 18
  • 31