我想要一个后台服务,它会在应用程序关闭后保持活动状态,并且在应用程序启动时我可以再次绑定。
为了进行测试,我将每次绑定到服务时计数器都会增加。
所以理论上应用程序应该启动,我将创建服务,然后绑定到它 -> 计数器应该向上移动。
然后我关闭应用程序并再次按下绑定按钮,它应该记录一个“1”并再次向上移动计数器。
但它没有......每次我重新启动应用程序并绑定到它时它都会显示一个 0......
这是我当前的测试 - 服务 - 类:
package com.programm.testapp;
import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;
import android.util.Log;
public class TestService extends Service {
/*
* Service Binder
*/
private final IBinder iBinder = new TestService.LocalConnectionService();
public class LocalConnectionService extends Binder {
public TestService getService(){
return TestService.this;
}
}
/*
* Test var
* It should increase every time the app is started.
*/
private int test;
@Override
public IBinder onBind(Intent intent) {
Log.d("mDEBUG", "Test: " + test);
test++;
return iBinder;
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.d("mDEBUG", "Service: Start Command");
return START_STICKY;
}
}
Qyouu
相关分类