Android Service完全解析,關于服務你所需知道的一切

原文地址:Android Service完全解析,關于服務你所需知道的一切(上)

相信大多數(shù)朋友對Service這個名詞都不會陌生,沒錯,一個老練的Android程序員如果連Service都沒聽說過的話,那確實也太遜了。Service作為Android四大組件之一,在每一個應用程序中都扮演著非常重要的角色。它主要用于在后臺處理一些耗時的邏輯,或者去執(zhí)行某些需要長期運行的任務。必要的時候我們甚至可以在程序退出的情況下,讓Service在后臺繼續(xù)保持運行狀態(tài)。
不過,雖然Service幾乎被每一個Android程序員所熟知,但并不是每個人都已經(jīng)將Service的各個知識點都掌握得非常透徹。那么今天我就將帶著大家對Service進行一次全面、深入的探究,希望每個人在讀完本篇文章后都能對Service有更深一層的理解。
Service的基本用法
關于Service最基本的用法自然就是如何啟動一個Service了,啟動Service的方法和啟動Activity很類似,都需要借助Intent來實現(xiàn),下面我們就通過一個具體的例子來看一下。
新建一個Android項目,項目名就叫ServiceTest,這里我選擇使用4.0的API。
然后新建一個MyService繼承自Service,并重寫父類的onCreate()、onStartCommand()和onDestroy()方法,如下所示:

public class MyService extends Service {

    public static final String TAG = "MyService";

    @Override
    public void onCreate() {
        super.onCreate();
        Log.d(TAG, "onCreate() executed");
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        Log.d(TAG, "onStartCommand() executed");
        return super.onStartCommand(intent, flags, startId);
    }
    
    @Override
    public void onDestroy() {
        super.onDestroy();
        Log.d(TAG, "onDestroy() executed");
    }

    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

}

可以看到,我們只是在onCreate()、onStartCommand()和onDestroy()方法中分別打印了一句話,并沒有進行其它任何的操作。
然后打開或新建activity_main.xml作為程序的主布局文件,代碼如下所示:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <Button
        android:id="@+id/start_service"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Start Service" />

    <Button
        android:id="@+id/stop_service"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Stop Service" />

</LinearLayout>

我們在布局文件中加入了兩個按鈕,一個用于啟動Service,一個用于停止Service。
然后打開或新建MainActivity作為程序的主Activity,在里面加入啟動Service和停止Service的邏輯,代碼如下所示:

public class MainActivity extends Activity implements OnClickListener {

    private Button startService;

    private Button stopService;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        startService = (Button) findViewById(R.id.start_service);
        stopService = (Button) findViewById(R.id.stop_service);
        startService.setOnClickListener(this);
        stopService.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
        case R.id.start_service:
            Intent startIntent = new Intent(this, MyService.class);
            startService(startIntent);
            break;
        case R.id.stop_service:
            Intent stopIntent = new Intent(this, MyService.class);
            stopService(stopIntent);
            break;
        default:
            break;
        }}
    }

可以看到,在Start Service按鈕的點擊事件里,我們構建出了一個Intent對象,并調用startService()方法來啟動MyService。然后在Stop Serivce按鈕的點擊事件里,我們同樣構建出了一個Intent對象,并調用stopService()方法來停止MyService。代碼的邏輯非常簡單,相信不需要我再多做解釋了吧。
另外需要注意,項目中的每一個Service都必須在AndroidManifest.xml中注冊才行,所以還需要編輯AndroidManifest.xml文件,代碼如下所示:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.servicetest"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="14"
        android:targetSdkVersion="17" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        
    ……

        <service android:name="com.example.servicetest.MyService" >
        </service>
    </application>

</manifest>

這樣的話,一個簡單的帶有Service功能的程序就寫好了,現(xiàn)在我們將程序運行起來,并點擊一下Start Service按鈕,可以看到LogCat的打印日志如下:


也就是說,當啟動一個Service的時候,會調用該Service中的onCreate()和onStartCommand()方法。
那么如果我再點擊一次Start Service按鈕呢?這個時候的打印日志如下:

可以看到,這次只有onStartCommand()方法執(zhí)行了,onCreate()方法并沒有執(zhí)行,為什么會這樣呢?這是由于onCreate()方法只會在Service第一次被創(chuàng)建的時候調用,如果當前Service已經(jīng)被創(chuàng)建過了,不管怎樣調用startService()方法,onCreate()方法都不會再執(zhí)行。因此你可以再多點擊幾次Start Service按鈕試一次,每次都只會有onStartCommand()方法中的打印日志。
我們還可以到手機的應用程序管理界面來檢查一下MyService是不是正在運行,如下圖所示:

恩,MyService確實是正在運行的,即使它的內部并沒有執(zhí)行任何的邏輯。
回到ServiceTest程序,然后點擊一下Stop Service按鈕就可以將MyService停止掉了。
Service和Activity通信
上面我們學習了Service的基本用法,啟動Service之后,就可以在onCreate()或onStartCommand()方法里去執(zhí)行一些具體的邏輯了。不過這樣的話Service和Activity的關系并不大,只是Activity通知了Service一下:“你可以啟動了?!比缓骃ervice就去忙自己的事情了。那么有沒有什么辦法能讓它們倆的關聯(lián)更多一些呢?比如說在Activity中可以指定讓Service去執(zhí)行什么任務。當然可以,只需要讓Activity和Service建立關聯(lián)就好了。
觀察MyService中的代碼,你會發(fā)現(xiàn)一直有一個onBind()方法我們都沒有使用到,這個方法其實就是用于和Activity建立關聯(lián)的,修改MyService中的代碼,如下所示:

public class MyService extends Service {

    public static final String TAG = "MyService";

    private MyBinder mBinder = new MyBinder();

    @Override
    public void onCreate() {
        super.onCreate();
        Log.d(TAG, "onCreate() executed");
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        Log.d(TAG, "onStartCommand() executed");
        return super.onStartCommand(intent, flags, startId);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        Log.d(TAG, "onDestroy() executed");
    }

    @Override
    public IBinder onBind(Intent intent) {
        return mBinder;
    }

    class MyBinder extends Binder {

        public void startDownload() {
            Log.d("TAG", "startDownload() executed");
            // 執(zhí)行具體的下載任務
        }

    }

}

這里我們新增了一個MyBinder類繼承自Binder類,然后在MyBinder中添加了一個startDownload()方法用于在后臺執(zhí)行下載任務,當然這里并不是真正地去下載某個東西,只是做個測試,所以startDownload()方法只是打印了一行日志。
然后修改activity_main.xml中的代碼,在布局文件中添加用于綁定Service和取消綁定Service的按鈕:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <Button
        android:id="@+id/start_service"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Start Service" />

    <Button
        android:id="@+id/stop_service"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Stop Service" />

    <Button
        android:id="@+id/bind_service"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Bind Service" />
    
    <Button 
        android:id="@+id/unbind_service"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Unbind Service"
        />
    
</LinearLayout>

接下來再修改MainActivity中的代碼,讓MainActivity和MyService之間建立關聯(lián),代碼如下所示:

public class MainActivity extends Activity implements OnClickListener {

    private Button startService;

    private Button stopService;

    private Button bindService;

    private Button unbindService;

    private MyService.MyBinder myBinder;

    private ServiceConnection connection = new ServiceConnection() {

        @Override
        public void onServiceDisconnected(ComponentName name) {
        }

        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            myBinder = (MyService.MyBinder) service;
            myBinder.startDownload();
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        startService = (Button) findViewById(R.id.start_service);
        stopService = (Button) findViewById(R.id.stop_service);
        bindService = (Button) findViewById(R.id.bind_service);
        unbindService = (Button) findViewById(R.id.unbind_service);
        startService.setOnClickListener(this);
        stopService.setOnClickListener(this);
        bindService.setOnClickListener(this);
        unbindService.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
        case R.id.start_service:
            Intent startIntent = new Intent(this, MyService.class);
            startService(startIntent);
            break;
        case R.id.stop_service:
            Intent stopIntent = new Intent(this, MyService.class);
            stopService(stopIntent);
            break;
        case R.id.bind_service:
            Intent bindIntent = new Intent(this, MyService.class);
            bindService(bindIntent, connection, BIND_AUTO_CREATE);
            break;
        case R.id.unbind_service:
            unbindService(connection);
            break;
        default:
            break;
        }
    }

}

可以看到,這里我們首先創(chuàng)建了一個ServiceConnection的匿名類,在里面重寫了onServiceConnected()方法和onServiceDisconnected()方法,這兩個方法分別會在Activity與Service建立關聯(lián)和解除關聯(lián)的時候調用。在onServiceConnected()方法中,我們又通過向下轉型得到了MyBinder的實例,有了這個實例,Activity和Service之間的關系就變得非常緊密了?,F(xiàn)在我們可以在Activity中根據(jù)具體的場景來調用MyBinder中的任何public方法,即實現(xiàn)了Activity指揮Service干什么Service就去干什么的功能。
當然,現(xiàn)在Activity和Service其實還沒關聯(lián)起來了呢,這個功能是在Bind Service按鈕的點擊事件里完成的??梢钥吹剑@里我們仍然是構建出了一個Intent對象,然后調用bindService()方法將Activity和Service進行綁定。bindService()方法接收三個參數(shù),第一個參數(shù)就是剛剛構建出的Intent對象,第二個參數(shù)是前面創(chuàng)建出的ServiceConnection的實例,第三個參數(shù)是一個標志位,這里傳入BIND_AUTO_CREATE表示在Activity和Service建立關聯(lián)后自動創(chuàng)建Service,這會使得MyService中的onCreate()方法得到執(zhí)行,但onStartCommand()方法不會執(zhí)行。
然后如何我們想解除Activity和Service之間的關聯(lián)怎么辦呢?調用一下unbindService()方法就可以了,這也是Unbind Service按鈕的點擊事件里實現(xiàn)的邏輯。
現(xiàn)在讓我們重新運行一下程序吧,在MainActivity中點擊一下Bind Service按鈕,LogCat里的打印日志如下圖所示:


另外需要注意,任何一個Service在整個應用程序范圍內都是通用的,即MyService不僅可以和MainActivity建立關聯(lián),還可以和任何一個Activity建立關聯(lián),而且在建立關聯(lián)時它們都可以獲取到相同的MyBinder實例。
如何銷毀Service
在Service的基本用法這一部分,我們介紹了銷毀Service最簡單的一種情況,點擊Start Service按鈕啟動Service,再點擊Stop Service按鈕停止Service,這樣MyService就被銷毀了,可以看到打印日志如下所示:

那么如果我們是點擊的Bind Service按鈕呢?由于在綁定Service的時候指定的標志位是BIND_AUTO_CREATE,說明點擊Bind Service按鈕的時候Service也會被創(chuàng)建,這時應該怎么銷毀Service呢?其實也很簡單,點擊一下Unbind Service按鈕,將Activity和Service的關聯(lián)解除就可以了。
先點擊一下Bind Service按鈕,再點擊一下Unbind Service按鈕,打印日志如下所示:

以上這兩種銷毀的方式都很好理解。那么如果我們既點擊了Start Service按鈕,又點擊了Bind Service按鈕會怎么樣呢?這個時候你會發(fā)現(xiàn),不管你是單獨點擊Stop Service按鈕還是Unbind Service按鈕,Service都不會被銷毀,必要將兩個按鈕都點擊一下,Service才會被銷毀。也就是說,點擊Stop Service按鈕只會讓Service停止,點擊Unbind Service按鈕只會讓Service和Activity解除關聯(lián),一個Service必須要在既沒有和任何Activity關聯(lián)又處理停止狀態(tài)的時候才會被銷毀。
為了證實一下,我們在Stop Service和Unbind Service按鈕的點擊事件里面加入一行打印日志:

public void onClick(View v) {
    switch (v.getId()) {
    case R.id.start_service:
        Intent startIntent = new Intent(this, MyService.class);
        startService(startIntent);
        break;
    case R.id.stop_service:
        Log.d("MyService", "click Stop Service button");
        Intent stopIntent = new Intent(this, MyService.class);
        stopService(stopIntent);
        break;
    case R.id.bind_service:
        Intent bindIntent = new Intent(this, MyService.class);
        bindService(bindIntent, connection, BIND_AUTO_CREATE);
        break;
    case R.id.unbind_service:
        Log.d("MyService", "click Unbind Service button");
        unbindService(connection);
        break;
    default:
        break;
    }
}

然后重新運行程序,先點擊一下Start Service按鈕,再點擊一下Bind Service按鈕,這樣就將Service啟動起來,并和Activity建立了關聯(lián)。然后點擊Stop Service按鈕后Service并不會銷毀,再點擊一下Unbind Service按鈕,Service就會銷毀了,打印日志如下所示:


我們應該始終記得在Service的onDestroy()方法里去清理掉那些不再使用的資源,防止在Service被銷毀后還會有一些不再使用的對象仍占用著內存。
Service和Thread的關系
不少Android初學者都可能會有這樣的疑惑,Service和Thread到底有什么關系呢?什么時候應該用Service,什么時候又應該用Thread?答案可能會有點讓你吃驚,因為Service和Thread之間沒有任何關系!
之所以有不少人會把它們聯(lián)系起來,主要就是因為Service的后臺概念。Thread我們大家都知道,是用于開啟一個子線程,在這里去執(zhí)行一些耗時操作就不會阻塞主線程的運行。而Service我們最初理解的時候,總會覺得它是用來處理一些后臺任務的,一些比較耗時的操作也可以放在這里運行,這就會讓人產(chǎn)生混淆了。但是,如果我告訴你Service其實是運行在主線程里的,你還會覺得它和Thread有什么關系嗎?讓我們看一下這個殘酷的事實吧。
在MainActivity的onCreate()方法里加入一行打印當前線程id的語句:
Log.d("MyService", "MainActivity thread id is " + Thread.currentThread().getId());
然后在MyService的onCreate()方法里也加入一行打印當前線程id的語句:

Log.d("MyService", "MyService thread id is " + Thread.currentThread().getId());
現(xiàn)在重新運行一下程序,并點擊Start Service按鈕,會看到如下打印日志:



可以看到,它們的線程id完全是一樣的,由此證實了Service確實是運行在主線程里的,也就是說如果你在Service里編寫了非常耗時的代碼,程序必定會出現(xiàn)ANR的。
你可能會驚呼,這不是坑爹么?。磕俏乙猄ervice又有何用呢?其實大家不要把后臺和子線程聯(lián)系在一起就行了,這是兩個完全不同的概念。Android的后臺就是指,它的運行是完全不依賴UI的。即使Activity被銷毀,或者程序被關閉,只要進程還在,Service就可以繼續(xù)運行。比如說一些應用程序,始終需要與服務器之間始終保持著心跳連接,就可以使用Service來實現(xiàn)。你可能又會問,前面不是剛剛驗證過Service是運行在主線程里的么?在這里一直執(zhí)行著心跳連接,難道就不會阻塞主線程的運行嗎?當然會,但是我們可以在Service中再創(chuàng)建一個子線程,然后在這里去處理耗時邏輯就沒問題了。
額,既然在Service里也要創(chuàng)建一個子線程,那為什么不直接在Activity里創(chuàng)建呢?這是因為Activity很難對Thread進行控制,當Activity被銷毀之后,就沒有任何其它的辦法可以再重新獲取到之前創(chuàng)建的子線程的實例。而且在一個Activity中創(chuàng)建的子線程,另一個Activity無法對其進行操作。但是Service就不同了,所有的Activity都可以與Service進行關聯(lián),然后可以很方便地操作其中的方法,即使Activity被銷毀了,之后只要重新與Service建立關聯(lián),就又能夠獲取到原有的Service中Binder的實例。因此,使用Service來處理后臺任務,Activity就可以放心地finish,完全不需要擔心無法對后臺任務進行控制的情況。
一個比較標準的Service就可以寫成:

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    new Thread(new Runnable() {
        @Override
        public void run() {
            // 開始執(zhí)行后臺任務
        }
    }).start();
    return super.onStartCommand(intent, flags, startId);
}

class MyBinder extends Binder {

    public void startDownload() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                // 執(zhí)行具體的下載任務
            }
        }).start();
    }

}

創(chuàng)建前臺Service
Service幾乎都是在后臺運行的,一直以來它都是默默地做著辛苦的工作。但是Service的系統(tǒng)優(yōu)先級還是比較低的,當系統(tǒng)出現(xiàn)內存不足情況時,就有可能會回收掉正在后臺運行的Service。如果你希望Service可以一直保持運行狀態(tài),而不會由于系統(tǒng)內存不足的原因導致被回收,就可以考慮使用前臺Service。前臺Service和普通Service最大的區(qū)別就在于,它會一直有一個正在運行的圖標在系統(tǒng)的狀態(tài)欄顯示,下拉狀態(tài)欄后可以看到更加詳細的信息,非常類似于通知的效果。當然有時候你也可能不僅僅是為了防止Service被回收才使用前臺Service,有些項目由于特殊的需求會要求必須使用前臺Service,比如說墨跡天氣,它的Service在后臺更新天氣數(shù)據(jù)的同時,還會在系統(tǒng)狀態(tài)欄一直顯示當前天氣的信息,如下圖所示:

Paste_Image.png

那么我們就來看一下如何才能創(chuàng)建一個前臺Service吧,其實并不復雜,修改MyService中的代碼,如下所示:

public class MyService extends Service {public static final String TAG = "MyService";

private MyBinder mBinder = new MyBinder();

@Override
public void onCreate() {
    super.onCreate();
    Notification notification = new Notification(R.drawable.ic_launcher,
            "有通知到來", System.currentTimeMillis());
    Intent notificationIntent = new Intent(this, MainActivity.class);
    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0,
            notificationIntent, 0);
    notification.setLatestEventInfo(this, "這是通知的標題", "這是通知的內容",
            pendingIntent);
    startForeground(1, notification);
    Log.d(TAG, "onCreate() executed");
}

.........

}

這里只是修改了MyService中onCreate()方法的代碼。可以看到,我們首先創(chuàng)建了一個Notification對象,然后調用了它的setLatestEventInfo()方法來為通知初始化布局和數(shù)據(jù),并在這里設置了點擊通知后就打開MainActivity。然后調用startForeground()方法就可以讓MyService變成一個前臺Service,并會將通知的圖片顯示出來。
現(xiàn)在重新運行一下程序,并點擊Start Service或Bind Service按鈕,MyService就會以前臺Service的模式啟動了,并且在系統(tǒng)狀態(tài)欄會彈出一個通欄圖標,下拉狀態(tài)欄后可以看到通知的詳細內容,如下圖所示。


在上篇文章中我們知道了,Service其實是運行在主線程里的,如果直接在Service中處理一些耗時的邏輯,就會導致程序ANR。
讓我們來做個實驗驗證一下吧,修改上一篇文章中創(chuàng)建的ServiceTest項目,在MyService的onCreate()方法中讓線程睡眠60秒,如下所示:
public class MyService extends Service {

......

@Override
public void onCreate() {
    super.onCreate();
    Log.d(TAG, "onCreate() executed");
    try {
        Thread.sleep(60000);
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
}

......

}
重新運行后,點擊一下Start Service按鈕或Bind Service按鈕,程序就會阻塞住并無法進行任何其它操作,過一段時間后就會彈出ANR的提示框,如下圖所示。



之前我們提到過,應該在Service中開啟線程去執(zhí)行耗時任務,這樣就可以有效地避免ANR的出現(xiàn)。
那么本篇文章的主題是介紹遠程Service的用法,如果將MyService轉換成一個遠程Service,還會不會有ANR的情況呢?讓我們來動手嘗試一下吧。
將一個普通的Service轉換成遠程Service其實非常簡單,只需要在注冊Service的時候將它的android:process屬性指定成:remote就可以了,代碼如下所示:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.servicetest"
    android:versionCode="1"
    android:versionName="1.0" >

    ......
    
    <service
        android:name="com.example.servicetest.MyService"
        android:process=":remote" >
    </service>

</manifest>

現(xiàn)在重新運行程序,并點擊一下Start Service按鈕,你會看到控制臺立刻打印了onCreate() executed的信息,而且主界面并沒有阻塞住,也不會出現(xiàn)ANR。大概過了一分鐘后,又會看到onStartCommand() executed打印了出來。
為什么將MyService轉換成遠程Service后就不會導致程序ANR了呢?這是由于,使用了遠程Service后,MyService已經(jīng)在另外一個進程當中運行了,所以只會阻塞該進程中的主線程,并不會影響到當前的應用程序。為了證實一下MyService現(xiàn)在確實已經(jīng)運行在另外一個進程當中了,我們分別在MainActivity的onCreate()方法和MyService的onCreate()方法里加入一行日志,打印出各自所在的進程id,如下所示:

Log.d("TAG", "process id is " + Process.myPid());

再次重新運行程序,然后點擊一下Start Service按鈕,打印結果如下圖所示:



可以看到,不僅僅是進程id不同了,就連應用程序包名也不一樣了,MyService中打印的那條日志,包名后面還跟上了:remote標識。
那既然遠程Service這么好用,干脆以后我們把所有的Service都轉換成遠程Service吧,還省得再開啟線程了。其實不然,遠程Service非但不好用,甚至可以稱得上是較為難用。一般情況下如果可以不使用遠程Service,就盡量不要使用它。
下面就來看一下它的弊端吧,首先將MyService的onCreate()方法中讓線程睡眠的代碼去除掉,然后重新運行程序,并點擊一下Bind Service按鈕,你會發(fā)現(xiàn)程序崩潰了!為什么點擊Start Service按鈕程序就不會崩潰,而點擊Bind Service按鈕就會崩潰呢?這是由于在Bind Service按鈕的點擊事件里面我們會讓MainActivity和MyService建立關聯(lián),但是目前MyService已經(jīng)是一個遠程Service了,Activity和Service運行在兩個不同的進程當中,這時就不能再使用傳統(tǒng)的建立關聯(lián)的方式,程序也就崩潰了。
那么如何才能讓Activity與一個遠程Service建立關聯(lián)呢?這就要使用AIDL來進行跨進程通信了(IPC)。
AIDL(Android Interface Definition Language)是Android接口定義語言的意思,它可以用于讓某個Service與多個應用程序組件之間進行跨進程通信,從而可以實現(xiàn)多個應用程序共享同一個Service的功能。
下面我們就來一步步地看一下AIDL的用法到底是怎樣的。首先需要新建一個AIDL文件,在這個文件中定義好Activity需要與Service進行通信的方法。新建MyAIDLService.aidl文件,代碼如下所示:

package com.example.servicetest;
interface MyAIDLService {
int plus(int a, int b);
String toUpperCase(String str);
}

點擊保存之后,gen目錄下就會生成一個對應的Java文件,如下圖所示:



然后修改MyService中的代碼,在里面實現(xiàn)我們剛剛定義好的MyAIDLService接口,如下所示:

public class MyService extends Service {

......

@Override
public IBinder onBind(Intent intent) {
    return mBinder;
}

MyAIDLService.Stub mBinder = new Stub() {

    @Override
    public String toUpperCase(String str) throws RemoteException {
        if (str != null) {
            return str.toUpperCase();
        }
        return null;
    }

    @Override
    public int plus(int a, int b) throws RemoteException {
        return a + b;
    }
};

}

這里先是對MyAIDLService.Stub進行了實現(xiàn),重寫里了toUpperCase()和plus()這兩個方法。這兩個方法的作用分別是將一個字符串全部轉換成大寫格式,以及將兩個傳入的整數(shù)進行相加。然后在onBind()方法中將MyAIDLService.Stub的實現(xiàn)返回。這里為什么可以這樣寫呢?因為Stub其實就是Binder的子類,所以在onBind()方法中可以直接返回Stub的實現(xiàn)。
接下來修改MainActivity中的代碼,如下所示:

public class MainActivity extends Activity implements OnClickListener {

private Button startService;  

private Button stopService;  

private Button bindService;  

private Button unbindService;  
  
private MyAIDLService myAIDLService;  

private ServiceConnection connection = new ServiceConnection() {  

    @Override  
    public void onServiceDisconnected(ComponentName name) {  
    }  

    @Override  
    public void onServiceConnected(ComponentName name, IBinder service) {  
        myAIDLService = MyAIDLService.Stub.asInterface(service);  
        try {  
            int result = myAIDLService.plus(3, 5);  
            String upperStr = myAIDLService.toUpperCase("hello world");  
            Log.d("TAG", "result is " + result);  
            Log.d("TAG", "upperStr is " + upperStr);  
        } catch (RemoteException e) {  
            e.printStackTrace();  
        }  
    }  
};  

......  

}

我們只是修改了ServiceConnection中的代碼??梢钥吹?,這里首先使用了MyAIDLService.Stub.asInterface()方法將傳入的IBinder對象傳換成了MyAIDLService對象,接下來就可以調用在MyAIDLService.aidl文件中定義的所有接口了。這里我們先是調用了plus()方法,并傳入了3和5作為參數(shù),然后又調用了toUpperCase()方法,并傳入hello world字符串作為參數(shù),最后將調用方法的返回結果打印出來。
現(xiàn)在重新運行程序,并點擊一下Bind Service按鈕,可以看到打印日志如下所示:



由此可見,我們確實已經(jīng)成功實現(xiàn)跨進程通信了,在一個進程中訪問到了另外一個進程中的方法。
不過你也可以看出,目前的跨進程通信其實并沒有什么實質上的作用,因為這只是在一個Activity里調用了同一個應用程序的Service里的方法。而跨進程通信的真正意義是為了讓一個應用程序去訪問另一個應用程序中的Service,以實現(xiàn)共享Service的功能。那么下面我們自然要學習一下,如何才能在其它的應用程序中調用到MyService里的方法。
在上一篇文章中我們已經(jīng)知道,如果想要讓Activity與Service之間建立關聯(lián),需要調用bindService()方法,并將Intent作為參數(shù)傳遞進去,在Intent里指定好要綁定的Service,示例代碼如下:

Intent bindIntent = new Intent(this, MyService.class);
bindService(bindIntent, connection, BIND_AUTO_CREATE);

這里在構建Intent的時候是使用MyService.class來指定要綁定哪一個Service的,但是在另一個應用程序中去綁定Service的時候并沒有MyService這個類,這時就必須使用到隱式Intent了?,F(xiàn)在修改AndroidManifest.xml中的代碼,給MyService加上一個action,如下所示:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.servicetest"
android:versionCode="1"
android:versionName="1.0" >

......  

<service  
    android:name="com.example.servicetest.MyService"  
    android:process=":remote" >  
    <intent-filter>  
        <action android:name="com.example.servicetest.MyAIDLService"/>  
    </intent-filter>  
</service>  

</manifest>

這就說明,MyService可以響應帶有com.example.servicetest.MyAIDLService這個action的Intent。
現(xiàn)在重新運行一下程序,這樣就把遠程Service端的工作全部完成了。然后創(chuàng)建一個新的Android項目,起名為ClientTest,我們就嘗試在這個程序中遠程調用MyService中的方法。
ClientTest中的Activity如果想要和MyService建立關聯(lián)其實也不難,首先需要將MyAIDLService.aidl文件從ServiceTest項目中拷貝過來,注意要將原有的包路徑一起拷貝過來,完成后項目的結構如下圖所示:



然后打開或新建activity_main.xml,在布局文件中也加入一個Bind Service按鈕:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>

<Button
android:id="@+id/bind_service"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Bind Service"
/>

</LinearLayout>

接下來打開或新建MainActivity,在其中加入和MyService建立關聯(lián)的代碼,如下所示:

public class MainActivity extends Activity {

private MyAIDLService myAIDLService;  

private ServiceConnection connection = new ServiceConnection() {  

    @Override  
    public void onServiceDisconnected(ComponentName name) {  
    }  

    @Override  
    public void onServiceConnected(ComponentName name, IBinder service) {  
        myAIDLService = MyAIDLService.Stub.asInterface(service);  
        try {  
            int result = myAIDLService.plus(50, 50);  
            String upperStr = myAIDLService.toUpperCase("comes from ClientTest");  
            Log.d("TAG", "result is " + result);  
            Log.d("TAG", "upperStr is " + upperStr);  
        } catch (RemoteException e) {  
            e.printStackTrace();  
        }  
    }  
};  

@Override  
protected void onCreate(Bundle savedInstanceState) {  
    super.onCreate(savedInstanceState);  
    setContentView(R.layout.activity_main);  
    Button bindService = (Button) findViewById(R.id.bind_service);  
    bindService.setOnClickListener(new OnClickListener() {  
        @Override  
        public void onClick(View v) {  
            Intent intent = new Intent("com.example.servicetest.MyAIDLService");  
            bindService(intent, connection, BIND_AUTO_CREATE);  
        }  
    });  
}  

}

這部分代碼大家一定會非常眼熟吧?沒錯,這和在ServiceTest的MainActivity中的代碼幾乎是完全相同的,只是在讓Activity和Service建立關聯(lián)的時候我們使用了隱式Intent,將Intent的action指定成了com.example.servicetest.MyAIDLService。
在當前Activity和MyService建立關聯(lián)之后,我們仍然是調用了plus()和toUpperCase()這兩個方法,遠程的MyService會對傳入的參數(shù)進行處理并返回結果,然后將結果打印出來。
這樣的話,ClientTest中的代碼也就全部完成了,現(xiàn)在運行一下這個項目,然后點擊Bind Service按鈕,此時就會去和遠程的MyService建立關聯(lián),觀察LogCat中的打印信息如下所示:



不用我說,大家都已經(jīng)看出,我們的跨進程通信功能已經(jīng)完美實現(xiàn)了。
不過還有一點需要說明的是,由于這是在不同的進程之間傳遞數(shù)據(jù),Android對這類數(shù)據(jù)的格式支持是非常有限的,基本上只能傳遞Java的基本數(shù)據(jù)類型、字符串、List或Map等。那么如果我想傳遞一個自定義的類該怎么辦呢?這就必須要讓這個類去實現(xiàn)Parcelable接口,并且要給這個類也定義一個同名的AIDL文件。這部分內容并不復雜,而且和Service關系不大,所以就不再詳細進行講解了,感興趣的朋友可以自己去查閱一下相關的資料。

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌,老刑警劉巖,帶你破解...
    沈念sama閱讀 227,702評論 6 531
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異,居然都是意外死亡,警方通過查閱死者的電腦和手機,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 98,143評論 3 415
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人,你說我怎么就攤上這事。” “怎么了?”我有些...
    開封第一講書人閱讀 175,553評論 0 373
  • 文/不壞的土叔 我叫張陵,是天一觀的道長。 經(jīng)常有香客問我,道長,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 62,620評論 1 307
  • 正文 為了忘掉前任,我火速辦了婚禮,結果婚禮上,老公的妹妹穿的比我還像新娘。我一直安慰自己,他們只是感情好,可當我...
    茶點故事閱讀 71,416評論 6 405
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著,像睡著了一般。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 54,940評論 1 321
  • 那天,我揣著相機與錄音,去河邊找鬼。 笑死,一個胖子當著我的面吹牛,可吹牛的內容都是我干的。 我是一名探鬼主播,決...
    沈念sama閱讀 43,024評論 3 440
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了?” 一聲冷哼從身側響起,我...
    開封第一講書人閱讀 42,170評論 0 287
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 48,709評論 1 333
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內容為張勛視角 年9月15日...
    茶點故事閱讀 40,597評論 3 354
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 42,784評論 1 369
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖,靈堂內的尸體忽然破棺而出,到底是詐尸還是另有隱情,我是刑警寧澤,帶...
    沈念sama閱讀 38,291評論 5 357
  • 正文 年R本政府宣布,位于F島的核電站,受9級特大地震影響,放射性物質發(fā)生泄漏。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點故事閱讀 44,029評論 3 347
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧,春花似錦、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 34,407評論 0 25
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 35,663評論 1 280
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人。 一個月前我還...
    沈念sama閱讀 51,403評論 3 390
  • 正文 我出身青樓,卻偏偏與公主長得像,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當晚...
    茶點故事閱讀 47,746評論 2 370

推薦閱讀更多精彩內容