当前位置:网站首页>Android Aidl: cross process call service (Aidl service), kotlininvoke function
Android Aidl: cross process call service (Aidl service), kotlininvoke function
2022-06-24 00:07:00 【Ordinary netizens】
/**
- Construct the stub at attach it to the interface.
*/
public Stub() {
this.attachInterface(this, DESCRIPTOR);
}
/**
Cast an IBinder object into an com.zhuanghongji.startservicefromanotherapp.IAppServiceRemoteBinder interface,
generating a proxy if needed.
*/
public static com.zhuanghongji.startservicefromanotherapp.IAppServiceRemoteBinder asInterface(android.os.IBinder obj) {
if ((obj == null)) {
return null;
}
android.os.IInterface iin = obj.queryLocalInterface(DESCRIPTOR);
if (((iin != null) && (iin instanceof com.zhuanghongji.startservicefromanotherapp.IAppServiceRemoteBinder))) {
return ((com.zhuanghongji.startservicefromanotherapp.IAppServiceRemoteBinder) iin);
}
return new com.zhuanghongji.startservicefromanotherapp.IAppServiceRemoteBinder.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_basicTypes: {
data.enforceInterface(DESCRIPTOR);
int _arg0;
_arg0 = data.readInt();
long _arg1;
_arg1 = data.readLong();
boolean _arg2;
_arg2 = (0 != data.readInt());
float _arg3;
_arg3 = data.readFloat();
double _arg4;
_arg4 = data.readDouble();
java.lang.String _arg5;
_arg5 = data.readString();
this.basicTypes(_arg0, _arg1, _arg2, _arg3, _arg4, _arg5);
reply.writeNoException();
return true;
}
}
return super.onTransact(code, data, reply, flags);
}
private static class Proxy implements com.zhuanghongji.startservicefromanotherapp.IAppServiceRemoteBinder {
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;
}
/**
Demonstrates some basic types that you can use as parameters
and return values in AIDL.
*/
@Override
public void basicTypes(int anInt, long aLong, boolean aBoolean, float aFloat, double aDouble, java.lang.String aString) throws android.os.RemoteException {
android.os.Parcel _data = android.os.Parcel.obtain();
android.os.Parcel _reply = android.os.Parcel.obtain();
try {
_data.writeInterfaceToken(DESCRIPTOR);
_data.writeInt(anInt);
_data.writeLong(aLong);
_data.writeInt(((aBoolean) ? (1) : (0)));
_data.writeFloat(aFloat);
_data.writeDouble(aDouble);
_data.writeString(aString);
mRemote.transact(Stub.TRANSACTION_basicTypes, _data, _reply, 0);
_reply.readException();
} finally {
_reply.recycle();
_data.recycle();
}
}
}
static final int TRANSACTION_basicTypes = (android.os.IBinder.FIRST_CALL_TRANSACTION + 0);
}
/**
Demonstrates some basic types that you can use as parameters
and return values in AIDL.
*/
public void basicTypes(int anInt, long aLong, boolean aBoolean, float aFloat, double aDouble, java.lang.String aString) throws android.os.RemoteException;
}
/*
Stub Object is in the called process , That is, the server process
*/
stay Service Rewriting in onBind() Method :
@Override
public IBinder onBind(Intent intent) {
return new IAppServiceRemoteBinder.Stub() {
@Override
public void basicTypes(int anInt, long aLong, boolean aBoolean, float aFloat, double aDouble, String aString) throws RemoteException {
// …
}
};
}
Now rewrite AnotherApp Medium MainActivity:
public class MainActivity extends AppCompatActivity implements OnClickListener, ServiceConnection {
private Intent mServiceIntent;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
findViewById(R.id.btn_start_Service).setOnClickListener(this);
findViewById(R.id.btn_stop_Service).setOnClickListener(this);
findViewById(R.id.btn_bind_Service).setOnClickListener(this);
findViewById(R.id.btn_unbind_Service).setOnClickListener(this);
mServiceIntent = new Intent();
mServiceIntent.setComponent(new ComponentName(“com.zhuanghongji.startservicefromanotherapp”,
“com.zhuanghongji.startservicefromanotherapp.AppService”));
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.btn_start_Service:
startService(mServiceIntent);
break;
case R.id.btn_stop_Service:
stopService(mServiceIntent);
break;
case R.id.btn_bind_Service:
bindService(mServiceIntent, this, Context.BIND_AUTO_CREATE);
break;
case R.id.btn_unbind_Service:
unbindService(this);
break;
}
}
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
Log.i(“TAG”, "Bind Service : " + service);
}
@Override
public void onServiceDisconnected(ComponentName name) {
}
}
After reinstallation , Click on “ Bind external services ”

You will see the following log :

Four 、 Cross application binding Service And communication
stay app Medium IAppServiceRemoteBinder Add interface method :
void setData(String data);Copy the package name of the file
com.zhuanghongji.startservicefromanotherappstay AnotherApp Create a new one in
AIDL Folder, Create a new package inside ( The package name is the package name just copied , Otherwise it will compile incorrectly ).take
IAppServiceRemoteBinder.aidlCopy the entire file to the newly created package .
// IAppServiceRemoteBinder.aidl
package com.zhuanghongji.startservicefromanotherapp;
// Declare any non-default types here with import statements
interface IAppServiceRemoteBinder {
/**
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);
void setData(String data);
}
Build -> ReBuild Project In the after AppService Medium onBind(Intent intent) To realize void setData(String data);
package com.zhuanghongji.startservicefromanotherapp;
public class AppService extends Service {
private String mData = “ Default data ”;
private boolean isRunning = false;
public AppService() {
}
@Override
public void onCreate() {
super.onCreate();
Log.i(“TAG”, “AppService onCreate”);
new Thread(new Runnable() {
@Override
public void run() {
isRunning = true;
while (isRunning) {
Log.i(“TAG”, mData);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}).start();
}
@Override
public void onDestroy() {
super.onDestroy();
Log.i(“TAG”, “AppService onDestroy”);
isRunning = false;
}
@Override
public IBinder onBind(Intent intent) {
return new IAppServiceRemoteBinder.Stub() {
@Override
public void basicTypes(int anInt, long aLong, boolean aBoolean, float aFloat, double aDouble, String aString) throws RemoteException {
// …
}
@Override
public void setData(String data) throws RemoteException {
mData = data;
}
};
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
return super.onStartCommand(intent, flags, startId);
// Do what you want to do
}
}
At this time, be sure to App Of MainActivity Note out startService and stopService .
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// startService(new Intent(this, AppService.class));
}
@Override
protected void onDestroy() {
super.onDestroy();
// stopService(new Intent(this, AppService.class));
}
}
Run respectively app and anotherApp Then click “ Bind external services ” You will find that the log is printed every second “ Default data ”.
Click on “ Sync button ” Print after “ This is the data in another application ”.
So far, cross process communication is realized .
Here's how anotherApp The main layout and its xml Code :

<LinearLayout
xmlns:android=“http://schemas.android.com/apk/res/android”
xmlns:tools=“http://schemas.android.com/tools”
android:layout_width=“match_parent”
android:layout_height=“match_parent”
android:orientation=“vertical”
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity">
<TextView
android:layout_width=“wrap_content”
android:layout_height=“wrap_content”
android:text=“Hello World!”/>
<Button
android:id="@+id/btn_start_Service"
android:layout_width=“wrap_content”
android:layout_height=“wrap_content”
android:text=“ Start the service ”/>
<Button
android:id="@+id/btn_stop_Service"
android:layout_width=“wrap_content”
android:layout_height=“wrap_content”
android:text=“ Out of Service ”/>
<Button
android:id="@+id/btn_bind_Service"
android:layout_width=“wrap_content”
android:layout_height=“wrap_content”
android:text=“ Bind external services ”/>
<Button
android:id="@+id/btn_unbind_Service"
android:layout_width=“wrap_content”
android:layout_height=“wrap_content”
android:text=“ Unbind external service ”/>
<EditText
android:id="@+id/edit_text"
android:text=“ This is the data in another application ”
android:layout_width=“match_parent”
android:layout_height=“wrap_content”/>
<Button
《Android Summary of learning notes + Latest mobile architecture video + Big Android interview questions + Project actual combat source code handout 》
【docs.qq.com/doc/DSkNLaERkbnFoS0ZF】 Full content open source sharing
边栏推荐
- Classical Chinese can be programmed???
- Usage of go in SQL Server
- 【FreeRTOS】07 二值信号量、计数信号量
- Android App Bundle探索,客户端开发面试题目
- String s = new String(“xyz“) 创建了几个字符串对象?
- 人工智能技术岗位面试要注意什么?
- 【数字信号】基于matlab模拟窗函数频谱细化【含Matlab源码 1906期】
- 1.< tag-动态规划和路径组合问题>lt.62. 不同路径 + lt.63. 不同路径 II
- Go language core 36 lectures (go language practice and application 11) -- learning notes
- New function of lightweight application server: simple experience of offline business migration by using image sharing
猜你喜欢

High imitation Betta app

Generate all possible binary search trees

1.< tag-动态规划和路径组合问题>lt.62. 不同路径 + lt.63. 不同路径 II

Three types of transactions in EF core (saveChanges, dbcontexttransaction, transactionscope)

I was cheated by my colleagues to work overtime on weekends. I haven't seen redis used like this...

Six necessary open source projects for private activities

2018/gan:self attention generating adversarial networks

抖音实战~手机号密码一键注册登录流程(限制手机终端登录)

合成大西瓜小游戏微信小程序源码/微信游戏小程序源码

跟着CTF-wiki学pwn——ret2text
随机推荐
Taylor formula and common expansion
【面试经验包】面试被吊打经验总结(一)
WPF效果之Expander+ListBox
What is the difference between overload and override?
Jimureport building block report - table linkage chart topic
Windows10 security mode entry cycle blue screen repair
Easycvr implementation of adding preset point position function of Dahua equipment
Web site SSL certificate
js 语言 精度问题
Revit API: schedule viewschedule
测试 - 用例篇 - 细节狂魔
return、const、volatile关键字
Android App Bundle探索,客户端开发面试题目
When the IOT network card device is connected to easycvr, how can I view the streaming IP and streaming time?
Three cool and coquettish bottom navigation
并发和并行有什么区别?
Jeecgboot old version 2 x 3. X how to integrate building block reports?
Solve the problem of project dependency red reporting
[technical grass planting] Tencent Yunhao wool (consumption) record on the double 11
2021-11-23: Regulations: l[1] corresponds to a, l[2] corresponds to B, l[3] corresponds to C