Android中的跨进程回调
来源:互联网 发布:netty 好书 知乎 编辑:程序博客网 时间:2024/06/07 00:26
在Android应用程序开发中,可能会遇到跨进程回调问题,比如,调用一个服务,但服务是异步的,服务完成后,需要给客户一个通知,这时就需要用到跨进程回调了。跨进程回调本质上用到了Binder机制,其过程如下:
3.Client定义如下:
在以上代码中ITestListener为跨进程回调接口
1.定义aidl
ITest.aidl
package com.example.chirpdemo;import com.example.chirpdemo.ITestListener;interface ITest {int getValue();void setValue(int value);void listen(ITestListener listener);}
ITestListener.aidl
package com.example.chirpdemo;interface ITestListener {void onFinished(int result);}
2.Service定义如下:
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; } @Override public int getValue() throws RemoteException { return mValue; } @Override public void setValue(int value) throws RemoteException { mValue = value; if (null != mListener) { mListener.onFinished(-1); } } @Override public void listen(ITestListener listener) throws RemoteException { mListener = listener; } } @Override public void onCreate() { Log.d(TAG, "onCreate"); super.onCreate(); } @Override public void onDestroy() { Log.d(TAG, "onDestroy"); super.onDestroy(); } @Override public void onStart(Intent intent, int startId) { Log.d(TAG, "onStart"); super.onStart(intent, startId); } @Override public int onStartCommand(Intent intent, int flags, int startId) { Log.d(TAG, "onStartCommand"); return super.onStartCommand(intent, flags, startId); } @Override public boolean onUnbind(Intent intent) { Log.d(TAG, "onUnbind"); return super.onUnbind(intent); } @Override public void onRebind(Intent intent) { Log.d(TAG, "onRebind"); super.onRebind(intent); } @Override public IBinder onBind(Intent arg0) { return new ServiceImpl(); }}
3.Client定义如下:
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() { @Override public void onServiceConnected(ComponentName name, IBinder service) { leservice = ITest.Stub.asInterface(service); try { leservice.listen(new ITestListener.Stub() { @Override public void onFinished(int result) throws RemoteException { } }); } catch (RemoteException e) { // TODO Auto-generated catch block e.printStackTrace(); } } @Override public void onServiceDisconnected(ComponentName name) { leservice = null; Log.i("aaaaaaaaaaaa", "onDisconnected:" + Thread.currentThread()); } }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); startedService = bindService(new Intent("com.my.IService"), myServiceConnection, BIND_AUTO_CREATE); Log.i("aaaaaaaaaaaa", "bindService:" + Thread.currentThread()); Button bt = (Button)this.findViewById(R.id.bt1); bt.setOnClickListener(new OnClickListener() { @Override public void onClick(View arg0) { if ( startedService ) { if ( null != leservice ) { try { leservice.setValue(leservice.getValue() + 1); } catch (RemoteException e) { e.printStackTrace(); } } } } }); view.setOnClickListener(new OnClickListener() { @Override public void onClick(View arg0) { if ( startedService ) { if ( null != leservice ) { try { mEditText.setText(leservice.getValue() + ""); } catch (RemoteException e) { e.printStackTrace(); } } } } }); } @Override public void onStart() { super.onStart(); Log.i("aaaaaaaa", "onStart"); } @Override public void onPause() { super.onPause(); Log.i("aaaaaaaa", "onPause"); } @Override public void onResume() { super.onResume(); Log.i("aaaaaaaa", "onResume"); } @Override public void onStop() { super.onStop(); Log.i("aaaaaaaa", "onStop"); } @Override protected void onDestroy() { if (startedService) { unbindService(myServiceConnection); } Log.i(TAG, "onDestroy"); super.onDestroy(); }}
在以上代码中ITestListener为跨进程回调接口
- Android中的跨进程回调
- android中的跨进程服务组件aidl
- Android中的Binder跨进程通信机制
- Android中的跨进程调用技术AIDL
- Android中的IPC跨进程通讯
- Android Studio中的跨进程访问(aidl)
- Android 中的Binder跨进程通信机制与AIDL
- Android中的跨进程通信AIDL、Binder源码详解
- android 跨进程通信
- Android 跨进程通信
- Android跨进程通信
- Android组件跨进程
- Android跨进程通信
- Android LocalBroadcast跨进程
- Android跨进程通讯
- Android跨进程通信
- Android跨进程通信
- Android跨进程通信
- 获取WebView加载HTML时网页中的内容
- Transaction replication只对新增的Article产生Snapshot
- 2012国内各大互联网公司研发岗薪资情况
- Hoj 1307 Choose Your Words Carefully、Hoj 1989 The Most Frequent Word
- web入门--jsp/servlet/JSTL
- Android中的跨进程回调
- 关于Linux系统中/tmp目录的清除问题
- 正则表达式30分钟入门教程
- 2746:约瑟夫问题 百炼(非链表)
- 通用存储过程
- command alias
- Python Eric4安装配置
- ora-02049处理
- 挣脱浏览器的束缚(1) - 前言