はじめに
この記事ではAIDL最も基本的な使用は(、負荷を作成)、AIDLでより深く理解するために、その後のエッセイでは、我々が共有し、議論していきますが何であるかを説明します。
テキスト
- AIDL定義(AIDL何ですか?)
- AIDLアプリケーションシナリオ(AIDLは、あなたが行うことができますか?)
- アプリケーションのAIDLを書くためにどのように?(コード)
AIDL概要(定義)
- AIDL:Androidのインタフェース定義言語であり、Androidのインタフェース定義言語。また、AIDL言語:他の言葉で。
- プロセス間通信を達成するために言語をAIDLに設計されています。
この記事では、コードのAIDLを説明しています。:プロセス間通信の解析には、少しの友人が参照することができ、アプリケーション・プロセス間通信(AIDL及びIPC)について
AIDLのシナリオ
- 以下のような:アプリケーションが呼び出すAlipayの決済機能、マイクロチャネル決済機能を
AIDLアプリケーション書くAIDLのテンプレートコードを
*与你一步步掌握AIDL的应用*
需要
- アプリケーションA:アナログショッピングモールのアプリケーション(例えば:XXを綴ります)
- アプリケーションB(例えば:支払いPO)決済アプリケーションをシミュレートする支払サービスアプリケーションが存在し、実行され、サービスの支払方法は、戻り値で定義されています。
- 要件:呼び出し元のアプリケーションにおける決済サービスのA、Bの支払方法の適用は、パラメータを渡し、戻り値を取得します。
コードの実装
アプリケーションB:呼び出し先
-
- マニフェストファイルのAliPayService、および設定情報:サービスを作成します。
- AndroidManifest.xmlを
<!--调用远程服务,需要通过bind方式启动服务,调用服务方法-->
<service android:name=".service.pay.AliPayService">
<intent-filter>
<action android:name="com.zero.notes.service.pay.xxx"/>
</intent-filter>
</service>
- AliPayService
import android.app.Service;
import android.content.Intent;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.util.Log;
import android.widget.Toast;
/**
* 模拟:阿里支付服务
*/
public class AliPayService extends Service {
@Override
public IBinder onBind(Intent intent) {
return new MyBinder();
}
//模拟支付方法
public boolean aliPay(int money) {
Log.e("AIDL", "服务线程:" + Thread.currentThread().getName());
if (money > 100) {
handler.sendEmptyMessage(1);
return true;
} else {
handler.sendEmptyMessage(0);
return false;
}
}
// class MyBinder extends Binder implements IPayservice {
// @Override
// public boolean callAliPay(int money) throws RemoteException {
// return aliPay(money);
// }
// @Override
// public IBinder asBinder() {
// return null;
// }
// }
/**
* 创建中间人对象(中间帮助类)
*/
class MyBinder extends IPayservice.Stub {
@Override
public boolean callAliPay(int money) {
return aliPay(money);
}
}
private Handler handler = new Handler() {
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
if (msg.what == 1) {
Toast.makeText(getApplicationContext(), "土豪,购买成功...", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(getApplicationContext(), "钱太少啦,购买失败...", Toast.LENGTH_SHORT).show();
}
}
};
}
- IPayservice.aidlは、
ここで注意する:作成したIPayservice.aidlファイルには、インタフェースファイルです。あなたは、Androidのメーカーに直接AIDLファイルを作成することができます。クリーンプロジェクト全体を、必要なファイル情報を生成するためのコードエディタを聞かせて:作成したら、覚えておいてください。(アンドロイドStudioプロジェクトきれいな方法:ビルド- >クリーンプロジェクト)
// IPayservice.aidl
package com.zero.notes.service.pay;
// Declare any non-default types here with import statements
interface IPayservice {
boolean callAliPay(int money);
}
- MainActivityの
アプリケーションBが(:kotlin言語コードが書かれた)サービスを開始しました
class MainActivity : BaseActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
initData()
}
private fun initData() {
val intent = Intent(this,AliPayService::class.java)
startService(intent)
}
}
アプリケーションA:呼び出し側(書き込みコード:Kotlin言語)
- コピー:アプリケーションのアプリケーションBにIPayservice.aidlファイルをコピー
覚え時間::コピーは、パスの場所IPayservice.aidlは、アプリケーションBと完全に一致していなければならない、パッケージ名は、パス名は正確に同じになるように!
示されているように:図1️⃣実施形態アンドロイド図である2️⃣プロジェクトビューモードです。
- サービス接続オブジェクトを作成します。
//创建一个服务连接对象
class MyServiceConnection : ServiceConnection {
private lateinit var iPayService: IPayservice
override fun onServiceDisconnected(name: ComponentName?) {
}
override fun onServiceConnected(name: ComponentName?, service: IBinder?) {
//2. 获取中间人对象(服务绑定成功后会返回一个中间人对象)
iPayService = IPayservice.Stub.asInterface(service)
}
//获取中间人对象
fun getIPayService():IPayservice{
return iPayService
}
}
- アプリケーションのアクティビティの通話サービス方法
class MainActivity : AppCompatActivity() {
private var connection: MyServiceConnection? = null
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val intent = Intent()
intent.action = "com.zero.notes.service.pay.xxx"
intent.setPackage("com.zero.notes")
connection = MyServiceConnection()
bindService(intent, connection, Context.BIND_AUTO_CREATE)
tvJump.setOnClickListener {
val iPayService = connection?.getIPayService()
val pay = iPayService?.callAliPay(1000)!!
if (pay) {
//购买成功
} else {
//购买失败
}
}
}
override fun onDestroy() {
super.onDestroy()
if (connection != null) {
unbindService(connection)
connection = null
}
}
}
- アプリケーションA:活動xmlファイル
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:gravity="center_horizontal"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="@+id/tvJump"
android:layout_width="wrap_content"
android:layout_marginTop="30dp"
android:text="AIDL调用方法"
android:textColor="#FF212121"
android:textSize="20sp"
android:padding="16dp"
android:background="#66000000"
android:textStyle="bold"
android:layout_height="wrap_content"/>
</LinearLayout>