package com.soling.aidlapplication;
import android.app.Service;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.os.RemoteException;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import com.soling.aidl.IMyAidlInterface;
import com.soling.aidl.Person;
import java.util.List;
import butterknife.ButterKnife;
import butterknife.InjectView;
import butterknife.OnClick;
public class RemoteClientActivity extends AppCompatActivity {
@InjectView(R.id.tv1)
TextView tv1;
@InjectView(R.id.btn1)
Button btn1;
@InjectView(R.id.btn2)
Button btn2;
@InjectView(R.id.btn3)
Button btn3;
@InjectView(R.id.tv2)
TextView tv2;
private IMyAidlInterface mAsInterface;
private int index = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ButterKnife.inject(this);
Intent service = new Intent("com.soling.aidlservice.RemoteService");
ServiceConnection conn = new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
mAsInterface = IMyAidlInterface.Stub.asInterface(service);
}
@Override
public void onServiceDisconnected(ComponentName name) {
mAsInterface = null;
}
};
bindService(service, conn, Service.BIND_AUTO_CREATE);
}
@OnClick({R.id.btn1, R.id.btn2, R.id.btn3})
public void onViewClicked(View view) {
switch (view.getId()) {
case R.id.btn1:
try {
if (tv1 != null)
tv1.setText(mAsInterface.sayHello());
} catch (RemoteException e) {
e.printStackTrace();
}
break;
case R.id.btn2:
index++;
Person person = new Person();
person.setName("张三" + index);
person.setAge(20 + index);
person.setTelNumber("12345" + index);
try {
mAsInterface.savePersonInfo(person);
} catch (RemoteException e) {
e.printStackTrace();
}
break;
case R.id.btn3:
try {
List<Person> allPerson = mAsInterface.getAllPerson();
if (allPerson != null) {
StringBuilder stringBuilder = new StringBuilder();
for (int i = 0; i < allPerson.size(); i++) {
stringBuilder.append(allPerson.get(i).getName());
stringBuilder.append("/n 联系人:" + allPerson.get(i).getName());
stringBuilder.append("/n 电 话:" + allPerson.get(i).getTelNumber());
stringBuilder.append("/n 年 龄:" + allPerson.get(i).getAge());
}
tv2.setText(stringBuilder);
}else {
Toast.makeText(this, "请求数据为空,服务器端暂无数据", Toast.LENGTH_SHORT).show();
}
} catch (RemoteException e) {
e.printStackTrace();
}
break;
}
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
温馨提示
在studio上写的一个完整的AIDL Demo,里面有客户端和服务端两个应用,通过AIDL传递parcelable对象,具体看代码,已删除build目录,所以包比较小,下下来之后需要先编译一下,compileSdkVersion 26,targetSdkVersion 26,不适配的自己改
资源推荐
资源详情
资源评论
收起资源包目录
TestAIDL.zip (51个子文件)
aidlapplication
proguard-rules.pro 933B
aidlapplication.iml 10KB
src
test
java
com
soling
aidlapplication
ExampleUnitTest.java 420B
androidTest
java
com
soling
aidlapplication
ExampleInstrumentedTest.java 782B
main
res
mipmap-xxxhdpi
ic_launcher.png 10KB
ic_launcher_round.png 14KB
drawable
mipmap-hdpi
ic_launcher.png 3KB
ic_launcher_round.png 4KB
values
colors.xml 214B
strings.xml 81B
styles.xml 394B
mipmap-xxhdpi
ic_launcher.png 8KB
ic_launcher_round.png 10KB
mipmap-mdpi
ic_launcher.png 2KB
ic_launcher_round.png 2KB
mipmap-xhdpi
ic_launcher.png 5KB
ic_launcher_round.png 6KB
layout
activity_main.xml 2KB
aidl
com
soling
aidl
Person.aidl 128B
IMyAidlInterface.aidl 407B
java
com
soling
aidl
Person.java 1KB
aidlapplication
RemoteClientActivity.java 4KB
AndroidManifest.xml 762B
.gitignore 8B
build.gradle 956B
libs
aidlservice
proguard-rules.pro 933B
aidlservice.iml 10KB
src
test
java
com
soling
aidlservice
ExampleUnitTest.java 416B
androidTest
java
com
soling
aidlservice
ExampleInstrumentedTest.java 774B
main
res
mipmap-xxxhdpi
ic_launcher.png 10KB
ic_launcher_round.png 14KB
drawable
mipmap-hdpi
ic_launcher.png 3KB
ic_launcher_round.png 4KB
values
colors.xml 214B
strings.xml 77B
styles.xml 394B
mipmap-xxhdpi
ic_launcher.png 8KB
ic_launcher_round.png 10KB
mipmap-mdpi
ic_launcher.png 2KB
ic_launcher_round.png 2KB
mipmap-xhdpi
ic_launcher.png 5KB
ic_launcher_round.png 6KB
layout
activity_main.xml 535B
aidl
com
soling
aidl
Person.aidl 128B
IMyAidlInterface.aidl 407B
java
com
soling
aidl
Person.java 1KB
aidlservice
RemoteService.java 1KB
MainActivity.java 348B
AndroidManifest.xml 957B
.gitignore 8B
build.gradle 903B
libs
共 51 条
- 1
资源评论
- qq_354464922019-07-06还可以哦的。。。。。
Mryang121
- 粉丝: 2
- 资源: 3
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功