最简单的Android Aidl 使用
首先,为什么要用aidl
---------------------------------------------------------------------------------------------------------------------------
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里面没有东西就不贴了
ClientActivity的代码:
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); } }
这里就实现了最简单的aidl通信的过程
然后再启动client端,点击bindservice之后
<span style="white-space:pre"> </span>parcelable Object
另外,接口中的参数除了aidl支持的类型,其他类型必须标识其方向:到底是输入还是输出抑或两者兼之,用in,out或者inout来表示,一般 in标记,因为大多数情况下输入型参数。
过程中碰到的错误
java.lang.SecurityException: Binder invocation to an incorrect interface
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。