android_aidl_comm

很多人对AIDL的使用还是不太清楚,自己也在运用中也遇到了不少麻烦,在这里我就简单示例了一个DEMO,之前也参考过相关的文章,所以在这里就直接贴源码了,希望能给有关人士带来帮助,并以思维导图的方式进行描述:

上面是贴图,下面就直接是源码了:

A_To_B:project

MainActivity.java:

package com.jsd.aidl;

import com.jsd.aidl.service.RemoteService;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

/**
 *
 * @author jiangshide
 *
 */
public class MainActivity extends Activity{

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    getViewById();
  }
 
  public void getViewById(){
    Button start = (Button) findViewById(R.id.startService);
    start.setOnClickListener(new OnClickListener() {
       @Override
       public void onClick(View v) {
         Intent intent = new Intent();
         intent.setClass(MainActivity.this, RemoteService.class);
         startService(intent);
       }
    });
  }
 
}
 
Data.aidl:

package com.jsd.aidl.service;

parcelable Data;

ICommonService.java:

package com.jsd.aidl.service;
import com.jsd.aidl.service.Data;
interface ICommonService{
  String addString(String str);
  void addData(in Data data);
  Data getData();
}

RemoteService.java:

package com.jsd.aidl.service;

import android.os.Parcel;
import android.os.Parcelable;
import android.util.Log;

/**
 *
 * @author jiangshide
 *
 */
public class Data implements Parcelable {
 
  private static String TAG = "Data";
  public Data(Parcel source){
    name = source.readString();
    type = source.readInt();
    addr = source.readString();
    Log.i(TAG, name);
  }
  public static final Parcelable.Creator<Data> CREATOR = new Parcelable.Creator<Data>() {
    @Override
    public Data createFromParcel(Parcel source) {
       return new Data(source);
    }

    @Override
    public Data[] newArray(int size) {
       Log.i(TAG, size+":");
       return new Data[size];
    }
   
  };
  @Override
  public int describeContents() {
    return 0;
  }
  @Override
  public void writeToParcel(Parcel dest, int flags) {
    Log.i("TAG", "dest:"+name);
    dest.writeString(name);
    dest.writeInt(type);
    dest.writeString(addr);
  }
 
  private String name;
  private int type;
  private String addr;
  public Data(){
  }
  public Data(String name ,int type,String addr){
    this.name = name;
    this.type = type;
    this.addr = addr;
  }
  public String getName() {
    return name;
  }
  public void setName(String name) {
    this.name = name;
  }
  public int getType() {
    return type;
  }
  public void setType(int type) {
    this.type = type;
  }
  public String getAddr() {
    return addr;
  }
  public void setAddr(String addr) {
    this.addr = addr;
  }
  @Override
  public String toString() {
    return "Data [name=" + name + ", type=" + type + ", addr=" + addr + "]";
  }
}
 

Data.java:

package com.jsd.aidl.service;

import android.os.Parcel;
import android.os.Parcelable;
import android.util.Log;

/**
 *
 * @author jiangshide
 *
 */
public class Data implements Parcelable {
 
  private static String TAG = "Data";
  public Data(Parcel source){
    name = source.readString();
    type = source.readInt();
    addr = source.readString();
    Log.i(TAG, name);
  }
  public static final Parcelable.Creator<Data> CREATOR = new Parcelable.Creator<Data>() {
    @Override
    public Data createFromParcel(Parcel source) {
       return new Data(source);
    }

    @Override
    public Data[] newArray(int size) {
       Log.i(TAG, size+":");
       return new Data[size];
    }
   
  };
  @Override
  public int describeContents() {
    return 0;
  }
  @Override
  public void writeToParcel(Parcel dest, int flags) {
    Log.i("TAG", "dest:"+name);
    dest.writeString(name);
    dest.writeInt(type);
    dest.writeString(addr);
  }
 
  private String name;
  private int type;
  private String addr;
  public Data(){
  }
  public Data(String name ,int type,String addr){
    this.name = name;
    this.type = type;
    this.addr = addr;
  }
  public String getName() {
    return name;
  }
  public void setName(String name) {
    this.name = name;
  }
  public int getType() {
    return type;
  }
  public void setType(int type) {
    this.type = type;
  }
  public String getAddr() {
    return addr;
  }
  public void setAddr(String addr) {
    this.addr = addr;
  }
  @Override
  public String toString() {
    return "Data [name=" + name + ", type=" + type + ", addr=" + addr + "]";
  }
}
 
AndroidManifest.xml:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.jsd.aidl"
      android:versionCode="1"
      android:versionName="1.0">
    <uses-sdk android:minSdkVersion="8" />

    <application android:icon="@drawable/icon" android:label="@string/app_name">
    <activity android:name=".MainActivity">
       <intent-filter>
         <action android:name="com.jsd.aidl.service.RemoteService"></action>
       </intent-filter>
    </activity>
   
    <service android:name="com.jsd.aidl.service.RemoteService" android:process=":remote">
      
    </service>

    </application>
</manifest>

B_To_A:

MainActivity.java:

package com.jsd.aidl;

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.EditText;
import android.widget.TextView;

import com.jsd.aidl.service.Data;
import com.jsd.aidl.service.ICommonService;

/**
 *
 * @author jiangshide
 *
 */
public class MainActivity extends Activity {
 
  private ICommonService mRemoteSerivce;
  private ServiceConnection mRemoteConnection = new ServiceConnection(){
    @Override
    public void onServiceConnected(ComponentName className, IBinder service) {
       mRemoteSerivce = ICommonService.Stub.asInterface(service);
    }
    @Override
    public void onServiceDisconnected(ComponentName arg0) {
       mRemoteSerivce = null;
    }
  };
 
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        mInputStr = (EditText)findViewById(R.id.input_str);
        mShowString = (TextView)findViewById(R.id.show_string);
       
        mInputName = (EditText)findViewById(R.id.input_name);
        mInputType = (EditText)findViewById(R.id.input_type);
        mShowObject = (TextView)findViewById(R.id.show_object);
       
        bindService(new Intent("org.guoyu.service.RemoteService"),
                 mRemoteConnection ,
                 Context.BIND_AUTO_CREATE);
       
    }
   
    public void addstring(View v){
    String inputStr = mInputStr.getText().toString();
    try {
       String result = mRemoteSerivce.addStr(inputStr);
       Data d = mRemoteSerivce.getData();
       if(inputStr.equals(result)){
         System.out.println("result:"+result);
         mShowString.setText("add success!add data for:"+result);
       }else{
         mShowString.setText("add success!add data for: fail");
       }
    } catch (RemoteException e) {
       mShowString.setText("Remote Connection ERROR");
    }
    }
   
    public void adddata(View v){
    String inputName = mInputName.getText().toString();
    int inputType = Integer.parseInt(mInputType.getText().toString());
    String inputStr = mInputStr.getText().toString();
    try {
       mRemoteSerivce.addData(new Data(inputName ,inputType, inputName));
    } catch (RemoteException e) {
       mShowObject.setText("add object ERROR");
    }
    mShowObject.setText("add object success");
    }
   
    public void getdata(View v){
    try {
       Data data = mRemoteSerivce.getData();
       mShowObject.setText(data.toString());
    } catch (RemoteException e) {
       mShowObject.setText("get object ERROR");
    }
    }
   
   
    @Override
    public void onDestroy(){
    unbindService(mRemoteConnection);
    super.onDestroy();
    }
   
  private EditText mInputStr;
  private EditText mInputName;
  private EditText mInputType;
  private TextView mShowString;
  private TextView mShowObject;
}

Data.aidl

package com.jsd.aidl.service;
parcelable Data;

ICommonService.aidl:

package com.jsd.aidl.service;
import com.jsd.aidl.service.Data;

interface ICommonService{
 String  addStr(String str);
 void addData(int Data data);
 Data getData();
}

Data.java:

package com.jsd.aidl.service;

import android.os.Parcel;
import android.os.Parcelable;

/**
 *
 * @author jiangshide
 *
 */
public class Data implements Parcelable {
  public Data(Parcel source){
    name = source.readString();
    type = source.readInt();
    addr = source.readString();
   
  }
  public static final Parcelable.Creator<Data> CREATOR = new Parcelable.Creator<Data>() {
    @Override
    public Data createFromParcel(Parcel source) {
       return new Data(source);
    }

    @Override
    public Data[] newArray(int size) {
       return new Data[size];
    }
   
  };
  @Override
  public int describeContents() {
    return 0;
  }
  @Override
  public void writeToParcel(Parcel dest, int flags) {
    dest.writeString(name);
    dest.writeInt(type);
    dest.writeString(addr);
  }
  private String name;
  private int type;
  private String addr;
  public Data(){
  }
  public Data(String name ,int type,String addr){
    this.name = name;
    this.type = type;
    this.addr = addr;
  }
  public String getName() {
    return name;
  }
  public void setName(String name) {
    this.name = name;
  }
  public int getType() {
    return type;
  }
  public void setType(int type) {
    this.type = type;
  }
  public String getAddr() {
    return addr;
  }
  public void setAddr(String addr) {
    this.addr = addr;
  }
  @Override
  public String toString() {
    return "Data [name=" + name + ", type=" + type + ", addr=" + addr + "]";
  }
}
 

AndroidManifest.xml:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.jsd.aidl"
      android:versionCode="1"
      android:versionName="1.0">
    <uses-sdk android:minSdkVersion="8" />

    <application android:icon="@drawable/icon" android:label="@string/app_name">
        <activity android:name="com.jsd.aidl.MainActivity"
                  android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

    </application>
</manifest>

以上就是一个简单的两个应用之间实现通行的简单过程,如果需要的朋友可以参看更详细的官方文档

posted @ 2011-06-21 16:02  移动应用开发  阅读(215)  评论(0编辑  收藏  举报