多進程 多線程 優點 安全 穩定 擴大記憶體空間 節約CPU時間 AIDL=Android Interface definition language 多進程 多線程 優點 安全 穩定 擴大記憶體空間 節約CPU時間 使用情況:做一個下載,不想讓你的下載拖垮你的應用;播放器;ADK; 目的 =Aidl接 ...
多進程 | 多線程 | |
優點 | 安全 穩定 擴大記憶體空間 | 節約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);
}
<serviceservice
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];
}
};
}