簡體   English   中英

當電話進入空閑狀態時,后台服務停止

[英]Background service stops when phone goes in idle state

我有一個粘性的后台服務,必須一直運行。 基本上它跟蹤時間,它是一個自定義時鍾計時器。 但是一旦設備進入空閑狀態(當電話屏幕關閉時),定時器(后台服務)也會暫停。

即使屏幕關閉,我該怎么做才能使它始終保持運行?

    public class TimerService extends Service {
    private Context context;
    @Override
        public IBinder onBind(Intent intent) {
            Logger.LogI(TAG, "Service binding");
            return null;
        }

      @Override
        public void onCreate() {
            super.onCreate();
            context = getApplicationContext();
        }

     @Override
        public int onStartCommand(Intent intent, int flags, int startId) {
       return Service.START_STICKY;
        }
    }

因此,當設備進入睡眠模式時,有一些方法可以使服務不被殺死。 我開始將我的服務作為附加通知的前台服務。 我並不是說這是長期服務的更好方法。 但當然這個解決方案可以進行更多優化。 在睡眠模式下,此解決方案不會使服務進入暫停狀態。

以下是整個服務代碼:

public class TimerService extends Service {
    private ScheduledExecutorService scheduleTaskExecutor;
    private Context context;
    @Override
    public IBinder onBind(Intent intent) {
        Logger.LogI(TAG, "Service binding");
        return null;
    }

    @Override
    public void onCreate() {
        super.onCreate();
        context = getApplicationContext();
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        setNotification();
        if (scheduleTaskExecutor == null) {
            scheduleTaskExecutor = Executors.newScheduledThreadPool(1);
            scheduleTaskExecutor.scheduleAtFixedRate(new mainTask(), 0, 1, TimeUnit.SECONDS);
        }
        return Service.START_STICKY;
    }

    public void setNotification() {
        PendingIntent contentIntent;
        contentIntent = PendingIntent.getActivity(this, 0,
            new Intent(this, Main_Activity.class), 0);

        NotificationManager mNotificationManager = (NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE);
        NotificationCompat.Builder mBuilder =
            new NotificationCompat.Builder(this)
            .setSmallIcon(R.drawable.notification_icon)
            .setColor(this.getResources().getColor(R.color.action_bar_color))
            .setContentTitle("MainActivity")
            .setOngoing(true)
            .setAutoCancel(false)
            .setContentText("MyApp");
        mBuilder.setContentIntent(contentIntent);
        mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());

        Notification notification = mBuilder.build();
        startForeground(NOTIFICATION_ID, notification); //NOTIFICATION_ID is a random integer value which has to be unique in an app
    }
}
private class mainTask implements Runnable {

    public void run() {
        // 1 Second Timer
    }
}

有用的網址:

永遠在后台運行服務..? Android的

如何運行后5秒后無法在Android 5.1中運行后台服務?

如何每X秒運行一次方法

如何在睡眠模式下運行服務?

由UI啟動的第1部分持久foreGround android服務,也在睡眠模式下工作,也在手機重啟時啟動

由UI啟動的第2部分持久foreGround android服務,也在睡眠模式下工作,也在手機重啟時啟動

Android - 為服務實現startForeground?

如果您希望將服務作為永無止境的服務,請在onStartCommand中使用此代碼

@Override
    public int onStartCommand(Intent intent, int flags, int startId) {
     //Put code here
    return START_REDELIVER_INTENT;

}

您也可以使用START_STICKY。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM