最全Android AIDL:跨进程调用Service (AIDL Service),2024年最新大厂面试的员工什么水平

最后

其实Android开发的知识点就那么多,面试问来问去还是那么点东西。所以面试没有其他的诀窍,只看你对这些知识点准备的充分程度。so,出去面试时先看看自己复习到了哪个阶段就好。

上面分享的腾讯、头条、阿里、美团、字节跳动等公司2019-2021年的高频面试题,博主还把这些技术点整理成了视频和PDF(实际上比预期多花了不少精力),包含知识脉络 + 诸多细节,由于篇幅有限,上面只是以图片的形式给大家展示一部分。

【Android思维脑图(技能树)】

知识不体系?这里还有整理出来的Android进阶学习的思维脑图,给大家参考一个方向。

【Android高级架构视频学习资源】

**Android部分精讲视频领取学习后更加是如虎添翼!**进军BATJ大厂等(备战)!现在都说互联网寒冬,其实无非就是你上错了车,且穿的少(技能),要是你上对车,自身技术能力够强,公司换掉的代价大,怎么可能会被裁掉,都是淘汰末端的业务Curd而已!现如今市场上初级程序员泛滥,这套教程针对Android开发工程师1-6年的人员、正处于瓶颈期,想要年后突破自己涨薪的,进阶Android中高级、架构师对你更是如鱼得水,赶快领取吧!

网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。

需要这份系统化学习资料的朋友,可以戳这里获取

一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!

}

}

分别点击两个按钮同样可以看到输出的日志:

这里写图片描述

我们也可以在Service中重写 onStartCommand() 来接收其他应用传递过来的数据。

@Override

public int onStartCommand(Intent intent, int flags, int startId) {

return super.onStartCommand(intent, flags, startId);

// 做你想做的事

}

三、跨应用绑定Service

1. 创建AIDL文件

右键app包名新建.aidl文件:

  • 自动生成的代码如图中右边所示(IAppServiceRemoteBinder.aidl)。

  • 同时也自动新建了个aidl文件夹,.aidl文件就是在这个文件夹下(图中左边)。

这里写图片描述

Build->Make Project 发现在…debug目录下自动生成了对应的java接口文件

这里写图片描述

该文件代码如下:

/*

  • This file is auto-generated. DO NOT MODIFY.

  • Original file: F:\AndroidG\StartServiceFromAnotherApp\app\src\main\aidl\com\zhuanghongji\startservicefromanotherapp\IAppServiceRemoteBinder.aidl

*/

package com.zhuanghongji.startservicefromanotherapp;

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

public interface IAppServiceRemoteBinder extends android.os.IInterface {

/**

  • Local-side IPC implementation stub class.

*/

public static abstract class Stub extends android.os.Binder implements com.zhuanghongji.startservicefromanotherapp.IAppServiceRemoteBinder {

private static final java.lang.String DESCRIPTOR = “com.zhuanghongji.startservicefromanotherapp.IAppServiceRemoteBinder”;

/**

  • 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对象是在被调用端进程,也就是服务端进程

*/

在Service中重写onBind()方法:

@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 {

// …

}

};

}

这时重写AnotherApp中的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) {

}

}

重新安装后,点击“绑定外部服务”

这里写图片描述

会看到如下日志:

这里写图片描述

四、跨应用绑定Service并通信

  1. 在app 中的IAppServiceRemoteBinder增加接口方法:void setData(String data);

  2. 复制该文件的包名com.zhuanghongji.startservicefromanotherapp

  3. 在AnotherApp中新建一个AIDL Folder,再在里面新建一个包(包名是刚才复制的包名,否则会编译错误)。

  4. IAppServiceRemoteBinder.aidl文件整个复制到刚才新建的包下。

// 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 后在AppService中的onBind(Intent intent)中实现void setData(String data);

package com.zhuanghongji.startservicefromanotherapp;

public class AppService extends Service {

private String mData = “默认数据”;

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 {

// …

最后

分享一份工作1到5年以上的Android程序员架构进阶学习路线体系,希望能对那些还在从事Android开发却还不知道如何去提升自己的,还处于迷茫的朋友!

  • 阿里P7级Android架构师技术脑图;查漏补缺,体系化深入学习提升

  • **全套体系化高级架构视频;**七大主流技术模块,视频+源码+笔记

有任何问题,欢迎广大网友一起来交流

网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。

需要这份系统化学习资料的朋友,可以戳这里获取

一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!

Types(int anInt, long aLong, boolean aBoolean, float aFloat, double aDouble, String aString) throws RemoteException {

// …

最后

分享一份工作1到5年以上的Android程序员架构进阶学习路线体系,希望能对那些还在从事Android开发却还不知道如何去提升自己的,还处于迷茫的朋友!

  • 阿里P7级Android架构师技术脑图;查漏补缺,体系化深入学习提升

    [外链图片转存中…(img-5w7KfpvR-1715339433180)]

  • **全套体系化高级架构视频;**七大主流技术模块,视频+源码+笔记

[外链图片转存中…(img-rEy3dDL6-1715339433181)]

有任何问题,欢迎广大网友一起来交流

网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。

需要这份系统化学习资料的朋友,可以戳这里获取

一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!

  • 7
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值