标签:
|
多进程
|
多线程
|
优点
|
安全
稳定
扩大内存空间
|
节约CPU时间
|
interface IMyAidlInterface {
/**
* 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);
String getName(String nickName);
}
<service
android:name=".aidl.AIDLService"
android:process="com.aidl.test.service"
android:enabled="true"
android:exported="true">
</service>
import android.app.Service;activity
import android.content.Intent;
import android.os.IBinder;
import android.os.RemoteException;
import android.support.annotation.Nullable;
import com.syz.lianxi.IMyAidlInterface;
public class AIDLService extends Service {
IMyAidlInterface.Stub mStub = new IMyAidlInterface.Stub(){
@Override
public void basicTypes(int anInt, long aLong, boolean aBoolean, float aFloat, double aDouble, String aString) throws RemoteException {
}
@Override
public String getName(String nickName) throws RemoteException {
return nickName + "aidl_hahaha";
}
};
@Nullable
@Override
public IBinder onBind(Intent intent) {
return mStub;
}
}
import android.app.Activity;-------------------------------------------------------------------------------------------------------
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.os.RemoteException;
import android.view.View;
import android.widget.Toast;
import com.syz.lianxi.IMyAidlInterface;
import com.syz.lianxi.R;
public class AIDLActivity extends Activity {
ServiceConnectionmServiceConnection = new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
mIMyAidlInterface = IMyAidlInterface.Stub.asInterface(service);
}
@Override
public void onServiceDisconnected(ComponentName name) {
}
};
private IMyAidlInterface mIMyAidlInterface;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_aidl);
findViewById(R.id.button_aidl).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(mIMyAidlInterface!=null){
try {
String name = mIMyAidlInterface.getName("nick_know_maco");
Toast.makeText(AIDLActivity.this, name + "", Toast.LENGTH_LONG).show();
} catch (RemoteException e) {
e.printStackTrace();
}
}
}
});
bindService(new Intent(this, AIDLService.class), mServiceConnection, Context.BIND_AUTO_CREATE);
}
}
import android.os.Parcel;
import android.os.Parcelable;
public class Person implements Parcelable {
String mName;
int mAge;
String mAvatarUrl;
public String getName() {
return mName;
}
public void setName(String name) {
mName = name;
}
public int getAge() {
return mAge;
}
public void setAge(int age) {
mAge = age;
}
public String getAvatarUrl() {
return mAvatarUrl;
}
public void setAvatarUrl(String avatarUrl) {
mAvatarUrl = avatarUrl;
}
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeString(this.mName);
dest.writeInt(this.mAge);
dest.writeString(this.mAvatarUrl);
}
public Person() {
}
protected Person(Parcel in) {
this.mName = in.readString();
this.mAge = in.readInt();
this.mAvatarUrl = in.readString();
}
public static final Parcelable.Creator<Person> CREATOR = new Parcelable.Creator<Person>() {
public Person createFromParcel(Parcel source) {
return new Person(source);
}
public Person[] newArray(int size) {
return new Person[size];
}
};
}
标签:
原文地址:http://www.cnblogs.com/sunyz/p/5450629.html