Android中的跨进程回调
[java]在Android应用程序开发中,可能会遇到跨进程回调问题,比如,调用一个服务,但服务是异步的,服务完成后,需要给客户一个通知,这时就需要用到跨进程回调了。跨进程回调本质上用到了Binder机制,其过程如下:1.定义aidlITest.aidl[plain]package com.example.chirpdemo;import com.example.chirpdemo.ITestListener;inte易做图ce ITest {int getValue();void setValue(int value);void listen(ITestListener listener);}ITestListener.aidl[plain] www.zzzyk.compackage com.example.chirpdemo;inte易做图ce ITestListener {void onFinished(int result);}2.Service定义如下:[java]package com.example.chirpdemo;import android.app.Service;import android.content.Intent;import android.os.IBinder;import android.os.RemoteException;import android.util.Log;public class MyService extends Service {final private static String TAG = "MyService";public class ServiceImpl extends ITest.Stub {private int mValue;private ITestListener mListener;public ServiceImpl() {mValue = 0;}@Overridepublic int getValue() throws RemoteException {return mValue;}@Overridepublic void setValue(int value) throws RemoteException {mValue = value;if (null != mListener) {mListener.onFinished(-1);}}@Overridepublic void listen(ITestListener listener) throws RemoteException {mListener = listener;}}@Overridepublic void onCreate() {Log.d(TAG, "onCreate");super.onCreate();}@Overridepublic void onDestroy() {Log.d(TAG, "onDestroy");super.onDestroy();}@Overridepublic void onStart(Intent intent, int startId) {Log.d(TAG, "onStart");super.onStart(intent, startId);}@Overridepublic int onStartCommand(Intent intent, int flags, int startId) {Log.d(TAG, "onStartCommand");return super.onStartCommand(intent, flags, startId);}@Overridepublic boolean onUnbind(Intent intent) {Log.d(TAG, "onUnbind");return super.onUnbind(intent);}@Overridepublic void onRebind(Intent intent) {Log.d(TAG, "onRebind");super.onRebind(intent);}@Overridepublic IBinder onBind(Intent arg0) {return new ServiceImpl();}}3.Client定义如下:[java]package com.example.easytabdemo;import android.app.Activity;import android.content.ComponentName;import android.content.Context;import android.content.Intent;import android.content.ServiceConnection;import android.graphics.PixelFormat;import android.graphics.drawable.Drawable;import android.os.Bundle;import android.os.IBinder;import android.os.RemoteException;import android.util.Log;import android.view.View;import android.view.View.OnClickListener;import android.view.ViewGroup;import android.widget.Button;import android.widget.EditText;import android.widget.TextView;import com.example.chirpdemo.ITest;import com.example.chirpdemo.ITestListener;import com.example.easytabdemo.SlideTabHost.TabSpec;public class MainActivity extends Activity {final private static String TAG = "MainActivity";final Intent myIntent = new Intent("com.pyk.les.IlongExistService");private boolean startedService = false;private ITest leservice = null;ServiceConnection myServiceConnection = new ServiceConnection() {@Overridepublic void onServiceConnected(ComponentName name, IBinder service) {leservice = ITest.Stub.asInte易做图ce(service);try {leservice.listen(new ITestListener.Stub() {@Overridepublic void onFinished(int result) throws RemoteException {&补充:移动开发 , Android ,
上一个:Android学习之广播事件处理
下一个:WebView加载网络PDF