// AidlInterface.aidl
package com.example.tee.testapplication.aidl;
// Declare any non-default types here with import statements
interface AidlInterface {
/**
* 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);
}
package com.example.tee.testapplication.aidl;
interface IMyAidlInterface {
String getValue();
}
public class MAIDLService extends Service{
public class MAIDLServiceImpl extends IMyAidlInterface.Stub{
@Override
public String getValue() throws RemoteException {
return "get value";
}
}
@Nullable
@Override
public IBinder onBind(Intent intent) {
return new MAIDLServiceImpl();
}
}
<service android:name=".MAIDLService" android:process=":remote"//加上这句的话客户端调用会创建一个新的进程 android:exported="true"//默认就为true,可去掉,声明是否可以远程调用 > <intent-filter> <category android:name="android.intent.category.DEFAULT" /> <action android:name="com.example.tee.testapplication.aidl.IMyAidlInterface" /> </intent-filter> </service>
public class MainActivity extends AppCompatActivity {
private TextView mValueTV;
private IMyAidlInterface mAidlInterface = null;
private ServiceConnection mServiceConnection = new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
mAidlInterface = IMyAidlInterface.Stub.asInterface(service);
}
@Override
public void onServiceDisconnected(ComponentName name) {
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
Intent intent = new Intent("com.example.tee.testapplication.aidl.IMyAidlInterface");
bindService(intent, mServiceConnection, BIND_AUTO_CREATE);
mValueTV = (TextView) findViewById(R.id.tv_test_value);
mValueTV.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
try {
mValueTV.setText(mAidlInterface.getValue());
} catch (RemoteException e) {
e.printStackTrace();
}
}
});
}
@Override
protected void onDestroy() {
if(mAidlInterface != null){
unbindService(mServiceConnection);
}
super.onDestroy();
}
}
public class Student implements Parcelable{
public String name;
public int age;
protected Student(Parcel in) {
readFromParcel(in);
}
public Student() {
}
public static final Creator<Student> CREATOR = new Creator<Student>() {
@Override
public Student createFromParcel(Parcel in) {
return new Student(in);
}
@Override
public Student[] newArray(int size) {
return new Student[size];
}
};
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeInt(age);
dest.writeString(name);
}
public void readFromParcel(Parcel in){
age = in.readInt();
name = in.readString();
}
@Override
public String toString() {
return String.format(Locale.ENGLISH, "STUDENT[%s:%d]", name, age);
}
}
// Student.aidl package com.example.tee.testapplication.aidl; // Declare any non-default types here with import statements parcelable Student;
// IMyAidlInterface.aidl
package com.example.tee.testapplication.aidl;
// Declare any non-default types here with import statements
import com.example.tee.testapplication.aidl.Student;
interface IMyAidlInterface {
Student getStudent();
void setStudent(in Student student);
String getValue();
}
private Student mStudent;
public class MAIDLServiceImpl extends IMyAidlInterface.Stub{
@Override
public Student getStudent() throws RemoteException {
return mStudent;
}
@Override
public void setStudent(Student student) throws RemoteException {
mStudent = student;
}
@Override
public String getValue() throws RemoteException {
return "get value : " + Thread.currentThread().getName() + Thread.currentThread().getId();
}
}
mValueTV = (TextView) findViewById(R.id.tv_test_value);
mStudentTV = (TextView) findViewById(R.id.tv_test_student);
mValueTV.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
try {
mValueTV.setText(mAidlInterface.getValue());
} catch (RemoteException e) {
e.printStackTrace();
}
}
});
mStudentTV.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
try {
Student student = new Student();
student.age = 10;
student.name = "Tom";
mAidlInterface.setStudent(student);
mStudentTV.setText(mAidlInterface.getStudent().toString());
} catch (RemoteException e) {
e.printStackTrace();
}
}
});
android {
sourceSets {
main {
manifest.srcFile 'src/main/AndroidManifest.xml'
java.srcDirs = ['src/main/java', 'src/main/aidl']
resources.srcDirs = ['src/main/java', 'src/main/aidl']
aidl.srcDirs = ['src/main/aidl']
res.srcDirs = ['src/main/res']
assets.srcDirs = ['src/main/assets']
}
}
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有