亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

Android中如何利用AIDL機制調(diào)用遠程服務(wù)

 更新時間:2016年03月14日 09:39:26   作者:水火379  
這篇文章主要介紹了Android中如何利用AIDL機制調(diào)用遠程服務(wù)的相關(guān)資料,需要的朋友可以參考下

在Android中,每個應(yīng)用程序都有自己的進程,當需要在不同的進程之間傳遞對象時,該如何實現(xiàn)呢?顯然, Java中是不支持跨進程內(nèi)存共享的。因此要傳遞對象,需要把對象解析成操作系統(tǒng)能夠理解的數(shù)據(jù)格式,以達到跨界對象訪問的目的。在JavaEE中,采用RMI通過序列化傳遞對象。在Android中,則采用AIDL(Android Interface DefinitionLanguage:接口描述語言)方式實現(xiàn)。

AIDL是一種接口定義語言,用于約束兩個進程間的通訊規(guī)則,供編譯器生成代碼,實現(xiàn)Android設(shè)備上的兩個進程間通信(IPC)。AIDL的IPC機制和EJB所采用的CORBA很類似,進程之間的通信信息,首先會被轉(zhuǎn)換成AIDL協(xié)議消息,然后發(fā)送給對方,對方收到AIDL協(xié)議消息后再轉(zhuǎn)換成相應(yīng)的對象。由于進程之間的通信信息需要雙向轉(zhuǎn)換,所以android采用代理類在背后實現(xiàn)了信息的雙向轉(zhuǎn)換,代理類由android編譯器生成,對開發(fā)人員來說是透明的。

服務(wù)端:

//CalculateInterface.aidl
package com.itheima.aidl.calculate;
interface CalculateInterface {
double doCalculate(double a, double b);
}
//CalculateService.java
package com.itheima.myaidl.server;
import com.itheima.aidl.calculate.CalculateInterface;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.os.RemoteException;
import android.util.Log;
public class CalculateService extends Service{
private final CalculateInterface.Stub mBinder = new CalculateInterface.Stub() {
@Override
public double doCalculate(double a, double b) throws RemoteException {
return a+b;
}
};
@Override
public IBinder onBind(Intent intent) {
Log.i("test","onBind...");
return mBinder;
}
@Override
public boolean onUnbind(Intent intent) {
Log.i("test","onUnbind...");
return super.onUnbind(intent);
}
@Override
public void onCreate() {
super.onCreate();
Log.i("test","onCreate...");
}
@Override
public void onDestroy() {
super.onDestroy();
Log.i("test","onDestroy...");
}
}
//服務(wù)端manifast文件
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.itheima.myaidl.server"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="14"
android:targetSdkVersion="19" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity 
android:name="com.itheima.myaidl.server.MainActivity"
android:configChanges="locale|layoutDirection"
android:theme="@android:style/Theme.Light.NoTitleBar"
android:screenOrientation="portrait">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service android:name="com.itheima.myaidl.server.CalculateService">
<intent-filter>
<action android:name="com.itheima.myaidl.server.CalculateService" />
</intent-filter>
</service>
</application>
</manifest>
//客戶端
//MainActivity.java
package com.itheima.myaidl.client;
import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
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 com.itheima.aidl.calculate.CalculateInterface;
public class MainActivity extends Activity {
private CalculateInterface mService;
private ServiceConnection mServiceConnection = new ServiceConnection() {
@Override
public void onServiceDisconnected(ComponentName name) {
Log.i("test","service disconnected...");
mService = null;
}
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
Log.i("test","service connected...");
mService = CalculateInterface.Stub.asInterface(service); //獲取接口實例
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//綁定遠程服務(wù)
Bundle bundle = new Bundle();
Intent intent = new Intent();
intent.putExtras(bundle);
intent.setAction("com.itheima.myaidl.server.CalculateService");
bindService(intent, mServiceConnection, Context.BIND_AUTO_CREATE);
}
//TODO activity加載完畢時回調(diào)此方法
@Override
public void onWindowFocusChanged(boolean hasFocus) {
if(hasFocus){
try{
double result = mService.doCalculate(1, 2);
Log.i("test","result===>"+result);
}catch(RemoteException e){
e.printStackTrace();
}
}
super.onWindowFocusChanged(hasFocus);
}
@Override
protected void onDestroy() {
unbindService(mServiceConnection); //解綁遠程服務(wù)
super.onDestroy();
}
}

運行結(jié)果截圖:

以上所述是小編給大家介紹的Android中如何利用AIDL機制調(diào)用遠程服務(wù)的相關(guān)知識,希望對大家有所幫助!

相關(guān)文章

  • 支持多項選擇的ExpandableListView

    支持多項選擇的ExpandableListView

    這篇文章主要為大家詳細介紹了支持多項選擇的ExpandableListView,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2018-06-06
  • Android中使用GridView和ImageViewSwitcher實現(xiàn)電子相冊簡單功能實例

    Android中使用GridView和ImageViewSwitcher實現(xiàn)電子相冊簡單功能實例

    本篇文章主要介紹了Android中使用GridView和ImageViewSwitcher實現(xiàn)電子相冊簡單功能實例,具有一定的參考價值,有需要的可以了解一下。
    2016-12-12
  • android中view手勢滑動沖突的解決方法

    android中view手勢滑動沖突的解決方法

    本篇文章主要介紹了android中view手勢滑動沖突的解決方法,主要解決方法有兩種,外部和內(nèi)部攔截。有需要的可以參考下。
    2016-11-11
  • Android編程動態(tài)加載布局實例詳解【附demo源碼】

    Android編程動態(tài)加載布局實例詳解【附demo源碼】

    這篇文章主要介紹了Android編程動態(tài)加載布局,結(jié)合實例形式分析了Android動態(tài)加載布局的原理、操作步驟與相關(guān)實現(xiàn)技巧,需要的朋友可以參考下
    2016-10-10
  • Android this與Activity.this的區(qū)別

    Android this與Activity.this的區(qū)別

    這篇文章主要介紹了 Android this與Activity.this的區(qū)別的相關(guān)資料,需要的朋友可以參考下
    2016-09-09
  • Android ApplicationContext接口深入分析

    Android ApplicationContext接口深入分析

    ApplicationContext是Spring應(yīng)用程序中的中央接口,由于繼承了多個組件,使得ApplicationContext擁有了許多Spring的核心功能,如獲取bean組件,注冊監(jiān)聽事件,加載資源文件等
    2022-11-11
  • 詳解Android事件的分發(fā)、攔截和執(zhí)行

    詳解Android事件的分發(fā)、攔截和執(zhí)行

    這篇文章主要為大家詳細介紹了詳解Android事件的分發(fā)、攔截和執(zhí)行,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2016-09-09
  • Android自定義view貝塞爾曲線

    Android自定義view貝塞爾曲線

    這篇文章主要為大家詳細介紹了Android自定義view貝塞爾曲線,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-06-06
  • 安卓版微信小程序跳一跳輔助

    安卓版微信小程序跳一跳輔助

    這篇文章主要為大家詳細介紹了安卓版微信小程序跳一跳輔助,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2018-01-01
  • Kotlin Channel處理多個數(shù)據(jù)組合的流

    Kotlin Channel處理多個數(shù)據(jù)組合的流

    最近項目中對 kotlin 的使用比較多。不得不說 kotlin 確實可以極大的提高 android 的開發(fā)效率,channel用于協(xié)程之間的通訊,使用send和receive往通道里寫入或者讀取數(shù)據(jù),2個方法為非阻塞掛起函數(shù),channel是熱流,不管有沒有訂閱者都會發(fā)送
    2022-11-11

最新評論