詳細(xì)描述Android服務(wù)解說(shuō)
開(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)看一段例程:
- package test.pHello;
- import android.app.Activity;
- import android.content.ComponentName;
- import android.content.Context;
- import android.content.Intent;
- import android.content.ServiceConnection;
- import android.net.Uri;
- import android.os.Bundle;
- import android.os.IBinder;
- import android.view.Menu;
- import android.view.MenuItem;
- import android.widget.TextView;
- public class HelloActivity extends Activity {
- ITestService mService = null;
- ServiceConnection sconnection = new ServiceConnection()
- {
- public void onServiceConnected(ComponentName name, IBinder service)
- {
- mService = (ITestService)service;
- if (mService != null)
- {
- mService.showName();
- }
- }
- public void onServiceDisconnected(ComponentName name)
- {
- }
- };
- @Override
- public boolean onCreateOptionsMenu(Menu menu) {
- // TODO Auto-generated method stub
- super.onCreateOptionsMenu(menu);
- menu.add(0, Menu.FIRST+1, 1, "OpenActivity");
- menu.add(0, Menu.FIRST+2, 2, "StartService");
- menu.add(0, Menu.FIRST+3, 3, "StopService");
- menu.add(0, Menu.FIRST+4, 4, "BindService");
- return true;
- }
- @Override
- public boolean onOptionsItemSelected(MenuItem item) {
- // TODO Auto-generated method stub
- super.onOptionsItemSelected(item);
- switch(item.getItemId())
- {
- case Menu.FIRST + 1:
- {
- this.setTitle("Switch Activity");
- Intent i = new Intent();
- i.setAction("test_action");
- if (Tools.isIntentAvailable(this,i))
- this.startActivity(i);
- else
- this.setTitle("the Intent is unavailable!!!");
- break;
- }
- case Menu.FIRST + 2:
- {
- this.setTitle("Start Service");
- //Intent i = new Intent(this, TestService.class);
- Intent i = new Intent();
- i.setAction("start_service");
- this.startService(i);
- break;
- }
- case Menu.FIRST + 3:
- {
- this.setTitle("Stop Service");
- Intent i = new Intent(this, TestService.class);
- this.stopService(i);
- break;
- }
- case Menu.FIRST + 4:
- {
- this.setTitle("Bind Service!");
- Intent i = new Intent(this, TestService.class);
- this.bindService(i, this.sconnection, Context.BIND_AUTO_CREATE);
- break;
- }
- }
- return true;
- }
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- this.setContentView(R.layout.main);
- }
- }
編譯執(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)看一下。
【編輯推薦】