【问题标题】:Android is killing my service?Android正在扼杀我的服务?
【发布时间】:2015-06-13 10:02:11
【问题描述】:

使用 BroadCastReceiver,我在智能手机启动时执行服务:

public class BootReceiver extends BroadcastReceiver{
@Override
public void onReceive(Context context, Intent intent) {
    Intent startServiceIntent = new Intent(context, MyService.class);
    context.startService(startServiceIntent);
}
}

我的服务:

private Runnable myRunnable = new Runnable() {
    public void run() {
        parsing.cancel(true);
        parsing = new Parsing();
        parsing.execute();
        handler.postDelayed(this, timeoutUpdate);
    }
};

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    handler.removeCallbacks(myRunnable);
    handler.postDelayed(myRunnable, 1000); 
   return Service.START_STICKY;
}

该服务实际上是在启动时执行的,但是如果我在两次执行之间设置了 1 小时的超时时间,则服务不会执行(可能是系统正在杀死它)。否则,如果我在重复之间设置 60 秒,则一切正常。 我该怎么做?谢谢。

【问题讨论】:

    标签: android service broadcastreceiver android-service android-background


    【解决方案1】:

    您可以使用startForeground()foreground 中运行该服务。

    前台服务是被认为是某种东西的服务 用户是主动意识到的,因此不是系统的候选者 在内存不足时杀死。

    但请记住,前台服务必须为状态栏提供通知(在此处阅读),并且除非服务停止或从前台删除,否则无法解除通知。

    注意:这仍然不能绝对保证服务不会在内存极低的情况下被杀死。它只会降低它被杀死的可能性。

    如果您不想在前台运行服务,则可以使用 AlarmManager 定期运行服务

    Intent intent = new Intent(context, MyService.class);
    PendingIntent pintent = PendingIntent.getService(this, 0, intent, 0);
    AlarmManager alarm = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
    alarm.setRepeating(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), AlarmManager.INTERVAL_HOUR, pintent);
    

    更新

    使用取消注册的事件

        Intent intent = new Intent(context, MyService.class);
        PendingIntent pintent = PendingIntent.getService(this, 0, intent, 0);
        AlarmManager alarm =     (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
       alarmManager.cancel(pintent);
    

    【讨论】:

    • 您好,您的警报管理器解决方案应该在我的启动接收器类中实现?
    • 是的,它应该在 Receiver 和 MainActivity 中实现,因为当您安装应用程序时,警报管理器是通过您的 MainActivity 注册的
    • 如何停止以这种方式启动的服务?如果用户禁用了 Main Activity 的自动更新,并且该服务自启动后正在执行,我该如何停止它?
    • 我的朋友,我正在测试您的解决方案,如果一切正常,我会将其作为答案。而不是 cal.getTimeInMillis(),我使用 System.currentTimeMillis(),对吗?
    • 是的,你可以使用 System.currentTimeMillis()+1000*60*60 四小时间隔
    【解决方案2】:

    要安排作业,请在 BroadcastReciever 中使用 AlarmManager

    Intent intent = new Intent(context, MyService.class);
    PendingIntent pintent = PendingIntent.getService(context, 0, intent, 0);
    AlarmManager alarm = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
    alarm. setRepeating(AlarmManager.RTC_WAKEUP, triggerInMillis, intervalMillis, pintent);
    

    系统将唤醒您的服务,然后您将能够立即执行任务。

    【讨论】:

    • 嗨,这样我的服务总是在执行完所有执行后被销毁并每小时重新创建一次?
    • 阅读有关服务的文档。每个 context.startService 都会影响调用 onStartCommand()。如果服务正在运行,系统不会破坏服务。将使用现有实例。
    【解决方案3】:

    为您的服务设置最高优先级

    <intent-filter 
                   android:priority="integer" >
    </intent-filter>
    

    该值必须是整数,例如“100”。数字越大优先级越高。默认值为 0。该值必须大于 -1000 且小于 1000。

    【讨论】:

      猜你喜欢
      • 2017-06-28
      • 1970-01-01
      • 2023-03-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-03-25
      相关资源
      最近更新 更多