当应用程序被刷掉时,Android会在Service中维护变量值

Mee*_*tat 2 variables service android sticky

即使应用关闭,我的应用也会使用粘性服务在后台执行操作.

当应用程序被刷掉时,服务不会停止(或重新启动).但是所有变量值都会重置为初始值.即使应用程序被刷掉,我也不会保留所有变量值.这可能吗?

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
Run Code Online (Sandbox Code Playgroud)

正如您所看到的,当应用程序被刷掉时,Testobj.test的值被重置为它的初始值0.五秒后调用方法fillList,它将TestObj.test的值更改为11.

期望的行为是应用程序被刷掉后值保持为11.这可能吗?

gre*_*f82 6

系统可以随时卸载您的类,所以永远不要在Android上使用静态变量,或者至少应该谨慎使用它们.因此,您可以使用服务属性,但即使您使用前台服务也可以终止该服务.我的建议是将值写为共享首选项,并在服务重新启动时从那里获取它.

  • 是.对于活动,您可以使用onSaveInstanceState(),但无法在服务上执行此操作. (2认同)