android binder ipc
服務端
IDateTimeService.aidl 檔案寫法 與普通java類相同
package com.example.visualizertest;
interface IDateTimeService
{
String getCurrentDateTime(in String format) ;
}
沒問題的話 會在Gen目錄下生成 同名的.java檔案
/*
* This file is auto-generated. DO NOT MODIFY.
* Original file: D:\\ProjectGitReposit\\VisualizerTest\\src\\com\\example\\visualizertest\\IDateTimeService.aidl
*/
package com.example.visualizertest;
public interface IDateTimeService extends android.os.IInterface
{
/** Local-side IPC implementation stub class. */
public static abstract class Stub extends android.os.Binder implements com.example.visualizertest.IDateTimeService
{
private static final java.lang.String DESCRIPTOR = "com.example.visualizertest.IDateTimeService";
/** Construct the stub at attach it to the interface. */
public Stub()
{
this.attachInterface(this, DESCRIPTOR);
}
/**
* Cast an IBinder object into an com.example.visualizertest.IDateTimeService interface,
* generating a proxy if needed.
*/
public static com.example.visualizertest.IDateTimeService asInterface(android.os.IBinder obj)
{
if ((obj==null)) {
return null;
}
android.os.IInterface iin = obj.queryLocalInterface(DESCRIPTOR);
if (((iin!=null)&&(iin instanceof com.example.visualizertest.IDateTimeService))) {
return ((com.example.visualizertest.IDateTimeService)iin);
}
return new com.example.visualizertest.IDateTimeService.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_getCurrentDateTime:
{
data.enforceInterface(DESCRIPTOR);
java.lang.String _arg0;
_arg0 = data.readString();
java.lang.String _result = this.getCurrentDateTime(_arg0);
reply.writeNoException();
reply.writeString(_result);
return true;
}
}
return super.onTransact(code, data, reply, flags);
}
private static class Proxy implements com.example.visualizertest.IDateTimeService
{
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 getCurrentDateTime(java.lang.String format) 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.writeString(format);
mRemote.transact(Stub.TRANSACTION_getCurrentDateTime, _data, _reply, 0);
_reply.readException();
_result = _reply.readString();
}
finally {
_reply.recycle();
_data.recycle();
}
return _result;
}
}
static final int TRANSACTION_getCurrentDateTime = (android.os.IBinder.FIRST_CALL_TRANSACTION + 0);
}
public java.lang.String getCurrentDateTime(java.lang.String format) throws android.os.RemoteException;
}
寫個Service
package com.example.visualizertest;
import java.text.SimpleDateFormat;
import java.util.Date;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.os.RemoteException;
public class DateTimeService extends Service {
public IBinder onBind(Intent arg0) {
return new IDateTimeService.Stub() {
public String getCurrentDateTime(String format) throws RemoteException {
return new SimpleDateFormat(format).format(new Date());
}
};
}
}
配置檔案中配置
<service android:name=".DateTimeService" >
<intent-filter>
<action android:name="com.example.visualizertest.DateTimeService">
</action>
</intent-filter>
</service>
客戶端 :
建立一個與服務端 aidl相同的包名 相同的aidl檔案 gen下自動生成.java檔案與服務端相同。
否則會報ERROR/AndroidRuntime(716): java.lang.SecurityException: Binder invocation to an incorrect interface這個錯誤
使用service與在同程式 相同
package com.example.binder;
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.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import com.example.visualizertest.IDateTimeService;
public class MainActivity2 extends Activity {
class MyServiceConnection implements ServiceConnection {
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
printLog("bind success");
is=IDateTimeService.Stub.asInterface(service);
}
@Override
public void onServiceDisconnected(ComponentName name) {
// TODO Auto-generated method stub
}
}
TextView tv1;
Button b1;
Button b2;
MyServiceConnection sc;
IDateTimeService is;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv1 = (TextView) findViewById(R.id.textView1);
b1 = (Button) findViewById(R.id.button1);
b2 = (Button) findViewById(R.id.button2);
b1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent("com.example.visualizertest.DateTimeService");
sc = new MyServiceConnection();
bindService(intent, sc, BIND_AUTO_CREATE);
}
});
b2.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
try {
String s=is.getCurrentDateTime("yyyy-MM-hh");
printLog("return-->"+s);
} catch (RemoteException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
}
@Override
protected void onDestroy() {
unbindService(sc);
super.onDestroy();
}
private void printLog(String string) {
Log.d("MainActivity2", string);
}
}
結:aidl介面檔案相同
相關文章
- Android Binder IPC分析Android
- Android的IPC機制BinderAndroid
- Android IPC機制(三):淺談Binder的使用Android
- Android的IPC機制(三)——Binder連線池Android
- android-IPC/Binder/D-BUS(Binder/Messager/AIDL)程式間通訊(訊息機制)AndroidAI
- Binder通訊機制與IPC通訊.md
- Android程式間通訊(IPC)機制Binder簡要介紹和學習計劃Android
- Binder學習(四)利用AIDL、Messenger實現IPCAIMessenger
- Android Binder之旅Android
- 真香!為什麼Android要採用Binder作為IPC機制?全套教學資料Android
- Android-Binder(一)Android
- Android IPC 之AIDLAndroidAI
- Android IPC 機制分析Android
- Android Binder機制淺析Android
- Android中的IPC機制Android
- android IPC及原理簡介Android
- 藉助 AIDL 理解 Android Binder 機制——Binder 來龍去脈AIAndroid
- Android進階(六)Binder機制Android
- Android多程式之Binder的使用Android
- android Binder機制深入淺出Android
- Android aidl Binder框架淺析AndroidAI框架
- Android為什麼選擇binderAndroid
- Android Binder機制文章轉載Android
- 圖解Android中的binder機制圖解Android
- 理解 Android Binder 機制(三):Java層AndroidJava
- Binder學習(一)Android中的程式Android
- Android 之 Binder與程式間通訊Android
- [Android進階]Binder學習(初始篇)Android
- Android 高階面試-2:IPC 相關Android面試
- 由淺入深 學習 Android Binder(三)- java binder深究(從java到native)AndroidJava
- 理解 Android Binder 機制(二):C++層AndroidC++
- Android多程式之手動編寫Binder類Android
- Android Binder實現示例(C/C++層)AndroidC++
- Android系統之Binder通訊機制Android
- 理解 Android Binder 機制(一):驅動篇Android
- 徹底理解 Android Binder 通訊架構Android架構
- 02.Android之IPC機制問題Android
- Android C++層使用Binder通訊的方法AndroidC++