您的位置:首页 > 移动开发 > Android开发

Android 之简单 AIDL 客户端和服务端 附源码

2017-08-02 10:43 281 查看
AIDL 的全称是 Android Interface Definition Language(即 Android 接口定义语言)

在同一个app中

/ * * * * 首先在main 文件下定义 aidl 文件



// IMyAidlInterface.aidl
package com.test.service;

// Declare any non-default types here with import statements

interface IMyAidlInterface {
/**
* Demonstrates some basic types that you can use as parameters
* and return values in AIDL.
*/
void basicTypes(int anInt, long aLong, boolean aBoolean, float aFloat,
double aDouble, String aString);
int add(int a,int b);
int minus(int a,int b );
}


/ * * * * 在service 中,先运行一下 等待生成 则可以调用接口了

点击 Build -> Make Project,然后等待构建完成。



public class TestService extends Service {
private static final String TAG = TestService.class.getSimpleName();

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

private final IMyAidlInterface.Stub mBinder = new IMyAidlInterface.Stub() {
@Override
public void basicTypes(int anInt, long aLong, boolean aBoolean, float aFloat, double aDouble, String aString) throws RemoteException {

}

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

@Override
public int minus(int a, int b) throws RemoteException {
return a-b;
}
};

}


/ * * * * 在客户端调用

public class MainActivity extends Activity {
public static final String TAG = MainActivity.class.getSimpleName();
public static final int MEG_SUM = 0x1100;
private IMyAidlInterface.Stub mBinder;
private Handler mHandler = new Handler(){

@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
switch (msg.what) {
case MEG_SUM:
Toast.makeText(MainActivity.this, "接收到service消息==>" + msg.arg1, Toast.LENGTH_SHORT).show();
}

}
};

ServiceConnection connection = new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
mBinder = (IMyAidlInterface.Stub) service;

try {
msg = mBinder.add(3, 3);
} catch (RemoteException e) {
e.printStackTrace();
}
Toast.makeText(MainActivity.this, "Activity发送消息", Toast.LENGTH_SHORT).show();

Message message = Message.obtain();
message.what = MEG_SUM;
message.arg1 = msg;
mHandler.sendMessage(message);
}
@Override
public void onServiceDisconnected(ComponentName name) {
}
};
private int msg;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Intent intent = new Intent(this, TestService.class);
bindService(intent, connection, Context.BIND_AUTO_CREATE);

}

@Override
protected void onDestroy() {
super.onDestroy();
unbindService(connection);
}

}


在不同一个app中

/ * * * * * * Client * * * * * * *Service

相同的位置相同的地方 用相同的所有



/ * * * * * Service

public class TestService extends Service {
private static final String TAG = TestService.class.getSimpleName();
private IMyAIDL iMyAIDL;

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

@Override
public void onCreate() {
super.onCreate();
iMyAIDL = new IMyAIDL();
}

public  class  IMyAIDL  extends IMyAidlInterface.Stub{

@Override
public void basicTypes(int anInt, long aLong, boolean aBoolean, float aFloat, double aDouble, String aString) throws RemoteException {

}

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

@Override
public int minus(int a, int b) throws RemoteException {
return a-b;
}
}

}


<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.test.service">

<application
android: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=".TestService"
android:enabled="true"
android:exported="true">
<!--用于客户端调用-->
<intent-filter>
<action android:name="android.intent.action.MyService"/>
</intent-filter>
</service>
</application>

</manifest>


客户端

import com.test.service.IMyAidlInterface;

public class MainActivity extends Activity {

public static final String TAG = MainActivity.class.getSimpleName();
public static final int MEG_SUM = 0x1100;

private Handler mHandler = new Handler(){

@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
switch (msg.what) {
case MEG_SUM:
Toast.makeText(MainActivity.this, "接收到service消息==>" + msg.arg1, Toast.LENGTH_SHORT).show();
}

}
};

ServiceConnection connection = new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
iMyAidlInterface = IMyAidlInterface.Stub.asInterface(service);
try {
msg=  iMyAidlInterface.add(5,3);
Message message = Message.obtain();
message.what = MEG_SUM;
message.arg1 = msg;
mHandler.sendMessage(message);

Log.i(TAG, "onServiceConnected: "+msg);
} catch (RemoteException e) {
e.printStackTrace();
}

}
@Override
public void onServiceDisconnected(ComponentName name) {

}
};
private int msg;
private IMyAidlInterface iMyAidlInterface;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Intent intent = new Intent();
intent.setAction("android.intent.action.MyService");
intent.setPackage("com.test.service");
bindService(intent, connection, Context.BIND_AUTO_CREATE);

}

@Override
protected void onDestroy() {
super.onDestroy();
unbindService(connection);
}
}


代码下载链接这里写链接内容
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: