詳細描述Android服務解說

來源:互聯網
上載者:User

開放手機同盟成立和 Android服務 的推出是對現狀的重大改變,在帶來初步效益之前,還需要不小的耐心和高昂的投入,Google將繼續努力,讓這些服務變得更好,同時也將添加更有吸引力的特性、應用和服務。

一,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在同一個應用程式中的情況,如果分處在不同的程式中,那麼,調用方式又是一另一種情況。我們來看一下。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.