Service组件

   Service组件能够看做是没有界面的Activity组件,两者地位相同。它是运行在系统后台的一种服务,通常处理耗时较长的操做,不与用户进行交互。和其余组件同样,Service组件一样须要在AndroidManifest.xml中声明,在<service>中能够添加过滤器指定如何如何访问该Service。android

 

      
      
               
      
      
  1. <service android:name="MyService">  
  2.             <intent-filter>  
  3.      <action android:name="com.amaker.ch07.app.action.MY_SERVICE"/>  
  4.             </intent-filter>  
  5.         </service>  

  Service和整个应用程序在同一个进程和同一个线程中,并不是单独的进程和线程。它的运行会阻塞其余操做。
  Service的用处有两点,其一是在后台运行耗时较长且不与用户进行交互的操做;其二是经过绑定,与其余进程进行通讯。

1. 建立Service
  首先须要定义一个继承于Service的类,而后覆盖其中的的方法便可。代码以下:
app

 

      
      
               
      
      
  1. public class MyService extends Service{  
  2.  
  3. // 能够返回null,一般返回一个有aidl定义的接口,必须实现  
  4. public IBinder onBind(Intent intent) {  
  5. Log.i("SERVICE""onBind..............");  
  6. Toast.makeText(MyService.this"onBind..............", Toast.LENGTH_LONG).show();  
  7. return null;  
  8. }  
  9. // Service建立时调用  
  10. public void onCreate() {  
  11. Log.i("SERVICE""onCreate..............");  
  12. Toast.makeText(MyService.this"onCreate..............", Toast.LENGTH_LONG).show();  
  13. }  
  14. // 当客户端调用startService()方法启动Service时,该方法被调用  
  15. public void onStartCommond(Intent intent, int flag, int startId) {  
  16. Log.i("SERVICE""onStart..............");  
  17. Toast.makeText(MyService.this"onStart..............", Toast.LENGTH_LONG).show();  
  18. }  
  19. // 当Service再也不使用时调用  
  20. public void onDestroy() {  
  21. Log.i("SERVICE""onDestroy..............");  
  22. Toast.makeText(MyService.this"onDestroy..............", Toast.LENGTH_LONG).show();  
  23. }  
  24. }  

2. 启动和中止Service
  一旦定义好一个Service,就能够在其余组件中启动并中止该Service了。代码以下:
ide

 

      
      
               
      
      
  1. //启动Service
  2. // 建立Intent  
  3. Intent intent = new Intent();  
  4. // 设置Action属性  
  5. intent.setAction("com.amaker.ch07.app.action.MY_SERVICE");  
  6. // 启动该Service  
  7. startService(intent);  
  8.  
  9.  // 中止Service
  10. // 建立Intent  
  11. Intent intent = new Intent();  
  12. // 设置Action属性  
  13. intent.setAction("com.amaker.ch07.app.action.MY_SERVICE");  
  14. // 启动该Service  
  15. stopService(intent);  
  16.  

3. 绑定一个已经存在的Service
  绑定Service和启动它的区别在于,绑定服务通常用于远程调用。若是服务没有被建立,首先会调用onCreate方法,可是不会调用onStrart方法,而是调用onBind返回客户端一个IBind接口。
  绑定服务的代码以下:
this

 

      
      
               
      
      
  1. // 链接对象  
  2. private ServiceConnection conn = new ServiceConnection() {  
  3. @Override 
  4. public void onServiceConnected(ComponentName name, IBinder service) {  
  5. Log.i("SERVICE""链接成功!");  
  6. Toast.makeText(MainActivity.this"链接成功!", Toast.LENGTH_LONG).show();  
  7. }  
  8. @Override 
  9. public void onServiceDisconnected(ComponentName name) {  
  10. Log.i("SERVICE""断开链接!");  
  11. Toast.makeText(MainActivity.this"断开链接!", Toast.LENGTH_LONG).show();  
  12. }  
  13. };  
  14.  
  15. // 綁定Service监听器  
  16. private OnClickListener bindListener = new OnClickListener() {  
  17. @Override 
  18. public void onClick(View v) {  
  19. // 建立Intent  
  20. Intent intent = new Intent();  
  21. // 设置Action属性  
  22. intent.setAction("com.amaker.ch07.app.action.MY_SERVICE");  
  23.  
  24. // 绑定Service  
  25. bindService(intent, conn, Service.BIND_AUTO_CREATE);  
  26. }  
  27. };  
  28.  
  29.  
  30. // 解除绑定Service监听器  
  31. private OnClickListener unBindListener = new OnClickListener() {  
  32. @Override 
  33. public void onClick(View v) {  
  34. // 建立Intent  
  35. Intent intent = new Intent();  
  36. // 设置Action属性  
  37. intent.setAction("com.amaker.ch07.app.action.MY_SERVICE");  
  38. // 解除绑定Service  
  39. unbindService(conn);  
  40. }  
  41. };  
  42.  
  43. }  
 
相关文章
相关标签/搜索