Search code examples
androidandroid-serviceandroid-4.0-ice-cream-sandwich

Android: Static Arraylist is becoming empty in service


I have declared a static Arraylist variable inside a service file (backgroundService.java). I am updating the static variable once i received a Push Message (using GCM). I am using 4.0.4 Device(Samsung S3).. The static variable becomes empty, once my app goes background and then if i long press the memu button and Swipes my application(Once we swipe the app is getting finished but not force stopped),the service is paused for a while and again started i.e, the service onDestroy method is not called instead OnCreate method is after that pause.. After that pause the static ArrayList becomes empty. How to resolve it ? I dont want my static Arraylist to lose its value..

This problem occurs in Android Version 4.0+ devices only.

public class BackgroundService extends Service 

{ private Timer timer = new Timer();

Context mContext;

String LOG_TAG=BackgroundService.class.getSimpleName();
    ApplicationInfo ai;
    private static boolean isRunning = false; 
public static ArrayList<String> mDMPush=new ArrayList<String>();

@Override
public void onCreate() 
{
      super.onCreate();
      Log.e(LOG_TAG,"on create ");
      isRunning = true; 


}


@Override
public void onStart(Intent intent, int startId) {

    super.onStart(intent, startId);  


    Log.e(LOG_TAG,"mDMPush"+mDMPush);

    Log.e(LOG_TAG,"mDMPush outside if else "+mDMPush.size()+"   "+mDMPush.isEmpty());
            startService();



}

public static boolean isRunning() 
{ 
    return isRunning; 
} 

private void startService()
{        
   int delay = 100;
   int period =500;
final PackageManager pm = getPackageManager();

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

        ActivityManager am = (ActivityManager) mContext.getSystemService(Activity.ACTIVITY_SERVICE);
        String packageName = am.getRunningTasks(1).get(0).topActivity.getPackageName();
        try {
                 ai = pm.getApplicationInfo(packageName, 0);
                 if ((ai.flags & ApplicationInfo.FLAG_SYSTEM) != 0) {


                       **Iam doing an action inside this timer using that arraylist mDMPush.. I ll add value to the arraylist once i received a push***              

                 }
           } catch (NameNotFoundException e) {
                 // TODO Auto-generated catch block
                 e.printStackTrace();
           }



       }
     }, delay, period);

}  

Solution

  • This how you have to save array list in persistence storage

      Set<String> localStore = new HashSet<String>();
                List<String> list = new ArrayList<String>();
        localStore.addAll(list);
        SharedPreferences ssidPref = getSharedPreferences("ssidPref", Context.MODE_PRIVATE);
                    SharedPreferences.Editor EDITIOR = ssidPref.edit();
                    EDITIOR.putStringSet("ssidStored", localStore);
                    EDITIOR.commit();
    

    to Read

    localStore = ssidPref.getStringSet("ssidStored", null);