自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

詳細(xì)描述Android服務(wù)解說(shuō)

移動(dòng)開(kāi)發(fā) Android
Android服務(wù)中有幾個(gè)重要的組件,其中之一就是Service,這是沒(méi)有UI的組件,可以做為后臺(tái)的服務(wù),當(dāng)然可以使用Intent來(lái)啟動(dòng)。

開(kāi)放手機(jī)聯(lián)盟的成立和 Android服務(wù) 的推出是對(duì)現(xiàn)狀的重大改變,在帶來(lái)初步效益之前,還需要不小的耐心和高昂的投入,谷歌將繼續(xù)努力,讓這些服務(wù)變得更好,同時(shí)也將添加更有吸引力的特性、應(yīng)用和服務(wù)。

一,Android服務(wù)中的Service與調(diào)用者在同一線程,所以要是耗時(shí)的操作要在Service中新開(kāi)線程。
二,Android的Service中,主要是實(shí)現(xiàn)其onCreate,onStart, onDestroy,onBind,onUnBind幾個(gè)函數(shù),來(lái)實(shí)現(xiàn)我們所需要的功能。

簡(jiǎn)單的調(diào)可以在調(diào)用者對(duì)象中使用Context.startService來(lái)調(diào)用,以Intent為參數(shù),當(dāng)然,Intent搜索,匹配目標(biāo)的方式與以前在《Intent使用》中方式一樣。

下面來(lái)看一段例程:

  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. }  

編譯執(zhí)行,你會(huì)發(fā)現(xiàn),是先執(zhí)行onCreate,然后再執(zhí)行onBind,在調(diào)用者的Context.bindService返回時(shí),ServiceConnection的OnConnected并沒(méi)有馬上被執(zhí)行。Android服務(wù)遠(yuǎn)程綁定:上述綁定是在調(diào)用者與Service在同一個(gè)應(yīng)用程序中的情況,如果分處在不同的程序中,那么,調(diào)用方式又是一另一種情況。我們來(lái)看一下。

【編輯推薦】

  1. Android應(yīng)用程序組建原理深入剖析
  2. Android SMS短信服務(wù)相關(guān)概念簡(jiǎn)述
  3. PythonAndroid數(shù)據(jù)庫(kù)相關(guān)代碼解讀
  4. PythonAndroid安裝卸載程序具體操作方法解析
  5. Android應(yīng)用程序的四個(gè)關(guān)鍵點(diǎn)
責(zé)任編輯:chenqingxiang 來(lái)源: 清華大學(xué)出版社
相關(guān)推薦

2009-09-14 13:14:49

LINQ序列

2009-09-14 14:58:52

LINQ to XML

2009-10-13 17:16:40

VB.NET Web服

2009-09-14 16:33:55

LINQ To XML

2009-09-24 16:19:53

Hibernate應(yīng)用

2009-09-25 14:28:40

Hibernate S

2009-11-18 11:14:49

2009-09-08 11:09:39

LINQ數(shù)據(jù)源

2009-08-10 16:40:03

C#索引器

2009-09-25 11:04:32

Hibernate3實(shí)

2010-04-09 17:45:06

Oracle索引

2009-10-15 14:59:45

網(wǎng)絡(luò)布線光纖技術(shù)

2009-08-26 15:53:48

C#擴(kuò)展方法

2009-08-27 15:17:40

C# const變量

2009-09-07 15:15:43

2009-10-10 10:04:50

RHEL合法使用

2010-09-08 15:10:48

2009-10-16 11:02:40

VB調(diào)用動(dòng)態(tài)連接庫(kù)

2009-10-12 12:54:58

VB.NET聲明API

2009-09-03 17:59:18

C#調(diào)用事件
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)