Search code examples
androidservicecrashbroadcastreceiverlocationmanager

Android Service Not Retaining Data onRestart


I am using an Arraylist to store user selected alert-locations and am comparing these "locations" with the user's present location in my service via a broadcaster.

On force crashing the application, I am able to restart the service but the data in the Arraylist created for comparison disappears and hence, the service cannot find the preexisting data.

I have no clue what I am missing? Here is my method used for saving the location and requestID in a ArrayList.

private void addProximityAlert(double latitude, double longitude,int requestID) {
     Location alertLoc=new Location("C");
     IntentFilter filter = new IntentFilter(proximitysd);
     registerReceiver(mybroadcast, filter);
     Intent intent = new Intent(proximitysd); 
     //Saving requestID in an array  
     requestNo.add(Integer.valueOf(requestID)); 
     alertLoc.setLatitude(latitude);
     alertLoc.setLongitude(longitude);
     //Saving alertLoc in an array
     proximitylocation.add(alertLoc);   
     PendingIntent proximityIntent = PendingIntent.getBroadcast(this,     requestID, intent, PendingIntent.FLAG_CANCEL_CURRENT);
     Log.v("add Proximity ","Pendingintentcreated");
}

p.s. I am not intending to use locationmanager.addproximityalert.


Solution

  • You could use shared preferences to save data when your app force closes. This is as good an tutorial as any.