即使在清除正在运行的应用程序(RAM)之后我怎么能连续运行Android服务...请提供服务代码而不是让我重新启动android开发人员 我的代码如下:
public class MyService extends Service {
public MyService() {
}
@Nullable
@Override
public IBinder onBind(Intent intent) {
// TODO: Return the communication channel to the service.
return null;
}
@Override
public int onStartCommand(Intent intent1, int flags, int startId) {
// do your jobs here
return super.onStartCommand(intent1, flags, startId);
}
答案 0 :(得分:1)
您需要根据需要从onStartCommand返回START_STICKY或START_REDELIVER_INTENT。
public class MyService extends Service {
public MyService() {
}
@Nullable
@Override
public IBinder onBind(Intent intent) {
// TODO: Return the communication channel to the service.
return null;
}
@Override
public int onStartCommand(Intent intent1, int flags, int startId) {
// do your jobs here
return START_STICKY;
}
}
如开发者网站所述,如果您不打算传递任何启动服务的意图,则只需返回START_STICKY标志即可。这将使用空白的intent对象重新创建服务。
答案 1 :(得分:0)
您已将服务视为粘性,如此
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
return START_STICKY;
}