详细描述Android服务解说


  本文标签:Android服务

  开放手机联盟的成立和 Android服务 的推出是对现状的重大改变,在带来初步效益之前,还需要不小的耐心和高昂的投入,谷歌将继续努力,让这些服务变得更好,同时也将添加更有吸引力的特性、应用和服务  。

  一,Android服务中的Service与调用者在同一线程,所以要是耗时的操作要在Service中新开线程  。
二,Android的Service中,主要是实现其onCreate,onStart, onDestroy,onBind,onUnBind几个函数,来实现我们所需要的功能  。

  简单的调可以在调用者对象中使用Context.startService来调用,以Intent为参数,当然,Intent搜索,匹配目标的方式与以前在《Intent使用》中方式一样  。

  下面来看一段例程:

  1. package test.pHello;  
  2.  
  3. import android.app.Activity;  
  4. import android.content.ComponentName;  
  5. import android.content.Context;  
  6. import android.content.Intent;  
  7. import android.content.ServiceConnection;  
  8. import android.net.Uri;  
  9. import android.os.Bundle;  
  10. import android.os.IBinder;  
  11. import android.view.Menu;  
  12. import android.view.MenuItem;  
  13. import android.widget.TextView;  
  14.  
  15. public class HelloActivity extends Activity {   
  16.    
  17.  ITestService mService = null;  
  18.  ServiceConnection sconnection = new ServiceConnection()  
  19.  {  
  20.   public void onServiceConnected(ComponentName name, IBinder service)  
  21.   {  
  22.    mService  = (ITestService)service;  
  23.    if (mService != null)  
  24.    {  
  25.     mService.showName();  
  26.    }  
  27.   }  
  28.  
  29.   public void onServiceDisconnected(ComponentName name)   
  30.   {     
  31.      
  32.   }  
  33.  };  
  34.  @Override  
  35.  public boolean onCreateOptionsMenu(Menu menu) {  
  36.   // TODO Auto-generated method stub  
  37.   super.onCreateOptionsMenu(menu);  
  38.   menu.add(0, Menu.FIRST+1, 1, "OpenActivity");  
  39.   menu.add(0, Menu.FIRST+2, 2, "StartService");  
  40.   menu.add(0, Menu.FIRST+3, 3, "StopService");  
  41.   menu.add(0, Menu.FIRST+4, 4, "BindService");  
  42.   return true;  
  43.  }  
  44.  
  45.  @Override  
  46.  public boolean onOptionsItemSelected(MenuItem item) {  
  47.   // TODO Auto-generated method stub  
  48.   super.onOptionsItemSelected(item);  
  49.   switch(item.getItemId())  
  50.   {  
  51.   case Menu.FIRST + 1:  
  52.   {  
  53.    this.setTitle("Switch Activity");  
  54.    Intent i = new Intent();     
  55.    i.setAction("test_action");    
  56.    if (Tools.isIntentAvailable(this,i))  
  57.     this.startActivity(i);  
  58.    else  
  59.     this.setTitle("the Intent is unavailable!!!");  
  60.    break;  
  61.   }  
  62.   case Menu.FIRST + 2:  
  63.   {  
  64.    this.setTitle("Start Service");  
  65.    //Intent i = new Intent(this, TestService.class);  
  66.    Intent i = new Intent();  
  67.    i.setAction("start_service");  
  68.    this.startService(i);  
  69.    break;  
  70.   }  
  71.   case Menu.FIRST + 3:  
  72.   {  
  73.    this.setTitle("Stop Service");  
  74.    Intent i = new Intent(this, TestService.class);  
  75.    this.stopService(i);  
  76.    break;  
  77.   }  
  78.   case Menu.FIRST + 4:  
  79.   {  
  80.    this.setTitle("Bind Service!");  
  81.    Intent i = new Intent(this, TestService.class);  
  82.    this.bindService(i, this.sconnection, Context.BIND_AUTO_CREATE);  
  83.      
  84.    break;  
  85.   }  
  86.   }  
  87.   return true;  
  88.  }  
  89.  
  90.  @Override  
  91.     public void onCreate(Bundle savedInstanceState) {  
  92.         super.onCreate(savedInstanceState);         
  93.         this.setContentView(R.layout.main);     
  94.     }  
  95. }  

  编译执行,你会发现,是先执行onCreate,然后再执行onBind,在调用者的Context.bindService返回时,ServiceConnection的OnConnected并没有马上被执行  。Android服务远程绑定:上述绑定是在调用者与Service在同一个应用程序中的情况,如果分处在不同的程序中,那么,调用方式又是一另一种情况  。我们来看一下  。