[关闭]
@ZeroGeek 2018-08-03T07:51:20.000000Z 字数 4012 阅读 615

Service回顾(一)

Android知识点


定义

Service是一种可以长时间在后台运行任务的应用组件,不提供用户交互。

不同类型的Service

注意

简单使用Service

必须在manifest里面声明service

1.继承Service

2.继承IntentService

onStartCommand()的返回值

这个值决定,当系统杀死该Service时,如何继续处理。

START_NOT_STICKY:不重建
START_STICKY:保证Service可重建,适用于媒体播放
START_REDELIVER_INTENT:适用于下载文件

启动Service

  1. Intent intent = new Intent(this, TimeService.class);
  2. startService(intent);

保证Service不被杀死

创建notification

  1. Intent notificationIntent = new Intent(this, ExampleActivity.class);
  2. PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
  3. Notification notification = new Notification.Builder(this)
  4. .setContentTitle(getText(R.string.notification_title))
  5. .setContentText(getText(R.string.notification_message))
  6. .setSmallIcon(R.drawable.icon)
  7. .setContentIntent(pendingIntent)
  8. .setTicker(getText(R.string.ticker_text))
  9. .build();
  10. startForeground(ONGOING_NOTIFICATION_ID, notification);

注意:ONGOING_NOTIFICATION_ID一定不能为0.
可通过stopForeground()移出前台任务

生命周期,有两种路径

看图

Bound Services

提供一种C-S模式,允许进程间通信。
注意:Service可以同时启动和绑定

交互方式

1.继承Binder

如果不需要跨进程通信,可以使用继承Binder的方式,得到Service的实例,在Service中提供公有方法给其它组件调用。
简单示例:

  1. public class LocalService extends Service {
  2. private final IBinder mBinder = new LocalBinder();
  3. @Nullable
  4. @Override
  5. public IBinder onBind(Intent intent) {
  6. return mBinder;
  7. }
  8. public class LocalBinder extends Binder {
  9. LocalService getService () {
  10. return LocalService.this;
  11. }
  12. }
  13. // 提供公有方法
  14. public void show() {
  15. Toast.makeText(getApplicationContext(), "OK!", Toast.LENGTH_SHORT).show();
  16. }
  17. }

Activity中

  1. private LocalService mService;
  2. private boolean mBound;
  3. @Override
  4. protected void onStart() {
  5. super.onStart();
  6. // 注意这里
  7. Intent intent = new Intent(this, LocalService.class);
  8. bindService(intent, mConnection, Service.BIND_AUTO_CREATE);
  9. }
  10. @Override
  11. protected void onStop() {
  12. super.onStop();
  13. if (mBound) {
  14. unbindService(mConnection);
  15. mBound = false;
  16. }
  17. }
  18. // 所有与Service的连接都是通过ServiceConnection
  19. private ServiceConnection mConnection = new ServiceConnection() {
  20. @Override
  21. public void onServiceConnected(ComponentName name, IBinder service) {
  22. LocalService.LocalBinder binder = (LocalService.LocalBinder) service;
  23. mService = binder.getService();
  24. mBound = true;
  25. }
  26. @Override
  27. public void onServiceDisconnected(ComponentName name) {
  28. mBound = false;
  29. }
  30. };

之后可以用mService去处理任务了。别忘了声明Service。

2.使用Messenger

  1. public class MessengerService extends Service {
  2. static final int MSG_SAY_HELLO = 1;
  3. class IncomingHandler extends Handler {
  4. @Override
  5. public void handleMessage(Message msg) {
  6. switch (msg.what) {
  7. case MSG_SAY_HELLO:
  8. // do something
  9. break;
  10. default:
  11. super.handleMessage(msg);
  12. }
  13. }
  14. }
  15. final Messenger mMessenger = new Messenger(new IncomingHandler());
  16. @Override
  17. public IBinder onBind(Intent intent) {
  18. return mMessenger.getBinder();
  19. }
  20. }
  1. Messenger mService = null;
  2. boolean mBound;
  3. private ServiceConnection mConnection = new ServiceConnection() {
  4. public void onServiceConnected(ComponentName className, IBinder service) {
  5. mService = new Messenger(service);
  6. mBound = true;
  7. }
  8. public void onServiceDisconnected(ComponentName className) {
  9. mService = null;
  10. mBound = false;
  11. }
  12. };
  13. public void sayHello(View v) {
  14. if (!mBound) return;
  15. // Create and send a message to the service, using a supported 'what' value
  16. Message msg = Message.obtain(null, MessengerService.MSG_SAY_HELLO, 0, 0);
  17. try {
  18. mService.send(msg);
  19. } catch (RemoteException e) {
  20. e.printStackTrace();
  21. }
  22. }
  23. @Override
  24. protected void onStart() {
  25. super.onStart();
  26. bindService(new Intent(this, MessengerService.class), mConnection,
  27. Context.BIND_AUTO_CREATE);
  28. }
  29. @Override
  30. protected void onStop() {
  31. super.onStop();
  32. if (mBound) {
  33. unbindService(mConnection);
  34. mBound = false;
  35. }
  36. }

2种用法都差不多,但是Messenger支持IPC,不支持多线程

pay

  1. Caution: If you use an intent to bind to a Service, ensure that your app is secure by using an explicit intent. Using an implicit intent to start a service is a security hazard because you can't be certain what service will respond to the intent, and the user can't see which service starts. Beginning with Android 5.0 (API level 21), the system throws an exception if you call bindService() with an implicit intent.

关闭的方法

添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注