package com.xxx.xxx; interface IService { String localcity(); byte[] bitmapbyte(); }顶端是包名,即当前文件所在的包名,IService为接口名,函数内则是后面需要实现的接口函数。
/* * This file is auto-generated. DO NOT MODIFY. * Original file: D:\\Android-APK-CODE\\2015-No Protect\\car\\circle\\code\\PvWeather_3.0\\src\\com\\pve\\weatherhz\\IService.aidl */ package com.pve.weatherhz; public interface IService extends android.os.IInterface { /** Local-side IPC implementation stub class. */ public static abstract class Stub extends android.os.Binder implements com.pve.weatherhz.IService { private static final java.lang.String DESCRIPTOR = "com.pve.weatherhz.IService"; /** Construct the stub at attach it to the interface. */ public Stub() { this.attachInterface(this, DESCRIPTOR); } /** * Cast an IBinder object into an com.pve.weatherhz.IService interface, * generating a proxy if needed. */ public static com.pve.weatherhz.IService asInterface(android.os.IBinder obj) { if ((obj==null)) { return null; } android.os.IInterface iin = obj.queryLocalInterface(DESCRIPTOR); if (((iin!=null)&&(iin instanceof com.pve.weatherhz.IService))) { return ((com.pve.weatherhz.IService)iin); } return new com.pve.weatherhz.IService.Stub.Proxy(obj); } @Override public android.os.IBinder asBinder() { return this; } @Override public boolean onTransact(int code, android.os.Parcel data, android.os.Parcel reply, int flags) throws android.os.RemoteException { switch (code) { case INTERFACE_TRANSACTION: { reply.writeString(DESCRIPTOR); return true; } case TRANSACTION_weather: { data.enforceInterface(DESCRIPTOR); java.lang.String _result = this.weather(); reply.writeNoException(); reply.writeString(_result); return true; } case TRANSACTION_temperature: { data.enforceInterface(DESCRIPTOR); java.lang.String _result = this.temperature(); reply.writeNoException(); reply.writeString(_result); return true; } case TRANSACTION_localcity: { data.enforceInterface(DESCRIPTOR); java.lang.String _result = this.localcity(); reply.writeNoException(); reply.writeString(_result); return true; } case TRANSACTION_weatherinfo: { data.enforceInterface(DESCRIPTOR); this.weatherinfo(); reply.writeNoException(); return true; } case TRANSACTION_bitmapbyte: { data.enforceInterface(DESCRIPTOR); byte[] _result = this.bitmapbyte(); reply.writeNoException(); reply.writeByteArray(_result); return true; } } return super.onTransact(code, data, reply, flags); } private static class Proxy implements com.pve.weatherhz.IService { private android.os.IBinder mRemote; Proxy(android.os.IBinder remote) { mRemote = remote; } @Override public android.os.IBinder asBinder() { return mRemote; } public java.lang.String getInterfaceDescriptor() { return DESCRIPTOR; } @Override public java.lang.String weather() throws android.os.RemoteException { android.os.Parcel _data = android.os.Parcel.obtain(); android.os.Parcel _reply = android.os.Parcel.obtain(); java.lang.String _result; try { _data.writeInterfaceToken(DESCRIPTOR); mRemote.transact(Stub.TRANSACTION_weather, _data, _reply, 0); _reply.readException(); _result = _reply.readString(); } finally { _reply.recycle(); _data.recycle(); } return _result; } @Override public java.lang.String temperature() throws android.os.RemoteException { android.os.Parcel _data = android.os.Parcel.obtain(); android.os.Parcel _reply = android.os.Parcel.obtain(); java.lang.String _result; try { _data.writeInterfaceToken(DESCRIPTOR); mRemote.transact(Stub.TRANSACTION_temperature, _data, _reply, 0); _reply.readException(); _result = _reply.readString(); } finally { _reply.recycle(); _data.recycle(); } return _result; } @Override public java.lang.String localcity() throws android.os.RemoteException { android.os.Parcel _data = android.os.Parcel.obtain(); android.os.Parcel _reply = android.os.Parcel.obtain(); java.lang.String _result; try { _data.writeInterfaceToken(DESCRIPTOR); mRemote.transact(Stub.TRANSACTION_localcity, _data, _reply, 0); _reply.readException(); _result = _reply.readString(); } finally { _reply.recycle(); _data.recycle(); } return _result; } @Override public void weatherinfo() throws android.os.RemoteException { android.os.Parcel _data = android.os.Parcel.obtain(); android.os.Parcel _reply = android.os.Parcel.obtain(); try { _data.writeInterfaceToken(DESCRIPTOR); mRemote.transact(Stub.TRANSACTION_weatherinfo, _data, _reply, 0); _reply.readException(); } finally { _reply.recycle(); _data.recycle(); } } @Override public byte[] bitmapbyte() throws android.os.RemoteException { android.os.Parcel _data = android.os.Parcel.obtain(); android.os.Parcel _reply = android.os.Parcel.obtain(); byte[] _result; try { _data.writeInterfaceToken(DESCRIPTOR); mRemote.transact(Stub.TRANSACTION_bitmapbyte, _data, _reply, 0); _reply.readException(); _result = _reply.createByteArray(); } finally { _reply.recycle(); _data.recycle(); } return _result; } } static final int TRANSACTION_weather = (android.os.IBinder.FIRST_CALL_TRANSACTION + 0); static final int TRANSACTION_temperature = (android.os.IBinder.FIRST_CALL_TRANSACTION + 1); static final int TRANSACTION_localcity = (android.os.IBinder.FIRST_CALL_TRANSACTION + 2); static final int TRANSACTION_weatherinfo = (android.os.IBinder.FIRST_CALL_TRANSACTION + 3); static final int TRANSACTION_bitmapbyte = (android.os.IBinder.FIRST_CALL_TRANSACTION + 4); } public java.lang.String localcity() throws android.os.RemoteException; public byte[] bitmapbyte() throws android.os.RemoteException; }然后紧接着就可以实现这几个接口函数了,当然这些接口函数不是在上面这个java文件实现的,而是我们需要创建一个Service类,名称自拟,我暂且定为AIDLService.java了。这个类很简答,实现了以下的几个基本功能。
package com.xxx.xxx; import java.io.ByteArrayOutputStream; import com.xxx.xxx.xxx; import android.app.Service; import android.content.Intent; import android.graphics.Bitmap; import android.graphics.drawable.BitmapDrawable; import android.os.IBinder; import android.os.RemoteException; import android.util.Log; public class AIDLService extends Service { private static final String TAG = "AIDLService"; //aidl 接口函数 private String localcity; private byte[] bitmapbyte; private final IBinder mBinder = new IServiceProxy(); @Override public IBinder onBind(Intent intent) { // TODO Auto-generated method stub Log.i(TAG, "onBind() called"); return mBinder; } @Override public final boolean onUnbind(Intent intent){ Log.i(TAG, "onUnbind() called"); return true; } @Override public final void onDestroy(){ super.onDestroy(); Log.i(TAG, "onDestroy() called"); } @Override public void onCreate(){ getWeatherinfo(); super.onCreate(); } private String getLocalcity(){ return localcity; } private byte[] getBitmap(){ return bitmapbyte; } public void initBitmap(String weather){ int resid = 0; mWeatherinfo = WeatherInfoParse.getInstance(); resid = mWeatherinfo.getWeatherIcon(weather); Bitmap m = ((BitmapDrawable)(getResources().getDrawable(resid))).getBitmap(); ByteArrayOutputStream baos = new ByteArrayOutputStream(); m.compress(Bitmap.CompressFormat.JPEG, 100, baos); bitmapbyte = baos.toByteArray(); } private class IServiceProxy extends IService.Stub { @Override public String localcity() throws RemoteException{ return getLocalcity(); } @Override public byte[] bitmapbyte() throws RemoteException{ return getBitmap(); } } }
<service android:name="com.xxx.xxx.AIDLService"> <intent-filter> <action android:name="android.intent.action.AIDLService"> </action> <category android:name="android.intent.category.DEFAULT"> </category> </intent-filter> </service>这个地方比较关键,因为我最开始的时候写的action,android:name为com.xxx.xxx.IService,也就是写了AIDL接口文件的名字,但是结果却是找不到服务,提示如下error
package com.xxx.xxx; import android.content.ComponentName; import android.content.Context; import android.content.Intent; import android.content.ServiceConnection; import android.os.IBinder; import android.os.RemoteException; import android.provider.Settings.SettingNotFoundException; import android.util.Log; public class WeatherManager { ServiceConn connect; Context mContext; private static IService iService; public static WeatherManager weatherManager = null; public WeatherManager(Context context){ if(mContext == null){ Context appContext = context.getApplicationContext(); if (appContext != null) { mContext = appContext; } else { mContext = context; } getInstandce(); if(iService==null) bindIService(); } } public void getInstandce(){ if(weatherManager == null){ weatherManager = this; //bindPlayerService(); } } public void resume(){ bindIService(); } public void suspend(){ unBindIService(); } public void destroy(){ unBindIService(); } public String getLocalcity(){ try{ if(iService != null) return iService.localcity(); } catch (RemoteException e) { e.printStackTrace(); } return null; } public byte[] getBitmapbyte(){ try{ if(iService != null) return iService.bitmapbyte(); } catch (RemoteException e) { e.printStackTrace(); } return null; } private boolean bindIService() { try { Intent startIntent = new Intent("android.intent.action.AIDLService"); connect = new ServiceConn(); return mContext.bindService(startIntent, connect, Context.BIND_AUTO_CREATE); } catch (Exception e) { e.printStackTrace(); Log.e("**************WeatherManager", "bindService is Failed!"); } return false; } private void unBindIService() { if(iService != null){ mContext.unbindService(connect); iService = null; } } private class ServiceConn implements ServiceConnection{ @Override public void onServiceConnected(ComponentName name, IBinder service) { iService = IService.Stub.asInterface(service); } @Override public void onServiceDisconnected(ComponentName name) { iService = null; } } }这部分需要注意的就是要初始化好需要调用的几个接口函数,以及最重要的bindService和unBindService函数,其中能否实现调用的根本就是这个bind是否成功了,如果bind失败那么必然调用也会失败,所以这个地方需要关注。
原文地址:http://blog.csdn.net/zhanghaofor/article/details/44490883