【问题标题】:Repeating service gets killed, does it restarts againg if we use start sticky重复服务被杀死,如果我们使用 start sticky 是否会再次重新启动
【发布时间】:2015-12-07 08:09:50
【问题描述】:

只要安装了应用程序,我就有每 5 分钟运行一次的重复服务,但有时我的服务会停止或被杀死,如果它被操作系统杀死,我如何使其失效并重新启动服务。

这就是我开始重复服务的方式。

    Intent serviceIntent = new Intent(context, SyncService.class);
    final AlarmManager alarm = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
    long interval = 60000; // 5 Mins - 1000*60; 1 sec=1000, 1min sec = 60*1000=60000
        final PendingIntent pending = PendingIntent.getService(context, 0, serviceIntent, 0);
    alarm.setRepeating(AlarmManager.RTC ,System.currentTimeMillis(), interval, pending);

这是我的服务:

public class SyncService extends IntentService {

public SyncService() {
    super("SyncService"); 
    // TODO Auto-generated constructor stub
}

@Override
protected void onHandleIntent(Intent intent) {


    SyncToServer();


 }
 }

【问题讨论】:

标签: android android-layout android-activity android-service


【解决方案1】:

IntentService 总是在工作完成时被杀死。如果您想要长期运行的服务,请改用基本服务:http://developer.android.com/reference/android/app/Service.html

【讨论】:

    猜你喜欢
    • 2015-08-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多