2017-01-10 92 views
1

我有一个必须始终运行的粘滞后台服务。基本上它跟踪时间,它是一个自定义时钟计时器。但是一旦设备进入空闲状态(当电话屏幕关闭时),定时器(后台服务)也会暂停。手机进入空闲状态时,后台服务会停止

即使屏幕关闭,我应该怎么做才能使它始终保持运行?

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; 
     } 
    } 
+0

你可以请把你的代码.. – pkgrover

+0

你有你的清单'wakeLock'许可? –

+0

是的,我有。但它会耗尽电池。我正在寻找优化的解决方案。但随时也可以分享该解决方案。由于该服务需要始终运行。 –

回答

3

所以当设备进入睡眠模式时,有一些服务不会被杀死。 我开始将我的服务作为通知附带的前台服务。我不认为它是长期服务的更好方法。但是,当然这个解决方案是为了更多的优化而打开的。此解决方案在睡眠模式下不会使服务进入暂停状态。

以下是整个服务代码:

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 
    } 
} 

有用的链接:

Run a service in the background forever..? Android

How to run background service after every 5 sec not working in android 5.1?

How to run a method every X seconds

How to make service run even in sleep mode?

part-1 persistent foreGround android service that starts by UI, works at sleep mode too, also starts at phone restart

part-2 persistent foreGround android service that starts by UI, works at sleep mode too, also starts at phone restart

Android - implementing startForeground for a service?

0

如果你想使你的服务为从未在onStartCommand

结束服务的话,用户的代码
@Override 
    public int onStartCommand(Intent intent, int flags, int startId) { 
    //Put code here 
    return START_REDELIVER_INTENT; 

} 

可以使用START_STICKY也。

+0

让我再次重现此问题,我会尽快回复您。 –

+0

AhmadShahwaiz:你检查过吗? – samsad

+0

该案件并未在我已编写的代码中重现。我已经在使用Start_Sticky服务。这和start_redeliver_intent的主要区别在于,在start_redeliver_intent服务中,当服务重新启动时,以前的意图再次被发送。我看不到start_redeliver_intent会在空闲状态下暂停服务。我会等待它重现,然后我会使用你的解决方案,如果它的工作,那么我会标记你的答案。 –

相关问题