首先,为什么要用aidl
---------------------------------------------------------------------------------------------------------------------------
package aid; interface ExecuteMyAidlService { String sayHello(); }MyAidlService.java文件
package aidlservice; import aid.ExecuteMyAidlService; import android.app.Service; import android.content.Intent; import android.os.IBinder; import android.os.RemoteException; public class MyAidlService extends Service { private static final String TAG = "MyAidlService"; private ExecuteMyAidlService.Stub mBinder = new ExecuteMyAidlService.Stub() { @Override public String sayHello() throws RemoteException { // TODO Auto-generated method stub return "hello "; } }; @Override public IBinder onBind(Intent intent) { // TODO Auto-generated method stub return mBinder; } private void Log(String str) { android.util.Log.d(TAG, "------ " + str + "------"); } @Override public void onCreate() { Log("service create"); } @Override public void onStart(Intent intent, int startId) { Log("service start id=" + startId); } }activity里面没有东西就不贴了
package com.example.aidlclienttest; import aid.ExecuteMyAidlService; import android.support.v7.app.ActionBarActivity; import android.content.ComponentName; import android.content.Intent; import android.content.ServiceConnection; import android.os.Bundle; import android.os.IBinder; import android.os.RemoteException; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.TextView; public class ClientActivity extends ActionBarActivity { private ExecuteMyAidlService mIaidlServerService = null; private TextView mTextView = null; private Button mButton = null; private ServiceConnection mConnection = new ServiceConnection() { public void onServiceDisconnected(ComponentName name) { mIaidlServerService = null; } public void onServiceConnected(ComponentName name, IBinder service) { mIaidlServerService = ExecuteMyAidlService.Stub.asInterface(service); //aidl通信 try { String mText = "Say hello: " + mIaidlServerService.sayHello(); mTextView.setText(mText); } catch (RemoteException e) { e.printStackTrace(); } } }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_client); mTextView = (TextView)findViewById(R.id.helloword); mButton = (Button)findViewById(R.id.getServiceFromAidl); mButton.setOnClickListener(new OnClickListener(){ @Override public void onClick(View v) { // TODO Auto-generated method stub Intent service = new Intent("aidl.ExecuteMyAidlService"); service.setAction("aidl.ExecuteMyAidlService"); bindService(service, mConnection,BIND_AUTO_CREATE); } }); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.client, menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { // Handle action bar item clicks here. The action bar will // automatically handle clicks on the Home/Up button, so long // as you specify a parent activity in AndroidManifest.xml. int id = item.getItemId(); if (id == R.id.action_settings) { return true; } return super.onOptionsItemSelected(item); } }
parcelable Object
package aid; import android.os.Parcel; import android.os.Parcelable; import android.os.Parcelable.Creator; public class Person implements Parcelable{ private String name; private int age; private String place; public Person(Parcel source) { // TODO Auto-generated constructor stub name = source.readString(); age = source.readInt(); place = source.readString(); } @Override public String toString() { return "Person [name=" + name + ", age=" + age + ", place=" + place + "]"; } public Person() { // TODO Auto-generated constructor stub } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public String getPlace() { return place; } public void setPlace(String place) { this.place = place; } @Override public int describeContents() { // TODO Auto-generated method stub return 0; } @Override public void writeToParcel(Parcel dest, int flags) { // TODO Auto-generated method stub dest.writeString(name); dest.writeInt(age); dest.writeString(place); } public static final Parcelable.Creator<Person> CREATOR = new Creator<Person>(){ @Override public Person createFromParcel(Parcel source) { // TODO Auto-generated method stub Person person = new Person(source); return person; } @Override public Person[] newArray(int size) { // TODO Auto-generated method stub return new Person[size]; }}; }同样也要写一个aidl文件
private ServiceConnection mConnection = new ServiceConnection() { public void onServiceDisconnected(ComponentName name) { mIaidlServerService = null; } public void onServiceConnected(ComponentName name, IBinder service) { mIaidlServerService = ExecuteMyAidlService.Stub.asInterface(service); //aidl通信 try { String mText = "Say hello: " + mIaidlServerService.sayHello(); Person mPerson = mIaidlServerService.getDefaultPerson(); System.out.println(mPerson); mPerson.setAge(mPerson.getAge()+1);
<span style="white-space:pre"> </span>//这里向service传入对象 mIaidlServerService.tellMeHowOldAmI(mPerson,mClientCallBack); mTextView.setText(mText); } catch (RemoteException e) { e.printStackTrace(); } } };
ClientCallBack.Stub mClientCallBack = new ClientCallBack.Stub() { <span style="white-space:pre"> </span> <span style="white-space:pre"> </span>@Override <span style="white-space:pre"> </span>public void handleByServer(String param) throws RemoteException { <span style="white-space:pre"> </span>// TODO Auto-generated method stub <span style="white-space:pre"> </span>Toast.makeText(getApplicationContext(), param, Toast.LENGTH_LONG).show(); <span style="white-space:pre"> </span>} <span style="white-space:pre"> </span>};
@Override public String tellMeHowOldAmI(Person person,ClientCallBack mClientCallBack) throws RemoteException { // TODO Auto-generated method stub mCallBack = mClientCallBack; mCallBack.handleByServer("your age is "+String.valueOf(person.getAge())); return String.valueOf(person.getAge()); } };
原文地址:http://blog.csdn.net/xyz5354/article/details/39929653