2.1.4、配置文件注册Service并启动Service
配置文件注册:
<service
android:name=“.IRemoteService”
android:exported=“true” />
启动Service:
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Intent intent = new Intent(this, IRemoteService.class);
startService(intent);
}
}
2.2、客户端
在同项目中新建一个名为client的module
2.2.1、创建客户端界面
ClientActivity
public class ClientActivity extends AppCompatActivity {
EditText etNum1, etNum2;
TextView tvRes;
Button btnAdd;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_client);
etNum1 = findViewById(R.id.et_num1);
etNum2 = findViewById(R.id.et_num2);
tvRes = findViewById(R.id.tv_res);
btnAdd = findViewById(R.id.btn_add);
btnAdd.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
}
});
}
}
布局文件:
<?xml version="1.0" encoding="utf-8"?><androidx.constraintlayout.widget.ConstraintLayout xmlns:android=“http://schemas.android.com/apk/res/android”
xmlns:app=“http://schemas.android.com/apk/res-auto”
xmlns:tools=“http://schemas.android.com/tools”
android:layout_width=“match_parent”
android:layout_height=“match_parent”
tools:context=“.ClientActivity”>
<EditText
android:id=“@+id/et_num1”
android:layout_width=“match_parent”
android:layout_height=“wrap_content”
android:hint=“输入第一个数字”
android:textSize=“22sp”
app:layout_constraintLeft_toLeftOf=“parent”
app:layout_constraintRight_toRightOf=“parent”
app:layout_constraintTop_toTopOf=“parent” />
<TextView
android:id=“@+id/tv_add”
android:layout_width=“wrap_content”
android:layout_height=“wrap_content”
android:text=“+”
android:textSize=“100sp”
app:layout_constraintLeft_toLeftOf=“parent”
app:layout_constraintRight_toRightOf=“parent”
app:layout_constraintTop_toBottomOf=“@+id/et_num1” />
<EditText
android:id=“@+id/et_num2”
android:layout_width=“match_parent”
android:layout_height=“wrap_content”
android:hint=“输入第二个数字”
android:textSize=“22sp”
app:layout_constraintLeft_toLeftOf=“parent”
app:layout_constraintRight_toRightOf=“parent”
app:layout_constraintTop_toBottomOf=“@+id/tv_add” />
<TextView
android:id=“@+id/tv_res”
android:layout_width=“wrap_content”
android:layout_height=“wrap_content”
android:layout_marginTop=“10dp”
android:text=“计算结果”
android:textSize=“40sp”
app:layout_constraintLeft_toLeftOf=“parent”
app:layout_constraintRight_toRightOf=“parent”
app:layout_constraintTop_toBottomOf=“@+id/et_num2” />
<Button
android:id=“@+id/btn_add”
android:layout_width=“match_parent”
android:layout_height=“wrap_content”
android:layout_marginTop=“10dp”
android:text=“计算”
android:textSize=“22sp”
app:layout_constraintLeft_toLeftOf=“parent”
app:layout_constraintRight_toRightOf=“parent”
app:layout_constraintTop_toBottomOf=“@+id/tv_res” />
</androidx.constraintlayout.widget.ConstraintLayout>
2.2.2、把服务端的aidl文件拷贝到客户端
2.2.3、绑定服务并拿取数据
public class ClientActivity extends AppCompatActivity {
private static final String TAG = “ClientActivity”;
EditText etNum1, etNum2;
TextView tvRes;
Button btnAdd;
IImoocAidl iImoocAidl;
private ServiceConnection mConn = new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName componentName, IBinder iBinder) {
// 拿到了远程服务
iImoocAidl = IImoocAidl.Stub.asInterface(iBinder);
}
@Override
public void onServiceDisconnected(ComponentName componentName) {
// 回收资源
iImoocAidl = null;
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_client);
etNum1 = findViewById(R.id.et_num1);
etNum2 = findViewById(R.id.et_num2);
tvRes = findViewById(R.id.tv_res);
btnAdd = findViewById(R.id.btn_add);
bindService();
btnAdd.setOnClickListener(view -> {
int num1 = Integer.parseInt(etNum1.getText().toString());
int num2 = Integer.parseInt(etNum2.getText().toString());
try {
// 调用远程服务
if (null != iImoocAidl) {
int res = iImoocAidl.add(num1, num2);
tvRes.setText(“计算结果为:” + res);
} else {
Log.e(TAG, “iImoocAidl is null!”);
}
} catch (RemoteException e) {
e.printStackTrace();
tvRes.setText(“Error!”);
}
});
}
建议
当我们出去找工作,或者准备找工作的时候,我们一定要想,我面试的目标是什么,我自己的技术栈有哪些,近期能掌握的有哪些,我的哪些短板 ,列出来,有计划的去完成,别看前两天掘金一些大佬在驳来驳去 ,他们的观点是他们的,不要因为他们的观点,膨胀了自己,影响自己的学习节奏。基础很大程度决定你自己技术层次的厚度,你再熟练框架也好,也会比你便宜的,性价比高的替代,很现实的问题但也要有危机意识,当我们年级大了,有哪些亮点,与比我们经历更旺盛的年轻小工程师,竞争。
-
无论你现在水平怎么样一定要 持续学习 没有鸡汤,别人看起来的毫不费力,其实费了很大力,这四个字就是我的建议!!!!!!!!!
-
准备想说怎么样写简历,想象算了,我觉得,技术就是你最好的简历
-
我希望每一个努力生活的it工程师,都会得到自己想要的,因为我们很辛苦,我们应得的。
-
有什么问题想交流,欢迎给我私信,欢迎评论
【附】相关架构及资料
内含往期Android高级架构资料、源码、笔记、视频。高级UI、性能优化、架构师课程、NDK、混合式开发(ReactNative+Weex)微信小程序、Flutter全方面的Android进阶实践技术
网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。
一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!
14258567890)]
[外链图片转存中…(img-9uX9y0FG-1714258567891)]
内含往期Android高级架构资料、源码、笔记、视频。高级UI、性能优化、架构师课程、NDK、混合式开发(ReactNative+Weex)微信小程序、Flutter全方面的Android进阶实践技术
网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。
一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!