按運行地點分類:
類別 |
區別 |
優點 |
缺點 |
應用 |
本地服務(Local) |
該服務依附在主進程上, |
服務依附在主進程上而不是獨立的進程,這樣在一定程度上節約了資源,另外Local服務因爲是在同一進程因此不需要IPC,也不需要AIDL。相應bindService會方便很多。 |
主進程被Kill後,服務便會終止。 |
非常常見的應用如:HTC的音樂播放服務,天天動聽音樂播放服務。 |
遠程服務(Remote) |
該服務是獨立的進程, |
服務爲獨立的進程,對應進程名格式爲所在包名加上你指定的android:process字符串。由於是獨立的進程,因此在Activity所在進程被Kill的時候,該服務依然在運行,不受其他進程影響,有利於爲多個進程提供服務具有較高的靈活性。 |
該服務是獨立的進程,會佔用一定資源,並且使用AIDL進行IPC稍微麻煩一點。 |
一些提供系統服務的Service,這種Service是常駐的。 |
其實remote服務還是很少見的,並且一般都是系統服務。
按運行類型分類:
類別 |
區別 |
應用 |
前臺服務 |
會在通知一欄顯示 ONGOING 的 Notification, |
當服務被終止的時候,通知一欄的 Notification 也會消失,這樣對於用戶有一定的通知作用。常見的如音樂播放服務。 |
後臺服務 |
默認的服務即爲後臺服務,即不會在通知一欄顯示 ONGOING 的 Notification。 |
當服務被終止的時候,用戶是看不到效果的。某些不需要運行或終止提示的服務,如天氣更新,日期同步,郵件同步等。 |
有同學可能會問,後臺服務我們可以自己創建ONGOING 的 Notification 這樣就成爲前臺服務嗎?答案是否定的,前臺服務是在做了上述工作之後需要調用 startForeground ( android 2.0 及其以後版本)或 setForeground (android 2.0 以前的版本)使服務成爲前臺服務。這樣做的好處在於,當服務被外部強制終止掉的時候,ONGOING 的Notification 任然會移除掉。
按使用方式分類:
類別 |
區別 |
startService 啓動的服務 |
主要用於啓動一個服務執行後臺任務,不進行通信。停止服務使用stopService |
bindService 啓動的服務 |
該方法啓動的服務要進行通信。停止服務使用unbindService |
startService 同時也 bindService 啓動的服務 |
停止服務應同時使用stepService與unbindService |
以上面三種方式啓動的服務其生命週期也有區別,將在隨後給出。
今天我們就來介紹一下Android中的四大組件中的服務Service,說到Service,
它分爲本地服務和遠程服務:區分這兩種服務就是看客戶端和服務端是否在同一個進程中,本地服務是在同一進程中的,遠程服務是不在同一個進程中的。
開啓服務也有兩種方式,一種是startService(),他對應的結束服務的方法是stopService(),另一種是bindService(),結束服務的是unBindService(),這兩種方式的區別就是:當客戶端Client使用startService方法開啓服務的時候,這個服務和Client之間就沒有聯繫了,Service的運行和Client是相互獨立的,想結束這個服務的話,就在服務本身中調用stopSelf()方法結束服務。而當客戶端Client使用bindService方法開始服務的時候,這個服務和Client是一種關聯的關係,他們之間使用Binder的代理對象進行交互,這個在後面會詳細說到,要是結束服務的話,需要在Client中和服務斷開,調用unBindService方法。
在這裏我們只做bindService方式的研究,而startService方式比較獨立和簡單,這裏就不做演示了。
首先來說一下本地服務:
本地服務很簡單的,就是Client和這個服務在同一個進程中:
先來看一下代碼吧:
下面這張圖是項目的結構圖:
爲了方便數據的訪問,這裏定義一個數據的訪問接口:
- package com.nativeservice.demo;
- /**
- * 訪問接口
- * @author weijiang204321
- */
- public interface IStudent {
- /**
- * 通過no訪問name
- * @param no
- * @return
- */
- public String getNameByNumber(int no);
- }
下面再來看一下StudentService的代碼:
- package com.nativeservice.demo;
- import android.app.Service;
- import android.content.Intent;
- import android.os.Binder;
- import android.os.IBinder;
- /**
- * 定義的服務Service
- * @author weijiang204321
- */
- public class StudentService extends Service{
- //名稱
- public static String[] nameAry = {"張飛","李小龍","趙薇"};
- /**
- * 通過no獲取name
- * @param no
- * @return
- */
- private String getNameByNo(int no){
- if(no>0 && no<4)
- return nameAry[no-1];
- return null;
- }
- @Override
- public IBinder onBind(Intent arg0) {
- return new StudentBinder();
- }
- /**
- * 自定義的Binder對象
- * @author weijiang204321
- *
- */
- private class StudentBinder extends Binder implements IStudent{
- @Override
- public String getNameByNumber(int no) {
- return getNameByNo(no);
- }
- }
- }
下面看一下Client代碼:
- package com.nativeservice.demo;
- import android.app.Activity;
- import android.content.ComponentName;
- import android.content.Intent;
- import android.content.ServiceConnection;
- import android.os.Bundle;
- import android.os.IBinder;
- import android.os.Looper;
- import android.widget.Toast;
- /**
- * 測試Service
- * @author weijiang204321
- *
- */
- public class MainActivity extends Activity {
- private IStudent student;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- //開啓查詢名稱的服務
- Intent service = new Intent(this,StudentService.class);
- bindService(service,new StudentConnection(),BIND_AUTO_CREATE);
- //延遲2s在顯示查詢的內容,不然開啓服務也是需要時間的,如果不延遲一段時間的話,student對象爲null;
- new Thread(){
- @Override
- public void run(){
- try {
- Thread.sleep(2*1000);
- Looper.prepare();
- Toast.makeText(getApplicationContext(), student.getNameByNumber(1), Toast.LENGTH_LONG).show();
- Looper.loop();
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- }
- }.start();
- }
- /**
- * 自定義的服務連接connection
- * @author weijiang204321
- *
- */
- private class StudentConnection implements ServiceConnection{
- @Override
- public void onServiceConnected(ComponentName name, IBinder service) {
- student = (IStudent)service;
- }
- @Override
- public void onServiceDisconnected(ComponentName name) {
- }
- }
- }
StudentConnection,必須實現兩個方法,這兩個方法見名思議,一個是連接時調用的方法,一個是斷開連接時的方法,在開始連接的方法onServiceConnected中傳回來一個IBinder對象,這個時候需要將其轉化一下,這個就是爲什麼要在開始的時候定義一個IStudent接口,在這裏訪問數據就方便了,同時在Client代碼中要做個延遲的操作來訪問數據,因爲開啓服務,連接這個過程是需要時間的,所以在這裏就延遲了2s,這裏只是爲了能夠正常顯示數據,才這麼做的,不然student對象是爲null的,當然要根據自己的實際情況操作。最後還要在AndroidMainfest.xml中配置Service:
- <service android:name=".StudentService"></service>
下面在來說一下遠程服務:
在說到遠程服務的時候,我們需要先了解一些預備的知識:
首先來了解一下AIDL機制:
AIDL的作用
由於每個應用程序都運行在自己的進程空間,並且可以從應用程序UI運行另一個服務進程,而且經常會在不同的進程間傳遞對象。在Android平臺,一個進程通常不能訪問另一個進程的內存空間,所以要想對話,需要將對象分解成操作系統可以理解的基本單元,並且有序的通過進程邊界。
通過代碼來實現這個數據傳輸過程是冗長乏味的,Android提供了AIDL工具來處理這項工作。
AIDL (Android Interface Definition Language) 是一種IDL 語言,用於生成可以在Android設備上兩個進程之間進行進程間通信(interprocess communication, IPC)的代碼。如果在一個進程中(例如Activity)要調用另一個進程中(例如Service)對象的操作,就可以使用AIDL生成可序列化的參數。
AIDL IPC機制是面向接口的,像COM或Corba一樣,但是更加輕量級。它是使用代理類在客戶端和實現端傳遞數據。
在Android中, 每個應用程序都有自己的進程,當需要在不同的進程之間傳遞對象時,該如何實現呢? 顯然, Java中是不支持跨進程內存共享的。因此要傳遞對象, 需要把對象解析成操作系統能夠理解的數據格式, 以達到跨界對象訪問的目的。在JavaEE中,採用RMI通過序列化傳遞對象。在Android中, 則採用AIDL(Android Interface Definition Language:接口描述語言)方式實現。
AIDL是一種接口定義語言,用於約束兩個進程間的通訊規則,供編譯器生成代碼,實現Android設備上的兩個進程間通信(IPC)。AIDL的IPC機制和EJB所採用的CORBA很類似,進程之間的通信信息,首先會被轉換成AIDL協議消息,然後發送給對方,對方收到AIDL協議消息後再轉換成相應的對象。由於進程之間的通信信息需要雙向轉換,所以android採用代理類在背後實現了信息的雙向轉換,代理類由android編譯器生成,對開發人員來說是透明的。
選擇AIDL的使用場合
官方文檔特別提醒我們何時使用AIDL是必要的:只有你允許客戶端從不同的應用程序爲了進程間的通信而去訪問你的service,以及想在你的service處理多線程。
瞭解了AIDL之後下面來看一下項目的結構:
我們這個遠程服務是想在將服務端和客戶端分別放到一個應用中,所以這裏要建立兩個Android項目一個是remoteService,一個是remoteClient
首先來看一下Service端的項目結構:
在這裏我們需要定義一個aidl文件,具體步驟很簡單的:
因爲AIDL相當於是一個接口,所以它的定義和interface的定義很類似的,使用interface關鍵字,有一點不同的是,AIDL中不能有修飾符(public,protected,private),不然報錯,這個你們可以自己嘗試一下,然後將定義好的AIDL文件的後綴名.java改成.aidl,此時在gen文件夾下面就會多出一個與之對應的java文件,這個是編譯器乾的事情。這個AIDL接口中定義的一般都是Client和Service交互的接口。
下面來看一下StudentQuery.aidl文件:
- package cn.itcast.aidl;
- //注意沒有任何的訪問權限修飾符
- interface StudentQuery {
- //通過number來訪問學生的name
- String queryStudent(int number);
- }
再來看一下服務端的代碼:
- package cn.itcast.remote.service;
- import cn.itcast.aidl.StudentQuery;
- import android.app.Service;
- import android.content.Intent;
- import android.os.IBinder;
- import android.os.RemoteException;
- /**
- * 遠程服務端
- */
- public class StudentQueryService extends Service {
- //姓名名稱
- private String[] names = {"張飛", "李靜", "趙薇"};
- private IBinder binder = new StudentQueryBinder();
- @Override
- public IBinder onBind(Intent intent) {
- return binder;
- }
- /**
- * 服務中定義的訪問方法
- * @param number
- * @return
- */
- private String query(int number){
- if(number > 0 && number < 4){
- return names[number - 1];
- }
- return null;
- }
- /**
- * 定義Binder,這裏需要繼承StudentQuery.Stub
- * StudentQuery是我們定義的AIDL
- * @author weijiang204321
- *
- */
- private final class StudentQueryBinder extends StudentQuery.Stub{
- public String queryStudent(int number) throws RemoteException {
- return query(number);
- }
- }
- }
- /*
- * This file is auto-generated. DO NOT MODIFY.
- * Original file: C:\\Users\\weijiang204321\\Desktop\\傳智播客Android視頻教程_源代碼\\remoteService\\src\\cn\\itcast\\aidl\\StudentQuery.aidl
- */
- package cn.itcast.aidl;
- //注意沒有任何的訪問權限修飾符
- public interface StudentQuery extends android.os.IInterface
- {
- /** Local-side IPC implementation stub class. */
- public static abstract class Stub extends android.os.Binder implements cn.itcast.aidl.StudentQuery
- {
- private static final java.lang.String DESCRIPTOR = "cn.itcast.aidl.StudentQuery";
- /** Construct the stub at attach it to the interface. */
- public Stub()
- {
- this.attachInterface(this, DESCRIPTOR);
- }
- /**
- * Cast an IBinder object into an cn.itcast.aidl.StudentQuery interface,
- * generating a proxy if needed.
- */
- public static cn.itcast.aidl.StudentQuery asInterface(android.os.IBinder obj)
- {
- if ((obj==null)) {
- return null;
- }
- android.os.IInterface iin = obj.queryLocalInterface(DESCRIPTOR);
- if (((iin!=null)&&(iin instanceof cn.itcast.aidl.StudentQuery))) {
- return ((cn.itcast.aidl.StudentQuery)iin);
- }
- return new cn.itcast.aidl.StudentQuery.Stub.Proxy(obj);
- }
- @Override public android.os.IBinder asBinder()
- {
- return this;
- }
- @Override public boolean onTransact(int code, android.os.Parcel data, android.os.Parcel reply, int flags) throws android.os.RemoteException
- {
- switch (code)
- {
- case INTERFACE_TRANSACTION:
- {
- reply.writeString(DESCRIPTOR);
- return true;
- }
- case TRANSACTION_queryStudent:
- {
- data.enforceInterface(DESCRIPTOR);
- int _arg0;
- _arg0 = data.readInt();
- java.lang.String _result = this.queryStudent(_arg0);
- reply.writeNoException();
- reply.writeString(_result);
- return true;
- }
- }
- return super.onTransact(code, data, reply, flags);
- }
- private static class Proxy implements cn.itcast.aidl.StudentQuery
- {
- private android.os.IBinder mRemote;
- Proxy(android.os.IBinder remote)
- {
- mRemote = remote;
- }
- @Override public android.os.IBinder asBinder()
- {
- return mRemote;
- }
- public java.lang.String getInterfaceDescriptor()
- {
- return DESCRIPTOR;
- }
- //通過number來訪問學生的name
- @Override public java.lang.String queryStudent(int number) throws android.os.RemoteException
- {
- android.os.Parcel _data = android.os.Parcel.obtain();
- android.os.Parcel _reply = android.os.Parcel.obtain();
- java.lang.String _result;
- try {
- _data.writeInterfaceToken(DESCRIPTOR);
- _data.writeInt(number);
- mRemote.transact(Stub.TRANSACTION_queryStudent, _data, _reply, 0);
- _reply.readException();
- _result = _reply.readString();
- }
- finally {
- _reply.recycle();
- _data.recycle();
- }
- return _result;
- }
- }
- static final int TRANSACTION_queryStudent = (android.os.IBinder.FIRST_CALL_TRANSACTION + 0);
- }
- //通過number來訪問學生的name
- public java.lang.String queryStudent(int number) throws android.os.RemoteException;
- }
- public static cn.itcast.aidl.StudentQuery asInterface(android.os.IBinder obj)
- {
- if ((obj==null)) {
- return null;
- }
- //如果bindService綁定的是同一進程的service,返回的是服務端Stub對象本身,那麼在客戶端是直接操作Stub對象,並不進行進程通信了
- android.os.IInterface iin = obj.queryLocalInterface(DESCRIPTOR);
- if (((iin!=null)&&(iin instanceof cn.itcast.aidl.StudentQuery))) {
- return ((cn.itcast.aidl.StudentQuery)iin);
- }
- //bindService綁定的不是同一進程的service,返回的是代理對象,obj==android.os.BinderProxy對象,被包裝成一個AIDLService.Stub.Proxy代理對象
- //不過AIDLService.Stub.Proxy進程間通信通過android.os.BinderProxy實現
- return new cn.itcast.aidl.StudentQuery.Stub.Proxy(obj);
- }
- /*
- * Copyright (C) 2006 The Android Open Source Project
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
- package android.os;
- /**
- * Base class for Binder interfaces. When defining a new interface,
- * you must derive it from IInterface.
- */
- public interface IInterface
- {
- /**
- * Retrieve the Binder object associated with this interface.
- * You must use this instead of a plain cast, so that proxy objects
- * can return the correct result.
- */
- public IBinder asBinder();
- }
- private static class Proxy implements com.demo.aidl.StudentAIDL
- {
- private android.os.IBinder mRemote;
- Proxy(android.os.IBinder remote)
- {
- mRemote = remote;
- }
- @Override public android.os.IBinder asBinder()
- {
- return mRemote;
- }
- public java.lang.String getInterfaceDescriptor()
- {
- return DESCRIPTOR;
- }
- @Override public java.lang.String queryNameByNo(int no) throws android.os.RemoteException
- {
- android.os.Parcel _data = android.os.Parcel.obtain();
- android.os.Parcel _reply = android.os.Parcel.obtain();
- java.lang.String _result;
- try {
- _data.writeInterfaceToken(DESCRIPTOR);
- _data.writeInt(no);
- mRemote.transact(Stub.TRANSACTION_queryNameByNo, _data, _reply, 0);
- _reply.readException();
- _result = _reply.readString();
- }
- finally {
- _reply.recycle();
- _data.recycle();
- }
- return _result;
- }
- }
在StudentQuery.Stub中有一個asInterface方法,這個方法中我們可以看到,如果這個Service和Client是在同一個進程中,則在Client中的StudentConnection類中返回的是IBinder就是實際的對象,如果不是在同一個進程中的話,返回的是IBinder的代理對象。
其他的問題我們暫時不看了,也不去做深入的瞭解了,
再來看一下AndroidMainfest.xml文件中配置Service:
- <service android:name=".StudentQueryService">
- <intent-filter >
- <action android:name="cn.itcast.student.query"/>
- </intent-filter>
- </service>
首先來看一下,客戶端肯定要有aidl文件,所以我們將服務端的aidl的文件拷到Client中(包括AIDL所在的包也要拷過來,刷新一下,在gen文件夾中出現了StudentQuery.java類),下面來看一下Client的代碼(MainActivity.java):
- package cn.itcast.remoteservice.client;
- import android.app.Activity;
- import android.content.ComponentName;
- import android.content.Intent;
- import android.content.ServiceConnection;
- import android.os.Bundle;
- import android.os.IBinder;
- import android.os.RemoteException;
- import android.util.Log;
- import android.view.View;
- import android.widget.EditText;
- import android.widget.TextView;
- import cn.itcast.aidl.StudentQuery;
- /**
- * 客戶端的測試代碼
- * @author weijiang204321
- *
- */
- public class MainActivity extends Activity {
- //定義控件
- private EditText numberText;
- private TextView resultView;
- private StudentQuery studentQuery;
- //定義一個連接
- private StudentConnection conn = new StudentConnection();
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- numberText = (EditText) this.findViewById(R.id.number);
- resultView = (TextView) this.findViewById(R.id.resultView);
- //這裏開啓一個Service使用隱式意圖action的名稱必須和remoteService中AndroidMainfest.xml中定義的服務的action的name一樣
- Intent service = new Intent("cn.itcast.student.query");
- bindService(service, conn, BIND_AUTO_CREATE);
- }
- /**
- * 給按鈕定義點擊事件
- * @param v
- */
- public void queryStudent(View v) {
- String number = numberText.getText().toString();
- int num = Integer.valueOf(number);
- try {
- resultView.setText(studentQuery.queryStudent(num));
- } catch (RemoteException e) {
- e.printStackTrace();
- }
- }
- @Override
- protected void onDestroy() {
- unbindService(conn);
- super.onDestroy();
- }
- /**
- * 自定義StudentConnection實現了ServiceConnection
- * @author weijiang204321
- *
- */
- private final class StudentConnection implements ServiceConnection {
- public void onServiceConnected(ComponentName name, IBinder service) {
- //這裏就用到了Stub類中的asInterface方法,將IBinder對象轉化成接口
- studentQuery = StudentQuery.Stub.asInterface(service);
- }
- public void onServiceDisconnected(ComponentName name) {
- studentQuery = null;
- }
- }
- }
客戶端和服務端的代碼結構看完了,下面我們來運行一下,首先安裝remoteService包,然後運行remoteClient包,在文本框中輸入學號,查詢到名稱了,運行成功。在不同的應用中,一定是跨進程的,不行我們可以查看一下系統的進程:
這裏我們使用adb命令查看:
http://blog.csdn.net/jiangwei0910410003/article/details/17114491
在這篇blog中我說到了怎麼使用這條命令
我們可以看到有兩個進程Proc #9和Proc #12
到這裏我們就介紹了本地服務和遠程服務的使用了,下面我們再來看一下額外的知識,就是怎麼在一個應用中進行遠程服務的訪問,我們之前涉及到的是跨應用的訪問,這裏其實很簡單,只需要改動一處就可以:下面是我改過的一個項目,在同一個應用中進行遠程服務的訪問,代碼都是一樣的,這裏就不做介紹了;
這裏需要修改的地方就是要在AndroidManifest.xml中修改一下Service的定義屬性:
- <service
- android:name="com.demo.remoteservice.StudentService"
- android:process=":remote">
- <intent-filter >
- <action android:name="com.demo.remoteservice.studentservice"/>
- </intent-filter>
- </service>
http://blog.csdn.net/jiangwei0910410003/article/details/18794945
運行結果是一樣的,爲了證明服務是遠程服務,我們在使用上面的命令打印一下當前系統中的進程信息:
系統中的Proc #9和Proc #10兩個進程,這裏就介紹了怎麼在同一個應用中跨進程訪問服務。
下面在來看一下AIDL:
我們上面說到的AIDL是說他怎麼用,而且很是簡單,我現在來具體看一下AIDL文件的定義:
Aidl默認支持的類型包話java基本類型(int、long、boolean等)和(String、List、Map、CharSequence),如果要傳遞自定義的類型該如何實現呢?
要傳遞自定義類型,首先要讓自定義類型支持parcelable協議,實現步驟如下:
1>自定義類型必須實現Parcelable接口,並且實現Parcelable接口的publicvoid writeToParcel(Parcel dest, int flags)方法 。
2>自定義類型中必須含有一個名稱爲CREATOR的靜態成員,該成員對象要求實現Parcelable.Creator接口及其方法。
3> 創建一個aidl文件聲明你的自定義類型。
Parcelable接口的作用:實現了Parcelable接口的實例可以將自身的狀態信息(狀態信息通常指的是各成員變量的值)寫入Parcel,也可以從Parcel中恢復其狀態。Parcel用來完成數據的序列化傳遞。
下面來看一下例子:
1> 創建自定義類型,並實現Parcelable接口,使其支持parcelable協議。如:在cn.itcast.domain包下創建Person.java:
- package cn.itcast.domain;
- import android.os.Parcel;
- import android.os.Parcelable;
- public class Person implements Parcelable
- privateInteger id;
- private Stringname;
- public Person(){}
- publicPerson(Integer id, String name) {
- this.id = id;
- this.name = name;
- }
- public IntegergetId() {
- return id;
- }
- public voidsetId(Integer id) {
- this.id = id;
- }
- public StringgetName() {
- return name;
- }
- public voidsetName(String name) {
- this.name = name;
- }
- @Override
- public intdescribeContents() {
- return 0;
- }
- @Override
- public voidwriteToParcel(Parcel dest, int flags) {//把javanbean中的數據寫到Parcel
- dest.writeInt(this.id);
- dest.writeString(this.name);
- }
- //添加一個靜態成員,名爲CREATOR,該對象實現了Parcelable.Creator接口
- publicstatic final Parcelable.Creator<Person> CREATOR = newParcelable.Creator<Person>(){
- @Override
- public PersoncreateFromParcel(Parcel source) {//從Parcel中讀取數據,返回person對象
- return newPerson(source.readInt(), source.readString());
- }
- @Override
- public Person[]newArray(int size) {
- return newPerson[size];
- }
- };
- }
2> 在自定義類型所在包下創建一個aidl文件對自定義類型進行聲明,文件的名稱與自定義類型同名。
3> 在接口aidl文件中使用自定義類型,需要使用import顯式導入,本例在cn.itcast.aidl包下創建IPersonService.aidl文件,內容如下:
- package cn.itcast.aidl;
- import cn.itcast.domain.Person;
- interface IPersonService {
- void save(inPerson person);
- }
4> 在實現aidl文件生成的接口(本例是IPersonService),但並非直接實現接口,而是通過繼承接口的Stub來實現(Stub抽象類內部實現了aidl接口),並且實現接口方法的代碼。內容如下:
- public class ServiceBinder extends IPersonService.Stub {
- @Override
- public voidsave(Person person) throws RemoteException {
- Log.i("PersonService",person.getId()+"="+ person.getName());
- }
- }
5> 創建一個Service(服務),在服務的onBind(Intent intent)方法中返回實現了aidl接口的對象(本例是ServiceBinder)。內容如下:
- public class PersonService extends Service {
- privateServiceBinder serviceBinder = new ServiceBinder();
- @Override
- public IBinderonBind(Intent intent) {
- return serviceBinder;
- }
- public class ServiceBinder extends IPersonService.Stub {
- @Override
- public void save(Person person) throwsRemoteException {
- Log.i("PersonService",person.getId()+"="+ person.getName());
- }
- }
- }
其他應用可以通過隱式意圖訪問服務,意圖的動作可以自定義,AndroidManifest.xml配置代碼如下:
- <service android:name=".PersonService" >
- <intent-filter>
- <actionandroid:nameactionandroid:name="cn.itcast.process.aidl.PersonService " />
- </intent-filter>
- </service>
6> 把應用中的aidl文件和所在package一起拷貝到客戶端應用的src目錄下,eclipse會自動在客戶端應用的gen目錄中爲aidl文件同步生成IPersonService.java接口文件,接下來再把自定義類型文件和類型聲明aidl文件及所在package一起拷貝到客戶端應用的src目錄下。
最後就可以在客戶端應用中實現與遠程服務的通信,代碼如下:
- public class ClientActivity extends Activity {
- private IPersonService personService;
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- this.bindService(newIntent("cn.itcast.process.aidl.PersonService"),this.serviceConnection, BIND_AUTO_CREATE);//綁定到服務
- }
- @Override
- protected voidonDestroy() {
- super.onDestroy();
- this.unbindService(serviceConnection);//解除服務
- }
- privateServiceConnection serviceConnection = new ServiceConnection() {
- @Override
- public void onServiceConnected(ComponentName name, IBinder service) {
- personService =IPersonService.Stub.asInterface(service);
- try {
- personService.save(new Person(56,"liming"));
- } catch(RemoteException e) {
- Log.e("ClientActivity",e.toString());
- }
- }
- @Override
- public void onServiceDisconnected(ComponentName name) {
- personService =null;
- }
- };
- }
下面來總結一下定義一個遠程服務的步驟:
假設A應用需要與B應用進行通信,調用B應用中的download(String path)方法,B應用以Service方式向A應用提供服務。需要下面四個步驟:
1> 在B應用中創建*.aidl文件,aidl文件的定義和接口的定義很相類,如:在cn.itcast.aidl包下創建IDownloadService.aidl文件,內容如下:
- package cn.itcast.aidl;
- interfaceIDownloadService {
- void download(String path);
- }
當完成aidl文件創建後,eclipse會自動在項目的gen目錄中同步生成IDownloadService.java接口文件。接口文件中生成一個Stub的抽象類,裏面包括aidl定義的方法,還包括一些其它輔助方法。值得關注的是asInterface(IBinder iBinder),它返回接口類型的實例,對於遠程服務調用,遠程服務返回給客戶端的對象爲代理對象,客戶端在onServiceConnected(ComponentName name, IBinder service)方法引用該對象時不能直接強轉成接口類型的實例,而應該使用asInterface(IBinder iBinder)進行類型轉換。
1.接口名和aidl文件名相同。
2.接口和方法前不用加訪問權限修飾符public,private,protected等,也不能用final,static。
3.Aidl默認支持的類型包話java基本類型(int、long、boolean等)和(String、List、Map、CharSequence),使用這些類型時不需要import聲明。對於List和Map中的元素類型必須是Aidl支持的類型。如果使用自定義類型作爲參數或返回值,自定義類型必須實現Parcelable接口。
4.自定義類型和AIDL生成的其它接口類型在aidl描述文件中,應該顯式import,即便在該類和定義的包在同一個包中。
5.在aidl文件中所有非Java基本類型參數必須加上in、out、inout標記,以指明參數是輸入參數、輸出參數還是輸入輸出參數。
6.Java原始類型默認的標記爲in,不能爲其它標記。
2> 在B應用中實現aidl文件生成的接口(本例是IDownloadService),但並非直接實現接口,而是通過繼承接口的Stub來實現(Stub抽象類內部實現了aidl接口),並且實現接口方法的代碼。內容如下:
- public class ServiceBinder extends IDownloadService.Stub {
- @Override
- public void download(String path) throws RemoteException {
- Log.i("DownloadService",path);
- }
- }
3> 在B應用中創建一個Service(服務),在服務的onBind(Intent intent)方法中返回實現了aidl接口的對象(本例是ServiceBinder)。內容如下:
- public class DownloadService extendsService {
- private ServiceBinder serviceBinder = new ServiceBinder();
- @Override
- public IBinder onBind(Intent intent) {
- return serviceBinder;
- }
- public class ServiceBinder extends IDownloadService.Stub {
- @Override
- public void download(String path) throws RemoteException {
- Log.i("DownloadService",path);
- }
- }
- }
其他應用可以通過隱式意圖訪問服務,意圖的動作可以自定義,AndroidManifest.xml配置代碼如下:
- <serviceandroid:name=".DownloadService">
- <intent-filter>
- <action android:name="cn.itcast.process.aidl.DownloadService"/>
- </intent-filter>
- </service>
4> 把B應用中aidl文件所在package連同aidl文件一起拷貝到客戶端A應用,eclipse會自動在A應用的gen目錄中爲aidl文件同步生成IDownloadService.java接口文件,接下來就可以在A應用中實現與B應用通信,代碼如下:
- public class ClientActivity extendsActivity {
- private IDownloadService downloadService;
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- this.bindService(newIntent("cn.itcast.process.aidl.DownloadService"), this.serviceConnection,BIND_AUTO_CREATE);//綁定到服務
- }
- @Override
- protected void onDestroy() {
- super.onDestroy();
- this.unbindService(serviceConnection);//解除服務
- }
- private ServiceConnection serviceConnection = new ServiceConnection() {
- @Override
- public void onServiceConnected(ComponentName name, IBinder service){
- downloadService = IDownloadService.Stub.asInterface(service);
- try {
- downloadService.download("http://www.itcast.cn");
- } catch (RemoteException e) {
- Log.e("ClientActivity", e.toString());
- }
- }
- @Override
- public void onServiceDisconnected(ComponentName name) {
- downloadService = null;
- }
- };
- }
最後看一下Service的生命週期以及和生命週期相關的方法:
與採用Context.startService()方法啓動服務有關的生命週期方法
onCreate()->onStart()->onDestroy()
onCreate()該方法在服務被創建時調用,該方法只會被調用一次,無論調用多少次startService()或bindService()方法,服務也只被創建一次。
onStart()只有採用Context.startService()方法啓動服務時纔會回調該方法。該方法在服務開始運行時被調用。多次調用startService()方法儘管不會多次創建服務,但onStart() 方法會被多次調用。
onDestroy()該方法在服務被終止時調用。
onCreate()->onBind()->onUnbind()->onDestroy()
onBind()只有採用Context.bindService()方法啓動服務時纔會回調該方法。該方法在調用者與服務綁定時被調用,當調用者與服務已經綁定,多次調用Context.bindService()方法並不會導致該方法被多次調用。
onUnbind()只有採用Context.bindService()方法啓動服務時纔會回調該方法。該方法在調用者與服務解除綁定時被調用。
如果先採用startService()方法啓動服務,然後調用bindService()方法綁定到服務,再調用unbindService()方法解除綁定,最後調用bindService()方法再次綁定到服務,觸發的生命週期方法如下:
onCreate()->onStart()->onBind()->onUnbind()[重載後的方法需返回true]->onRebind()
總結:
Android中的服務和windows中的服務是類似的東西,服務一般沒有用戶操作界面,它運行於系統中不容易被用戶發覺,可以使用它開發如監控之類的程序。服務的開發比較簡單,如下:
第一步:繼承Service類
public class SMSService extends Service { }
第二步:在AndroidManifest.xml文件中的<application>節點裏對服務進行配置:
<service android:name=".SMSService" />
服務不能自己運行,需要通過調用Context.startService()或Context.bindService()方法啓動服務。這兩個方法都可以啓動Service,但是它們的使用場合有所不同。使用startService()方法啓用服務,調用者與服務之間沒有關連,即使調用者退出了,服務仍然運行。使用bindService()方法啓用服務,調用者與服務綁定在了一起,調用者一旦退出,服務也就終止,大有“不求同時生,必須同時死”的特點。
採用Context.startService()方法啓動服務,在服務未被創建時,系統會先調用服務的onCreate()方法,接着調用onStart()方法。如果調用startService()方法前服務已經被創建,多次調用startService()方法並不會導致多次創建服務,但會導致多次調用onStart()方法。採用startService()方法啓動的服務,只能調用Context.stopService()方法結束服務,服務結束時會調用onDestroy()方法。
下面兩張圖是Service的生命週期:
圖一
圖二