詳解Android中Service AIDL的使用
有些朋友可能是從事開發工作的時間不是特別的長,所以覺得Service相對與另外兩個組件activity、broadcast receiver來說,使用可能并不是特別的多,所以對Service來說,理解不是特別的深入,只是有一個大概的概念,今天就和一塊來走一下Service,希望能夠幫助到大家對Service有更深入的理解。
Service基本用法——本地服務我們知道服務分為本地服務和遠程服務,而本地服務由于它的啟動方式不一樣,所以生命周期也就不一樣,對Service生命周期不熟悉的朋友,自行去百度一下啊。好了,那么我們分別看一下兩種不同的啟動方式。
我們先創建好Service:ServiceTest.java
package com.example.administrator.servicetestaidl;import android.app.Service;import android.content.Intent;import android.os.IBinder;import android.support.annotation.IntDef;import android.util.Log;public class ServiceTest extends Service { @Override public void onCreate() {super.onCreate();Log.d('ServiceTest',' -----> onCreate'); } @Override public int onStartCommand(Intent intent,int flags, int startId) {Log.d('ServiceTest',' -----> onStartCommand');return super.onStartCommand(intent, flags, startId); } @Override public void onDestroy() {super.onDestroy();Log.d('ServiceTest',' -----> onDestroy'); } @Override public IBinder onBind(Intent intent) {// TODO: Return the communication channel to the service.throw new UnsupportedOperationException('Not yet implemented'); }}
在看看MainActivity的代碼:
package com.example.administrator.servicetestaidl;import android.app.Activity;import android.content.Intent;import android.os.Bundle;import android.view.View;import android.widget.Button;public class MainActivity extends Activity { private Button startService, 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(new View.OnClickListener() { @Override public void onClick(View v) {Intent startService = new Intent(MainActivity.this,ServiceTest.class);startService(startService); }});/** * 停止服務 */stopService.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) {Intent stopService = new Intent(MainActivity.this,ServiceTest.class);stopService(stopService); }}); }}
布局activity_main
<?xml version='1.0' encoding='utf-8'?><LinearLayout xmlns:android='http://schemas.android.com/apk/res/android' android:layout_width='match_parent' android:layout_height='match_parent' android:orientation='vertical'> <Buttonandroid: android:layout_width='wrap_content'android:layout_height='wrap_content'android:text='開啟服務' /> <Buttonandroid: android:layout_width='wrap_content'android:layout_height='wrap_content'android:text='停止服務' /></LinearLayout>
配置文件AndroidManifest.xml
<?xml version='1.0' encoding='utf-8'?><manifest xmlns:android='http://schemas.android.com/apk/res/android' package='com.example.administrator.servicetestaidl'> <applicationandroid:allowBackup='true'android:icon='@mipmap/ic_launcher'android:label='@string/app_name'android:roundIcon='@mipmap/ic_launcher_round'android:supportsRtl='true'android:theme='@style/AppTheme'><activity android:name='.MainActivity'> <intent-filter><action android:name='android.intent.action.MAIN' /><category android:name='android.intent.category.LAUNCHER' /> </intent-filter></activity><service android:name='.ServiceTest' android:enabled='true' android:exported='true'></service> </application></manifest>
上面的代碼很簡單,并不難理解,在頁面上加兩個按鈕,一個是啟動服務,一個是銷毀服務的,并且我們在ServiceTest里面的幾個方法都加上了log,那我們點擊開啟服務,看看Log,如圖:
然后我們多次點擊開啟服務,如圖:
我們看到,后面即使多點幾下這個開啟服務,但是也只會調onStartCommand方法,onCreate方法并不會重復調用,那是因為我們點擊Service,由于該service已經存在,所以并不會重新創建,所以onCreate方法只會調用一次。我們還可以到手機的應用程序管理界面來檢查一下Service是不是正在運行,如下圖所示:
那當我們點擊停止服務按鈕呢,看看log:如圖
這時候說明了服務已經銷毀了。
有些朋友可能注意到了,我們剛剛那種啟動服務的方式,好像除了對Service進行開啟和銷毀以外,很難在activity里進行對Service進行控制,什么意思呢?舉個例子,如果說我現在用Service進行下載某些東西,我現在在Service寫有下載這兩個東西的方法,方法a,方法b,那么我怎樣在activity里面控制什么時候調用方法a,什么時候調用方法b呢,如果按照原本的啟動方式,好像并不好實現,或者說靈活性很差,那么有沒有辦法辦到呢,接著看Service另一種啟動方式。在前面我們有一個方法一直都沒有動onBind方法,我們就從這個方法入手,先看ServiceTest代碼:
package com.example.administrator.servicetestaidl;import android.app.Service;import android.content.Intent;import android.os.Binder;import android.os.IBinder;import android.support.annotation.IntDef;import android.util.Log;public class ServiceTest extends Service { @Override public void onCreate() {super.onCreate();Log.d('ServiceTest',' -----> onCreate'); } @Override public int onStartCommand(Intent intent,int flags, int startId) {Log.d('ServiceTest',' -----> onStartCommand');return super.onStartCommand(intent, flags, startId); } @Override public void onDestroy() {super.onDestroy();Log.d('ServiceTest',' -----> onDestroy'); } @Override public IBinder onBind(Intent intent) {return new Mybind(); } class Mybind extends Binder{public void getString(){ Log.d('ServiceTest',' -----> getString');} }}
在ServiceTest中增加了一個內部類Mybind,并且在Mybind中增加一個getString方法,在方法中打印log,然后在onBind方法中返回Mybind對象。
再看看MainActivity的代碼
package com.example.administrator.servicetestaidl;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.view.View;import android.widget.Button;public class MainActivity extends Activity { private Button startService,stopService,bindService,unbindService; private ServiceTest.Mybind mybind; private ServiceConnection connection = new ServiceConnection() {@Overridepublic void onServiceConnected(ComponentName name, IBinder service) { mybind = (ServiceTest.Mybind) service; mybind.getString(); //獲取到getString方法}@Overridepublic void onServiceDisconnected(ComponentName name) {} }; @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(new View.OnClickListener() { @Override public void onClick(View v) {Intent startService = new Intent(MainActivity.this,ServiceTest.class);startService(startService); }});/** * 停止服務 */stopService.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) {Intent stopService = new Intent(MainActivity.this,ServiceTest.class);stopService(stopService); }});/** * 綁定服務 */bindService.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) {Intent bindService = new Intent(MainActivity.this,ServiceTest.class);bindService(bindService,connection,BIND_AUTO_CREATE); }});/** * 解綁服務 */unbindService.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) {unbindService(connection); }}); }}
主頁面布局:activity_main
<?xml version='1.0' encoding='utf-8'?><LinearLayout xmlns:android='http://schemas.android.com/apk/res/android' android:layout_width='match_parent' android:layout_height='match_parent' android:orientation='vertical'> <Buttonandroid: android:layout_width='wrap_content'android:layout_height='wrap_content'android:text='開啟服務' /> <Buttonandroid: android:layout_width='wrap_content'android:layout_height='wrap_content'android:text='停止服務' /> <Buttonandroid: android:layout_width='wrap_content'android:layout_height='wrap_content'android:text='綁定服務' /> <Buttonandroid: android:layout_width='wrap_content'android:layout_height='wrap_content'android:text='解綁服務' /></LinearLayout>
可以看到,這里我們首先創建了一個ServiceConnection的匿名類,在里面重寫了onServiceConnected()方法和onServiceDisconnected()方法,這兩個方法分別會在Activity與Service建立關聯和解除關聯的時候調用。在onServiceConnected()方法中,我們又通過向下轉型得到了MyBind的實例,有了這個實例,Activity和Service之間的關系就變得非常緊密了。現在我們可以在Activity中根據具體的場景來調用MyBind中的任何public方法,即實現了Activity指揮Service干什么Service就去干什么的功能。
當我們點擊綁定服務的時候,結果如下,如圖
點擊解綁服務的時候,結果如下,如圖
注意:Service 是運行在后臺,沒有可視化的頁面,我們很多時候會把耗時的操作放在Service中執行,但是注意,Service是運行在主線程的,不是在子線程中,Service和Thread沒有半毛錢的關系,所以如果在Service中執行耗時操作,一樣是需要開起線程,否則會引起ANR,這個需要區別開來。
遠程服務 —— AIDLAIDL(Android Interface Definition Language)是Android接口定義語言的意思,它可以用于讓某個Service與多個應用程序組件之間進行跨進程通信,從而可以實現多個應用程序共享同一個Service的功能。實際上實現跨進程之間通信的有很多,比如廣播,Content Provider,但是AIDL的優勢在于速度快(系統底層直接是共享內存),性能穩,效率高,一般進程間通信就用它。
既然是跨進程,那必須的有兩個應用,一個是service端,一個是client端,然后實現客戶端從服務端獲取數據。那么我們創建一個服務端,項目結構如圖所示:
我們在服務端下建立一個MyAIDLService.aidl文件,目錄結構為如圖所示:
然后,我們在MyAIDLService下增加一個獲取字符串的方法。代碼如下:(注:剛剛建立的aidl文件中存在一個方法,那個方法可以忽略,可以刪掉不要)
// MyAIDLService.aidlpackage aidl;// Declare any non-default types here with import statementsinterface MyAIDLService { //獲取String數據 String getString();}
創建完aidl文件以后,我們build一下項目,然后會在build - >generated ->source ->aidl->debug下會生成一個aidl文件,那說明AIDL文件已經編譯成功。
接著建立一個MyService類,代碼如下:
package com.example.service;import android.app.Service;import android.content.Intent;import android.os.Binder;import android.os.IBinder;import android.os.RemoteException;import android.util.Log;import java.util.Map;import aidl.MyAIDLService;public class MyService extends Service { @Override public void onCreate() {super.onCreate(); } @Override public int onStartCommand(Intent intent, int flags, int startId) {return super.onStartCommand(intent, flags, startId); } @Override public void onDestroy() {super.onDestroy(); } @Override public IBinder onBind(Intent intent) {return new Mybind(); } class Mybind extends MyAIDLService.Stub {@Overridepublic String getString() throws RemoteException { String string = '我是從服務起返回的'; return string;} }}
代碼看起來是不是很熟悉,唯一不一樣的就是原來在本地服務的時候內部類繼承的是Binder,而現在繼承的是MyAIDLService.Stub,繼承的是我們剛剛建立的aidl文件,然后實現我們剛剛的定義的getString()方法,在這里,我們只是返回一句話,'我是從服務起返回的'~~~~~~~~~~~
客戶端首先將剛剛在服務端創建的MyAIDLService原封不動的復制到客戶端來。(注意:路徑要一模一樣)。接著我們在客戶端的MainActivity中加兩個按鈕,并且和服務端進行相連,代碼如下:
MainActivity
package com.example.administrator.servicetestaidl;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.view.View;import android.widget.Button;import android.widget.TextView;import aidl.MyAIDLService;public class MainActivity extends Activity { private Button bindService,unbindService; private TextView tvData; private MyAIDLService myAIDLService; private ServiceConnection connection = new ServiceConnection() {@Overridepublic void onServiceConnected(ComponentName name, IBinder service) { myAIDLService = MyAIDLService.Stub.asInterface(service); try {String str = myAIDLService.getString();tvData.setText(str); } catch (RemoteException e) {e.printStackTrace(); }}@Overridepublic void onServiceDisconnected(ComponentName name) { myAIDLService = null;} }; @Override protected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);bindService = (Button) findViewById(R.id.bind_service);unbindService = (Button) findViewById(R.id.unbind_service);tvData = (TextView) findViewById(R.id.tv_data);/** * 綁定服務 */bindService.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) {Intent intent = new Intent();intent.setAction('com.example.service.MyService');//從 Android 5.0開始 隱式Intent綁定服務的方式已不能使用,所以這里需要設置Service所在服務端的包名intent.setPackage('com.example.service');bindService(intent, connection, BIND_AUTO_CREATE); }});/** * 解綁服務 */unbindService.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) {unbindService(connection); }}); }}
大家是不是感覺和連接本地服務的代碼差不多,沒錯,這里只需要注意兩個地方,一個是綁定服務的時候,因為從 Android 5.0開始 隱式Intent綁定服務的方式已不能使用,所以這里需要設置Service所在服務端的包名
那么這個action是怎么來的呢,我們回來服務端的AndroidManifest.xml,代碼如下
<?xml version='1.0' encoding='utf-8'?><manifest xmlns:android='http://schemas.android.com/apk/res/android' package='com.example.service'> <applicationandroid:allowBackup='true'android:icon='@mipmap/ic_launcher'android:label='@string/app_name'android:roundIcon='@mipmap/ic_launcher_round'android:supportsRtl='true'android:theme='@style/AppTheme'><activity android:name='.MainActivity'> <intent-filter><action android:name='android.intent.action.MAIN' /><category android:name='android.intent.category.LAUNCHER' /> </intent-filter></activity><service android:name='.MyService' > <intent-filter><action android:name='com.example.service.MyService' /> </intent-filter></service> </application></manifest>
另一個需要注意的就是獲取MyAIDLService對象是通過MyAIDLService.Stub.asInterface(service);這里大家需要注意一下的。不過還有一點需要說明的是,由于這是在不同的進程之間傳遞數據,Android對這類數據的格式支持是非常有限的,基本上只能傳遞Java的基本數據類型、字符串、List或Map等。那么如果我想傳遞一個自定義的類該怎么辦呢?這就必須要讓這個類去實現Parcelable接口,并且要給這個類也定義一個同名的AIDL文件。這部分內容并不復雜,而且和Service關系不大,所以就不再詳細進行講解了,感興趣的朋友可以自己去查閱一下相關的資料。
注意:從服務器復制過來的aidl文件不能直接放到Java文件夾下面,必須建立一個aidl文件夾存放,否則會編譯不成功
好了,到這里,基本上就結束了,附上一張效果圖:
最后附上源碼鏈接
本地服務源碼:https://github.com/343661629/nativeService
遠程服務源碼:https://github.com/343661629/remoteService
以上就是詳解Android中Service AIDL的使用的詳細內容,更多關于Android中Service AIDL的使用的資料請關注好吧啦網其它相關文章!
相關文章: