学习分享
①「Android面试真题解析大全」PDF完整高清版+②「Android面试知识体系」学习思维导图压缩包
网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。
一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!
}
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并通信
-
在app 中的IAppServiceRemoteBinder增加接口方法:
void setData(String data);
-
复制该文件的包名
com.zhuanghongji.startservicefromanotherapp
-
在AnotherApp中新建一个
AIDL Folder
,再在里面新建一个包(包名是刚才复制的包名,否则会编译错误)。 -
将
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 {
// …
}
@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);
// 做你想做的事
}
}
此时务必要在App的MainActivity中注释掉 startService
和 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));
}
}
分别运行app和anotherApp后点击“绑定外部服务”后会发现日志每隔一秒打印出“默认数据”。
点击“同步按钮”后变成打印“这是另一个应用中的数据”。
至此就实现了跨进程间的通信。
下面给出anotherApp的主布局及其xml代码:
<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
总结
笔者之前工作是在金融公司可能并不是特别追求技术,而笔者又是喜欢追求技术的人,所以格格不入,只能把目标放在互联网大厂了。也希望大家都去敢于尝试和追逐自己的梦想!
BATJ大厂Android高频面试题
觉得有收获的记得点赞,关注+收藏哦!你们的点赞就是我的动力!
网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。
一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!
out_height=“wrap_content”
android:text=“Hello World!”/>
<Button
总结
笔者之前工作是在金融公司可能并不是特别追求技术,而笔者又是喜欢追求技术的人,所以格格不入,只能把目标放在互联网大厂了。也希望大家都去敢于尝试和追逐自己的梦想!
BATJ大厂Android高频面试题
[外链图片转存中…(img-uZr1mC7x-1715839275371)]
[外链图片转存中…(img-XnVkN1DU-1715839275371)]
[外链图片转存中…(img-BW7KdaQv-1715839275372)]
[外链图片转存中…(img-h9SRVlfd-1715839275372)]
觉得有收获的记得点赞,关注+收藏哦!你们的点赞就是我的动力!
网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。
一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!