Search code examples
androidvariablesservicesticky

Android maintain variable values in Service when app is swiped away


My app uses a sticky service to do things in background even when the app is closed.

When the app is swiped away, the service doesn't stop(or gets restarted). But all the variable values gets reset to the initial value. I wan't to keep all the variable values even when the app is swiped away. Is this possible?

Example

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Intent test = new Intent(this, TestService.class);
        startService(test);
    }
}

public class TestService extends Service {


    @Override
    public void onCreate() {
        super.onCreate();
        new Thread(new Runnable() {
            @Override
            public void run() {
                while (true) {
                    Log.e("testVal: ", TestObj.test + "");
                    try {
                        Thread.sleep(2000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        }).start();
        TestObj.fillList();
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {

        return Service.START_STICKY;
    }

    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }
}

public class TestObj {
    public static int test = 0;

    public static void fillList() {
        try {
            Thread.sleep(5000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        test = 11;
    }
}

Output:
**First time app is started:**
04-24 17:51:00.234  27628-27647/com.service.test.servicetest E/testVal:﹕ 0
04-24 17:51:02.235  27628-27647/com.service.test.servicetest E/testVal:﹕ 0
04-24 17:51:04.235  27628-27647/com.service.test.servicetest E/testVal:﹕ 0
04-24 17:51:06.236  27628-27647/com.service.test.servicetest E/testVal:﹕ 11
04-24 17:51:08.236  27628-27647/com.service.test.servicetest E/testVal:﹕ 11
04-24 17:51:10.237  27628-27647/com.service.test.servicetest E/testVal:﹕ 11
04-24 17:51:12.237  27628-27647/com.service.test.servicetest E/testVal:﹕ 11
04-24 17:51:14.237  27628-27647/com.service.test.servicetest E/testVal:﹕ 11
**Directly after app is swiped away:**
04-24 17:51:16.447  27745-27763/com.service.test.servicetest E/testVal:﹕ 0
04-24 17:51:18.492  27745-27763/com.service.test.servicetest E/testVal:﹕ 0
04-24 17:51:20.534  27745-27763/com.service.test.servicetest E/testVal:﹕ 0
**5 seconds after app is swiped away:**
04-24 17:51:22.575  27745-27763/com.service.test.servicetest E/testVal:﹕ 11
04-24 17:51:24.615  27745-27763/com.service.test.servicetest E/testVal:﹕ 11
04-24 17:51:26.655  27745-27763/com.service.test.servicetest E/testVal:﹕ 11
04-24 17:51:28.696  27745-27763/com.service.test.servicetest E/testVal:﹕ 11
04-24 17:51:30.736  27745-27763/com.service.test.servicetest E/testVal:﹕ 11

As you can see, when the app is swiped away, the value of Testobj.test is reset to it's initial value of 0. After five secondse the method fillList is called which changes the value of TestObj.test to 11.

The desired behaviour would be that the values stays 11 after the app is swiped away. Is this possible?


Solution

  • The system can unload your class at any time so never use static variables on Android or at least you should use them with care. So you can use a service attribute but the service can be killed even if you use a foreground service. My suggestion is to write the value as shared preference and get it from there if the service restarts.