Android AIDL-Service demo版
AIDL是用来定义两个进程之间的通信接口,语法简单,相当于(类似)java的接口写法:
<1> : AIDL定义接口的源代码必须以.aidl为文件扩展名;
<2> : 数据类型,除了基本类型,String,List,Map,CharSequence之外,其他的都要导入相关包;
实现这种接口的是aidl.exe,这个exe放在android SDK下.
下面给出一个demo:
先写AIDL文件:eclipse工程下直接新建一个文本ICat.aidl文件
package com.example.androidaidlserivcedemo0; interface ICat{ String getColor(); double getWeight(); }
eclipse会自动生成ICat.java类,在gen文件夹下,ICat接口包含一个Stub内部类,该内部类实现了IBinder,ICat两个接口,这个Stub类将会作为远程Service的回调类,实现IBinder,因此可作为Service的onBind()方法的返回值.
Service子类如下:
package com.example.androidaidlserivcedemo0; import java.util.Timer; import java.util.TimerTask; import com.example.androidaidlserivcedemo0.ICat.Stub; import android.app.Service; import android.content.Intent; import android.os.IBinder; import android.os.RemoteException; import android.util.Log; public class AIDLSerivce extends Service{ private final String TAG="AIDLSerivce"; private CatBinder catBinder; Timer timer=new Timer(); String[] colors=new String[]{ "red", "yellow", "black" }; double[] weights=new double[]{ 2.3, 3.1, 1.58 }; private String color; private double weight; public class CatBinder extends Stub{//此处不再是继承Binder,而是继承即实现ICat和IBinder的接口 @Override public String getColor() throws RemoteException { // TODO Auto-generated method stub return color; } @Override public double getWeight() throws RemoteException { // TODO Auto-generated method stub return weight; } } @Override public IBinder onBind(Intent intent) { // TODO Auto-generated method stub /** * 返回catBinder对象 * 在绑定本地Service的情况下,该catBinder对象会直接传给客户端的ServiceConnection对象 * 的第二个参数 * 在绑定远程Service的情况下,只将catBinder对象的代理 * 传给客户端的ServiceConnection对象 * 的OnServiceConnection方法的第二个参数 */ return catBinder; } @Override public void onCreate() { // TODO Auto-generated method stub super.onCreate(); catBinder=new CatBinder(); timer.schedule(new TimerTask(){ @Override public void run() { // TODO Auto-generated method stub int rand=(int)(Math.random()*3); color=colors[rand]; weight=weights[rand]; Log.i(TAG,"rand : " + rand); } }, 0,800); } }
主类:
package com.example.androidaidlserivcedemo0; import android.os.Bundle; import android.os.IBinder; import android.os.RemoteException; import android.app.Activity; import android.app.Service; import android.content.ComponentName; import android.content.Intent; import android.content.ServiceConnection; import android.view.Menu; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.EditText; public class MainActivity extends Activity { private EditText mColorText,mWeightText; private ICat catService; private Button mServiceButton; private ServiceConnection conn=new ServiceConnection(){ @Override public void onServiceConnected(ComponentName name, IBinder service) { // TODO Auto-generated method stub catService=ICat.Stub.asInterface(service); } @Override public void onServiceDisconnected(ComponentName name) { // TODO Auto-generated method stub catService=null; } }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mServiceButton=(Button)findViewById(R.id.button1); mColorText=(EditText)findViewById(R.id.editText1); mWeightText=(EditText)findViewById(R.id.editText2); Intent intent=new Intent(); intent.setAction("com.example.androidaidlserivcedemo0.AIDL_SERVICE"); bindService(intent,conn,Service.BIND_AUTO_CREATE); mServiceButton.setOnClickListener(new OnClickListener(){ @Override public void onClick(View v) { // TODO Auto-generated method stub try { mColorText.setText(catService.getColor()); mWeightText.setText(catService.getWeight()+""); } catch (RemoteException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }); } @Override protected void onDestroy() { // TODO Auto-generated method stub super.onDestroy(); this.unbindService(conn); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } }
布局:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context=".MainActivity" > <EditText android:id="@+id/editText1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignLeft="@+id/button1" android:layout_alignParentTop="true" android:layout_marginTop="22dp" android:ems="10" > <requestFocus /> </EditText> <Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_below="@+id/editText1" android:layout_marginLeft="61dp" android:layout_marginTop="96dp" android:text="service status" /> <EditText android:id="@+id/editText2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignLeft="@+id/button1" android:layout_below="@+id/editText1" android:layout_marginTop="34dp" android:ems="10" /> </RelativeLayout>
效果图: