Android Service学习之本地服务

移动开发 Android
Service是在一段不定的时间运行在后台,不和用户交互应用组件。让我们通过这一篇文章来认识一下Android系统中的服务。

Service是在一段不定的时间运行在后台,不和用户交互应用组件。每个Service必须在manifest中 通过来声明。可以通过contect.startservice和contect.bindserverice来启动。

Service和其他的应用组件一样,运行在进程的主线程中。这就是说如果service需要很多耗时或者阻塞的操作,需要在其子线程中实现。

service的两种模式(startService()/bindService()不是完全分离的):

  • 本地服务 Local Service 用于应用程序内部。

    它可以启动并运行,直至有人停止了它或它自己停止。在这种方式下,它以调用Context.startService()启动,而以调用 Context.stopService()结束。它可以调用Service.stopSelf() 或 Service.stopSelfResult()来自己停止。不论调用了多少次startService()方法,你只需要调用一次 stopService()来停止服务。

    用于实现应用程序自己的一些耗时任务,比如查询升级信息,并不占用应用程序比如Activity所属线程,而是单开线程后台执行,这样用户体验比较好。

  • 远程服务 Remote Service 用于android系统内部的应用程序之间。

    它可以通过自己定义并暴露出来的接口进行程序操作。客户端建立一个到服务对象的连接,并通过那个连接来调用服务。连接以调用 Context.bindService()方法建立,以调用 Context.unbindService()关闭。多个客户端可以绑定至同一个服务。如果服务此时还没有加载,bindService()会先加载 它。

    可被其他应用程序复用,比如天气预报服务,其他应用程序不需要再写这样的服务,调用已有的即可。

生命周期

Service的生命周期并不像Activity那么复杂,它只继承了onCreate(),onStart(),onDestroy()三个方法,当我 们第一次启动Service时,先后调用了onCreate(),onStart()这两个方法,当停止Service时,则执行onDestroy() 方法,这里需要注意的是,如果Service已经启动了,当我们再次启动Service时,不会在执行onCreate()方法,而是直接执行 onStart()方法。

而启动service,根据onStartCommand的返回值不同,有两个附加的模式:

1. START_STICKY 用于显示启动和停止service。

2. START_NOT_STICKY或START_REDELIVER_INTENT用于有命令需要处理时才运行的模式。

服务不能自己运行,需要通过调用Context.startService()或Context.bindService()方法启动服务。这两个方法都可以启动Service,但是它们的使用场合有所不同。

1. 使用startService()方法启用服务,调用者与服务之间没有关连,即使调用者退出了,服务仍然运行。

如果打算采用Context.startService()方法启动服务,在服务未被创建时,系统会先调用服务的onCreate()方法,接着调用onStart()方法。

如果调用startService()方法前服务已经被创建,多次调用startService()方法并不会导致多次创建服务,但会导致多次调用onStart()方法。

采用startService()方法启动的服务,只能调用Context.stopService()方法结束服务,服务结束时会调用onDestroy()方法。

2. 使用bindService()方法启用服务,调用者与服务绑定在了一起,调用者一旦退出,服务也就终止,大有“不求同时生,必须同时死”的特点。

onBind()只有采用Context.bindService()方法启动服务时才会回调该方法。该方法在调用者与服务绑定时被调用,当调用者与服务已经绑定,多次调用Context.bindService()方法并不会导致该方法被多次调用。

采用Context.bindService()方法启动服务时只能调用onUnbind()方法解除调用者与服务解除,服务结束时会调用onDestroy()方法。

看看官方给出的比较流程示意图:

两种比较

官方文档告诉我们,一个service可以同时start并且bind,在这样的情况,系统会一直保持service的运行状态如果service已经 start了或者BIND_AUTO_CREATE标志被设置。如果没有一个条件满足,那么系统将会调用onDestory方法来终止service.所 有的清理工作(终止线程,反注册接收器)都在onDestory中完成。

拥有service的进程具有较高的优先级

官方文档告诉我们,Android系统会尽量保持拥有service的进程运行,只要在该service已经被启动(start)或者客户端连接(bindService)到它。当内存不足时,需要保持,拥有service的进程具有较高的优先级。

1. 如果service正在调用onCreate,onStartCommand或者onDestory方法,那么用于当前service的进程则变为前台进程以避免被killed。

2. 如果当前service已经被启动(start),拥有它的进程则比那些用户可见的进程优先级低一些,但是比那些不可见的进程更重要,这就意味着service一般不会被killed.

3. 如果客户端已经连接到service (bindService),那么拥有Service的进程则拥有最高的优先级,可以认为service是可见的。

4. 如果service可以使用startForeground(int, Notification)方法来将service设置为前台状态,那么系统就认为是对用户可见的,并不会在内存不足时killed。

如果有其他的应用组件作为Service,Activity等运行在相同的进程中,那么将会增加该进程的重要性。

本地service

1.不需和Activity交互的本地服务

  1. public class LocalService extends Service {
  2. private static final String TAG = "LocalService";
  3. @Override
  4. public IBinder onBind(Intent intent) {
  5. Log.i(TAG, "onBind");
  6. return null;
  7. }
  8. @Override
  9. public void onCreate() {
  10. Log.i(TAG, "onCreate");
  11. super.onCreate();
  12. }
  13. @Override
  14. public void onDestroy() {
  15. Log.i(TAG, "onDestroy");
  16. super.onDestroy();
  17. }
  18. @Override
  19. public void onStart(Intent intent, int startId) {
  20. Log.i(TAG, "onStart");
  21. super.onStart(intent, startId);
  22. }
  23. }
  24. Activity:
  25. public class ServiceActivity extends Activity {
  26. @Override
  27. protected void onCreate(Bundle savedInstanceState) {
  28. super.onCreate(savedInstanceState);
  29. setContentView(R.layout.servicedemo);
  30. ((Button) findViewById(R.id.startLocalService)).setOnClickListener(
  31. new View.OnClickListener(){
  32. @Override
  33. public void onClick(View view) {
  34. // TODO Auto-generated method stub
  35. startService(new Intent("com.demo.SERVICE_DEMO"));
  36. }
  37. });
  38. ((Button) findViewById(R.id.stopLocalService)).setOnClickListener(
  39. new View.OnClickListener(){
  40. @Override
  41. public void onClick(View view) {
  42. // TODO Auto-generated method stub
  43. stopService(new Intent("com.demo.SERVICE_DEMO"));
  44. }
  45. });
  46. }
  47. }

在AndroidManifest.xml添加:

  1. < service android:name =".LocalService" >
  2. < intent-filter >
  3. < action android:name ="com.demo.SERVICE_DEMO" />
  4. < category android:name ="android.intent.category.default" />
  5. intent-filter >
  6. service >

否则启动服务时会提示new Intent找不到"com.demo.SERVICE_DEMO"。

对于这类不需和Activity交互的本地服务,是使用startService/stopService的最好例子。

运行时可以发现第一次startService时,会调用onCreate和onStart,在没有stopService前,无论点击多少次 startService,都只会调用onStart。而stopService时调用onDestroy。再次点击stopService,会发现不会 进入service的生命周期的,即不会再调用onCreate,onStart和onDestroy。

而onBind在startService/stopService中没有调用。

2.本地服务和Activity交互

对于这种case,官方的sample(APIDemo\app.LocalService)是最好的例子:

  1. /**
  2. * This is an example of implementing an application service that runs locally
  3. * in the same process as the application. The {@link LocalServiceController}
  4. * and {@link LocalServiceBinding} classes show how to interact with the
  5. * service.
  6. *
  7. *

    Notice the use of the {@link NotificationManager} when interesting things

  8. * happen in the service. This is generally how background services should
  9. * interact with the user, rather than doing something more disruptive such as
  10. * calling startActivity().
  11. */
  12. public class LocalService extends Service {
  13. private NotificationManager mNM;
  14. /**
  15. * Class for clients to access. Because we know this service always
  16. * runs in the same process as its clients, we don't need to deal with
  17. * IPC.
  18. */
  19. public class LocalBinder extends Binder {
  20. LocalService getService() {
  21. return LocalService.this;
  22. }
  23. }
  24. @Override
  25. public void onCreate() {
  26. mNM = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
  27. // Display a notification about us starting. We put an icon in the status bar.
  28. showNotification();
  29. }
  30. @Override
  31. public int onStartCommand(Intent intent, int flags, int startId) {
  32. Log.i( "LocalService", "Received start id " + startId + ": " + intent);
  33. // We want this service to continue running until it is explicitly
  34. // stopped, so return sticky.
  35. return START_STICKY;
  36. }
  37. @Override
  38. public void onDestroy() {
  39. // Cancel the persistent notification.
  40. mNM.cancel(R.string.local_service_started);
  41. // Tell the user we stopped.
  42. Toast.makeText( this, R.string.local_service_stopped, Toast.LENGTH_SHORT).show();
  43. }
  44. @Override
  45. public IBinder onBind(Intent intent) {
  46. return mBinder;
  47. }
  48. // This is the object that receives interactions from clients. See
  49. // RemoteService for a more complete example.
  50. private final IBinder mBinder = new LocalBinder();
  51. /**
  52. * Show a notification while this service is running.
  53. */
  54. private void showNotification() {
  55. // In this sample, we'll use the same text for the ticker and the expanded notification
  56. CharSequence text = getText(R.string.local_service_started);
  57. // Set the icon, scrolling text and timestamp
  58. Notification notification = new Notification(R.drawable.stat_sample, text,
  59. System.currentTimeMillis());
  60. // The PendingIntent to launch our activity if the user selects this notification
  61. PendingIntent contentIntent = PendingIntent.getActivity( this, 0,
  62. new Intent( this, LocalServiceController. class), 0);
  63. // Set the info for the views that show in the notification panel.
  64. notification.setLatestEventInfo( this, getText(R.string.local_service_label),
  65. text, contentIntent);
  66. // Send the notification.
  67. // We use a layout id because it is a unique number. We use it later to cancel.
  68. mNM.notify(R.string.local_service_started, notification);
  69. }
  70. }

这里可以发现onBind需要返回一个IBinder对象。也就是说和上一例子LocalService不同的是,

1. 添加了一个public内部类继承Binder,并添加getService方法来返回当前的Service对象;

2. 新建一个IBinder对象——new那个Binder内部类;

3. onBind方法返还那个IBinder对象。

  1. /**
  2. *

    Example of binding and unbinding to the {@link LocalService}.

  3. * This demonstrates the implementation of a service which the client will
  4. * bind to, receiving an object through which it can communicate with the service.

  5. */
  6. public class LocalServiceBinding extends Activity {
  7. private boolean mIsBound;
  8. private LocalService mBoundService;
  9. @Override
  10. protected void onCreate(Bundle savedInstanceState) {
  11. super.onCreate(savedInstanceState);
  12. setContentView(R.layout.local_service_binding);
  13. // Watch for button clicks.
  14. Button button = (Button)findViewById(R.id.bind);
  15. button.setOnClickListener(mBindListener);
  16. button = (Button)findViewById(R.id.unbind);
  17. button.setOnClickListener(mUnbindListener);
  18. }
  19. private ServiceConnection mConnection = new ServiceConnection() {
  20. public void onServiceConnected(ComponentName className, IBinder service) {
  21. // This is called when the connection with the service has been
  22. // established, giving us the service object we can use to
  23. // interact with the service. Because we have bound to a explicit
  24. // service that we know is running in our own process, we can
  25. // cast its IBinder to a concrete class and directly access it.
  26. mBoundService = ((LocalService.LocalBinder)service).getService();
  27. // Tell the user about this for our demo.
  28. Toast.makeText(LocalServiceBinding. this, R.string.local_service_connected,
  29. Toast.LENGTH_SHORT).show();
  30. }
  31. public void onServiceDisconnected(ComponentName className) {
  32. // This is called when the connection with the service has been
  33. // unexpectedly disconnected -- that is, its process crashed.
  34. // Because it is running in our same process, we should never
  35. // see this happen.
  36. mBoundService = null;
  37. Toast.makeText(LocalServiceBinding. this, R.string.local_service_disconnected,
  38. Toast.LENGTH_SHORT).show();
  39. }
  40. };
  41. private OnClickListener mBindListener = new OnClickListener() {
  42. public void onClick(View v) {
  43. // Establish a connection with the service. We use an explicit
  44. // class name because we want a specific service implementation that
  45. // we know will be running in our own process (and thus won't be
  46. // supporting component replacement by other applications).
  47. bindService( new Intent(LocalServiceBinding. this,
  48. LocalService. class), mConnection, Context.BIND_AUTO_CREATE);
  49. mIsBound = true;
  50. }
  51. };
  52. private OnClickListener mUnbindListener = new OnClickListener() {
  53. public void onClick(View v) {
  54. if (mIsBound) {
  55. // Detach our existing connection.
  56. unbindService( mConnection);
  57. mIsBound = false;
  58. }
  59. }
  60. };
  61. }

明显看出这里面添加了一个名为ServiceConnection类,并实现了onServiceConnected(从IBinder获取Service对象)和onServiceDisconnected(set Service to null)。

而bindService和unbindService方法都是操作这个ServiceConnection对象的。

AndroidManifest.xml里添加:

  1. < service android:name =".app.LocalService" />
  2. < activity android:name =".app.LocalServiceBinding" android:label ="@string/activity_local_service_binding" >
  3. < intent-filter >
  4. < action android:name ="android.intent.action.MAIN" />
  5. < category android:name ="android.intent.category.SAMPLE_CODE" />
  6. intent-filter >
  7. activity >

这里没什么特别的,因为service没有需要什么特别的action,所以只是声明service而已,而activity和普通的没差别。

运行时,发现调用次序是这样的:

bindService:

1.LocalService : onCreate

2.LocalService : onBind

3.Activity: onServiceConnected

unbindService: 只是调用onDestroy

可见,onStart是不会被调用的,而onServiceDisconnected没有调用的原因在上面代码的注释有说明。

介绍onStartCommand()需要用到的几个常量 (引自官方文档)

START_NOT_STICKY

If the system kills the service after onStartCommand() returns, do not recreate the service, unless there are pending intents to deliver. This is the safest option to avoid running your service when not necessary and when your application can simply restart any unfinished jobs.

START_STICKY

If the system kills the service after onStartCommand() returns, recreate the service and call onStartCommand(), but do not redeliver the last intent. Instead, the system calls onStartCommand() with a null intent, unless there were pending intents to start the service, in which case, those intents are delivered. This is suitable for media players (or similar services) that are not executing commands, but running indefinitely and waiting for a job.

START_REDELIVER_INTENT

If the system kills the service after onStartCommand() returns, recreate the service and call onStartCommand() with the last intent that was delivered to the service. Any pending intents are delivered in turn. This is suitable for services that are actively performing a job that should be immediately resumed, such as do wnloading a file.

Running a Service in the Foreground

具体内容查看官方文档,主要是使用 startForeground() 和 stopForeground()方法。

责任编辑:徐川 来源: OSChina
相关推荐

2017-01-01 09:43:40

2011-02-25 10:03:06

Proftpd

2010-09-30 09:42:06

2022-04-11 06:56:14

数据库安全数据泄露

2011-03-03 17:01:17

Vsftpd

2018-04-20 07:52:23

2010-08-05 11:25:52

Windows Ser迁移

2012-12-27 10:30:46

Android开发Service后台服务

2010-02-06 09:32:18

Android Ser

2014-09-16 11:17:36

AndroidService交互方式

2009-12-18 09:53:20

Android

2023-08-03 08:36:30

Service服务架构

2021-09-09 15:07:36

鸿蒙HarmonyOS应用

2011-06-01 10:58:54

Android Service

2021-05-27 10:36:41

网络通信内核

2014-02-26 13:08:48

AndroidService教程

2016-07-13 14:32:36

云计算

2023-11-06 13:17:53

ServiceAndroid

2016-12-09 09:23:50

android组件Service

2023-10-12 00:07:27

Service单体微服务
点赞
收藏

51CTO技术栈公众号