Android服务需要始终运行(从不暂停或停止)

我创build了一个服务,并希望始终运行此服务,直到我的手机重新启动或强制closures。 该服务应该在后台运行。

创build的服务和启动服务的示例代码:

开始服务:

Intent service = new Intent(getApplicationContext(), MyService.class); getApplicationContext().startService(service); 

服务:

 public class MyService extends Service { @Override public int onStartCommand(Intent intent, int flags, int startId) { // TODO do something useful HFLAG = true; //smsHandler.sendEmptyMessageDelayed(DISPLAY_DATA, 1000); return Service.START_NOT_STICKY; } @Override public IBinder onBind(Intent intent) { // TODO for communication return IBinder implementation return null; } } 

清单声明:

 <service android:name=".MyService" android:icon="@drawable/ic_launcher" android:label="@string/app_name" > </service> 

是否有可能总是像应用程序暂停时一样运行此服务。 一段时间后,我的应用程序暂停,服务也暂停或停止。 那么我怎么能在后台运行这个服务呢?

“有没有可能像应用程序暂停时一样运行此服务?

是。

  1. 在服务onStartCommand方法返回START_STICKY。

     public int onStartCommand(Intent intent, int flags, int startId) { return START_STICKY; } 
  2. 使用startService(MyService)在后台启动服务,以使其始终保持活动状态,无论绑定的客户端数量是多less。

     Intent intent = new Intent(this, PowerMeterService.class); startService(intent); 
  3. 创build活页夹。

     public class MyBinder extends Binder { public MyService getService() { return MyService.this; } } 
  4. 定义一个服务连接。

     private ServiceConnection m_serviceConnection = new ServiceConnection() { public void onServiceConnected(ComponentName className, IBinder service) { m_service = ((MyService.MyBinder)service).getService(); } public void onServiceDisconnected(ComponentName className) { m_service = null; } }; 
  5. 使用bindService绑定到服务。

      Intent intent = new Intent(this, MyService.class); bindService(intent, m_serviceConnection, BIND_AUTO_CREATE); 
  6. 为了您的服务,您可能需要通知在closures后启动适当的活动。

     private void addNotification() { // create the notification Notification.Builder m_notificationBuilder = new Notification.Builder(this) .setContentTitle(getText(R.string.service_name)) .setContentText(getResources().getText(R.string.service_status_monitor)) .setSmallIcon(R.drawable.notification_small_icon); // create the pending intent and add to the notification Intent intent = new Intent(this, MyService.class); PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 0); m_notificationBuilder.setContentIntent(pendingIntent); // send the notification m_notificationManager.notify(NOTIFICATION_ID, m_notificationBuilder.build()); } 
  7. 您需要修改清单,以单顶模式启动活动。

      android:launchMode="singleTop" 
  8. 请注意,如果系统需要资源,而且您的服务不是非常活跃,则可能会死亡。 如果这是不可接受的使用startForeground将服务带到前台。

      startForeground(NOTIFICATION_ID, m_notificationBuilder.build()); 

为了在自己的进程中启动服务,您必须在xml声明中指定以下内容。

 <service android:name="WordService" android:process=":my_process" android:icon="@drawable/icon" android:label="@string/service_name" > </service> 

在这里你可以find一个很好的教程,对我来说真的很有用

http://www.vogella.com/articles/AndroidServices/article.html

希望这可以帮助

一个简单的解决scheme是在系统停止时重新启动服务。

我发现这个方法非常简单的实现:

如何使android服务不可阻挡

你可以为服务实现startForeground ,即使它死了,你也可以在startCommand()上使用START_STICKY来重启它。 不知道这是否是正确的实施。

我发现了一个简单明了的方法来保持Service始终运行。

这家伙已经解释清楚,并使用了一个很好的algorithm。 他的做法是当服务即将被杀死时发送一个广播,然后用它来重新启动服务。

你应该检查出来: http : //fabcirablog.weebly.com/blog/creating-a-never-ending-background-service-in-android

如果你已经有一个服务,并希望它一直工作,你需要添加2件事情:

  1. 在服务本身:

     public int onStartCommand(Intent intent, int flags, int startId) { return START_STICKY; } 
  2. 在清单中:

     android:launchMode="singleTop" 

除非在服务中需要绑定,否则不需要添加绑定。