我有一个使用服务在后台运行的系统。当打开或“隐藏”应用程序时,它运行良好。当用户将其从最近的应用程序列表中删除时,我得到日志:I/ActivityManager: Killing 4419:com.cybermickey2077/u0a34 (adj 16): remove task
或类似的内容,并且服务停止。
即使应用程序完全关闭,我该怎么做才能保持服务运行?
代码:
public class TimeCounter extends Service {
@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public void onCreate() {
super.onCreate();
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Workercount = new Worker();
count.start();
return START_STICKY;
}
class Worker extends Thread {
public void run() {
while (true) {
this.verAlarmes();
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public void verAlarmes() {
Calendar calendar = Calendar.getInstance();
SimpleDateFormat format = new SimpleDateFormat("HH:mm");
String time = format.format(calendar.getTime());
Log.e("logmickey", "Now: " + time);
}
}
}