【问题标题】:Background service pause and start again automatically on OREO后台服务暂停并在 OREO 上自动重新启动
【发布时间】:2019-05-11 08:53:03
【问题描述】:

我创建了一个后台服务,用于注册 SCREEN_ON 和 SCREEN_OFF 意图以使用广播接收器捕获事件,但由于未知原因,有时我的服务没有将意图传递给我的主应用程序。为了测试,我使用十秒计时器生成了日志文件,它是我的后台服务工作与否,我附在下面。我遇到了一个问题,我的后台服务会暂停一段时间并自动启动。

这是我的后台服务

public class MyBackgroundService extends Service {

    BroadcastReceiver receiver;
    private static final String LOG_TAG = "MyBackgroundService";

    @Override
    public void onCreate() {
        super.onCreate();
        Timber.i("Foreground Service OnCreate");
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {

        Timber.i("Start Foreground Service");

        receiver = new ScreenStateReceiver();
        registerReceiver(receiver,  new IntentFilter(Intent.ACTION_SCREEN_ON));
        registerReceiver(receiver, new IntentFilter(Intent.ACTION_SCREEN_OFF));

        if (intent.getAction().equals(Constants.STARTFOREGROUND_ACTION)) {

            Intent notificationIntent = new Intent(this, HomeActivity.class);
           
            PendingIntent pendingIntent = PendingIntent.getActivity(this, 0,
                    notificationIntent, 0);

            Notification notification = new NotificationCompat.Builder(this)
                    .setContentTitle("text")
                    .setTicker("text")
                    .setContentText("text")
                    .setSmallIcon(R.drawable.logo_icon)
                    .setContentIntent(pendingIntent)
                    .setOngoing(true).build();

            startForeground(Constants.NOTIFICATION_ID.FOREGROUND_SERVICE,
                    notification);

        }
        startTimer();
        
        return START_STICKY;
    }

    @Override
    public boolean onUnbind(Intent intent) {
        Timber.i("Foreground Service onUnbind");
        return super.onUnbind(intent);
    }

    @Override
    public void onTaskRemoved(Intent rootIntent) {
        Timber.i("Foreground Service onTaskRemoved");
        super.onTaskRemoved(rootIntent);
    }

    @Override
    public void onDestroy() {
        stopForeground(true);
        Timber.i("Foreground Service onDestroy");
        stoptimertask();
        super.onDestroy();
    }

    @Override
    public IBinder onBind(Intent intent) {
        // Used only in case of bound services.
        return null;
    }

    private Timer timer;
    private TimerTask timerTask;
    public void startTimer() {
        //set a new Timer
        timer = new Timer();

        //initialize the TimerTask's job
        initializeTimerTask();

        //schedule the timer, to wake up every 1 second
        timer.schedule(timerTask, 10000, 10000); //
    }

    /**
     * it sets the timer to print the counter every x seconds
     */
    public void initializeTimerTask() {
        timerTask = new TimerTask() {
            public void run() {
                Timber.i("Timer");
            }
        };
    }

    /**
     * not needed
     */
    public void stoptimertask() {
        //stop the timer, if it's not already null
        if (timer != null) {
            timer.cancel();
            timer = null;
        }
    }

}

这是我的广播接收器

public class ScreenStateReceiver extends BroadcastReceiver {

    public ScreenStateReceiver(){
    }

    @Override
    public void onReceive(Context context, Intent intent) {

        String action = intent.getAction();
        if(action.equals(Intent.ACTION_SCREEN_OFF)){

            Timber.i("Screen OFF");
       }
        else if (intent.getAction().equals(Intent.ACTION_SCREEN_ON)) {
            Timber.i("Screen ON");
        }
    }
}

菜单,xml文件声明

<uses-permission android:name="android.permission.WAKE_LOCK"/>
    
    <service android:name=".service.MyBackgroundService"/>
    
    <receiver android:name=".receiver.ScreenStateReceiver" android:enabled="true" android:exported="true">
        <intent-filter >
            <action android:name="android.intent.action.SCREEN_OFF" />
            <action android:name="android.intent.action.SCREEN_ON" />
        </intent-filter>
    </receiver>

我正在使用意图启动服务

Intent service = new Intent(getContext().getApplicationContext(), MyBackgroundService.class);

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {

    service.setAction(Constants.STARTFOREGROUND_ACTION);
    getContext().getApplicationContext().startForegroundService(service);
}
else{

    service.setAction(Constants.MAIN_ACTION);
    getContext().getApplicationContext().startService(service);
}

这是我的日志文件的这张图片 LOG FILE 使用此日志文件,您可以看到后台服务自动停止和启动。 请帮我解决这个问题。

【问题讨论】:

  • 这里是一个类似案例的链接,它有一些见解,可能会有所帮助stackoverflow.com/questions/52425222/…
  • 也尝试检查这个链接,它显示了如何实现类似的fabcirablog.weebly.com/blog/…
  • @UmarQureshi 我已经尝试过你在第二条评论中给我的这个链接。它有同样的问题。
  • @UmarQureshi 我已经实现了你的两个链接,但它不起作用。
  • 同样的问题 :(

标签: android broadcastreceiver android-service


【解决方案1】:

Oreo 以后对服务有限制,只有当应用程序在前台时,我们才能通过调用 startService() 来启动服务。当您退出应用程序(应用程序在后台)时,它有几分钟的窗口,在该应用程序中可以运行该服务。在窗口结束时,应用程序被认为是空闲的,此时系统停止后台服务。为了实现在后台运行服务,要么我们必须实现前台服务,要么我们必须使用作业调度器等。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-11-26
    • 2014-02-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多