码迷,mamicode.com
首页 > 其他好文 > 详细

九 AIDL

时间:2016-05-01 16:13:47      阅读:164      评论:0      收藏:0      [点我收藏+]

标签:

 
进程
多线程
优点
安全
稳定
扩大内存空间
节约CPU时间
AIDL=Android Interface definition language
 
使用情况:做一个下载,不想让你的下载拖垮你的应用;播放器;ADK;
 
-----------------------------------------------------------------------------------------
目的
=Aidl接口+service+activity
=提供接口+输入信息+接收信息
 
aidl作用
=提供接口
 
声明
=另开进程
 
Service作用
=调用 AidlInterface.Stub+引用接口输入信息+返回(onBind,stub)
 
Activity作用
=绑定service+得到信息
 
步骤:
1、自动生成
    (1)aidl文件夹   main-aidl
   (2)自动创建AIDL       main-aidl-包-AIDL(右键创建)
    (3)自动生成相关代码    app-build-generated-source-aidl-debug-点运行
            会得到Interface
2、加入接口
----------------------------------------------------------------------------
3、输入信息  service
(1)创建aidl包    java-大包-aidl包
(2)创建AIDLService   
      ①继承
      ②绑定
      ③ 调用 Interface
      ④调用接口,放入信息
      ⑤声明另开进程
--------------------------------------------------------------------------------
4、接收信息 activity
  得到信息
 
 
 
----------------------------------------------------------------------------------------------------  
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>
service
import android.app.Service;
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;
}
}
activity
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];
}
};
}

九 AIDL

标签:

原文地址:http://www.cnblogs.com/sunyz/p/5450629.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!