[关闭]
@linux1s1s 2019-02-14T15:26:03.000000Z 字数 4307 阅读 3067

Android Service

AndroidComponent 2015-05


Service

啥也不说直接上代码

  1. public class AppService extends Service
  2. {
  3. private static final String TAG = "ExampleService";
  4. private final AppBinder mAppBinder = new AppService.AppBinder();
  5. @Override
  6. public IBinder onBind(Intent intent)
  7. {
  8. return mAppBinder;
  9. }
  10. @Override
  11. public void onCreate()
  12. {
  13. Log.i(TAG, "ExampleService.onCreate()");
  14. super.onCreate();
  15. }
  16. @Override
  17. public void onStart(Intent intent, int startId)
  18. {
  19. Log.i(TAG, "ExampleService.onStart()");
  20. super.onStart(intent, startId);
  21. }
  22. @Override
  23. public int onStartCommand(Intent intent, int flags, int startId)
  24. {
  25. Log.i(TAG, "ExampleService.onStartCommand()");
  26. return super.onStartCommand(intent, flags, startId);
  27. }
  28. @Override
  29. public void onDestroy()
  30. {
  31. Log.i(TAG, "ExampleService.onDestroy()");
  32. super.onDestroy();
  33. }
  34. public class AppBinder extends Binder
  35. {
  36. AppService getService()
  37. {
  38. return AppService.this;
  39. }
  40. }
  41. }

上面代码片段是一个Service,在生命周期位置标记了一下,既然Service已经生成好了,下面看一下如何使用

使用Service

startService,stopService

  1. public class MainActivity extends ActionBarActivity
  2. {
  3. private BroadcastReceiver mReceive;
  4. private final static String RECEIVE_KEY = "MY_RECEIVE_KEY";
  5. private final Intent mServiceIntent = new Intent(MainActivity.this, AppService.class);
  6. @Override
  7. protected void onCreate(Bundle savedInstanceState)
  8. {
  9. super.onCreate(savedInstanceState);
  10. setContentView(R.layout.activity_main);
  11. initContentView();
  12. mReceive = new MyReceive(mServiceIntent);
  13. IntentFilter filter = new IntentFilter();
  14. filter.addAction(RECEIVE_KEY);
  15. registerReceiver(mReceive, filter);
  16. }
  17. private static class MyReceive extends BroadcastReceiver
  18. {
  19. private Intent it;
  20. MyReceive(Intent it)
  21. {
  22. this.it = it;
  23. }
  24. @Override
  25. public void onReceive(Context context, Intent intent)
  26. {
  27. if (intent.getAction().equalsIgnoreCase(RECEIVE_KEY))
  28. {
  29. context.startService(it);
  30. }
  31. }
  32. }
  33. @Override
  34. protected void onDestroy()
  35. {
  36. super.onDestroy();
  37. if (mReceive != null)
  38. {
  39. try
  40. {
  41. unregisterReceiver(mReceive);
  42. }
  43. catch (Exception e)
  44. {
  45. e.printStackTrace();
  46. }
  47. mReceive = null;
  48. }
  49. stopService(mServiceIntent);
  50. }
  51. private void initContentView()
  52. {
  53. final View textView = findViewById(R.id.main_activity_text_view);
  54. if (textView != null)
  55. {
  56. textView.setOnClickListener(new OnClickListener()
  57. {
  58. @Override
  59. public void onClick(View v)
  60. {
  61. Intent itReceive = new Intent(RECEIVE_KEY);
  62. sendBroadcast(itReceive);
  63. }
  64. });
  65. }
  66. }
  67. }

bindService,unbindService

  1. public class MainActivity extends ActionBarActivity
  2. {
  3. private static AppServiceConnection SC = new AppServiceConnection();
  4. private static class AppServiceConnection implements ServiceConnection
  5. {
  6. @Override
  7. public void onServiceConnected(ComponentName name, IBinder service)
  8. {
  9. AppService s = ((AppService.AppBinder) service).getService();
  10. //TODO
  11. }
  12. @Override
  13. public void onServiceDisconnected(ComponentName name)
  14. {
  15. //TO show a message to custom;
  16. }
  17. }
  18. private static class MyReceive extends BroadcastReceiver
  19. {
  20. private Intent it;
  21. MyReceive(Intent it)
  22. {
  23. this.it = it;
  24. }
  25. @Override
  26. public void onReceive(Context context, Intent intent)
  27. {
  28. if (intent.getAction().equalsIgnoreCase(RECEIVE_KEY))
  29. {
  30. context.bindService(it, SC, Context.BIND_AUTO_CREATE);
  31. }
  32. }
  33. }
  34. @Override
  35. protected void onDestroy()
  36. {
  37. super.onDestroy();
  38. if (SC != null)
  39. {
  40. unbindService(SC);
  41. SC = null;
  42. }
  43. }
  44. }

配置Manifest

  1. <service
  2. android:name="studio.neulion.com.tasktest.AppService" />

运行

此处输入图片的描述

如果我们使用第一种方式startService,而在Activity的时候没有主动stopService,当我们退出App的时候会怎样?

Service还在运行

然后回到我们的Activity,再次点击启动Service,控制台输出日志如下:

此处输入图片的描述

onCreate()方法没有被调用,说明它并没有重新被创建。
然后我们修改一下代码,在onDestory方法中添加stopService,再退出后输出日志如下:

此处输入图片的描述

小结

startService()与bindService()区别:

另外说一下:开发人员需要在应用程序配置文件中声明全部的service,使用标签。
多说一句,对于Android的四大组件:

四大基本组件都需要注册才能使用,每个Activity、service、Content Provider都需要在AndroidManifest文件中进行配置。AndroidManifest文件中未进行声明的activity、服务以及内容提供者将不为系统所见,从而也就不可用。而broadcast receiver广播接收者的注册分静态注册(在AndroidManifest文件中进行配置)和通过代码动态创建并以调用Context.registerReceiver()的方式注册至系统。需要注意的是在AndroidManifest文件中进行配置的广播接收者会随系统的启动而一直处于活跃状态,只要接收到感兴趣的广播就会触发(即使程序未运行)。

最后说一下bindService需要的参数flag含义:

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