Android的跨进程通信
来源:互联网 发布:淘宝lol代练可靠吗 编辑:程序博客网 时间:2024/05/01 10:16
Android的跨进程通信
为了安全考虑,应用之间的内存是无法互相访问的,各自的数据都存在于自身的内存区域内。
还有一种方式是Binder1是系统服务,Binder2是应用端创建的Binder对象,他们两者通过Binder驱动进行连接后,应用端可以主动调系统服务,系统服务也可以主动调用应用端。WindowManagerService就是采用的这种方式。
先复习一下服务的应用绑定
当Activity需要使用Service中的方法时可以调用bindService()绑定服务,Service会返回一个IBinder对象,
这个对象可以调用Service中的方法.
(当启动服务的Activity退出时可以进行解绑操作)
复习:1)Activity中onResume()---------------所以不能吧这些代码写到onCreate方法中,应该有Activity提供的onResume方法,这个可以实时跟新数据的变化。
2)Activity中onActivityResult(......)---------
startActivity(Intent)和startActivityForResult(Intent,int)的区别使用:startActivity(Intent) 方法可以用来启动一个新的 activity ,这个 activity 将被放置在 activity 栈的栈顶。这个方法只有一个参数 Intent ,这个参数描述了将被执行的 activity 。
有时候你希望在一个 activity 结束时得到它返回的结果。举个例子,你可能启动一个 activity 来让用户从通讯簿中选择一个人;当它结束的时候将会返回这个所选择的人。为了得到这个返回的信息,你可以使用 startSubActivity(Intent, int) 这个方法来启动新的 activity ,第二个整形参数将会作为这次调用的识别标记。这个 activity 返回的结果你可以通过 onActivityResult(int, int, String, Bundle) 方法来获得,此方法的第一个参数就是之前调用所使用的识别标记。
3)IBinder是什么呢?首先要明白,Android的远程调用(就是跨进程调用)就是通过IBinder实现的.
简单的服务绑定
public class MainActivity extends Activity implements ServiceInterface {private Intent intent;private ServiceInterface si;private ServiceConnection conn = new ServiceConnection() {@Overridepublic void onServiceDisconnected(ComponentName name) {//意外中断事件System.out.println("意外中断事件");}@Overridepublic void onServiceConnected(ComponentName name, IBinder service) {// 监听器si = (ServiceInterface) service;System.out.println("绑定成功");}};@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);intent = new Intent(this, MySercvice.class);}@Overridepublic boolean onCreateOptionsMenu(Menu menu) {getMenuInflater().inflate(R.menu.main, menu);return true;}@Overrideprotected void onResume() {super.onResume();// 绑定服务,得到IBinder(ServiceInterface的实现类)bindService(intent, conn, BIND_AUTO_CREATE);}@Overrideprotected void onPause() {super.onPause();// 解绑服务unbindService(conn);System.out.println("解绑服务");}@Overridepublic void pay(View view) {// 调用服务中pay方法si.pay(view);}@Overridepublic void collection(View view) {// 调用服务中的collection()方法si.collection(view);}@Overrideprotected void onActivityResult(int requestCode, int resultCode, Intent data) {super.onActivityResult(requestCode, resultCode, data);System.out.println("---------------onActivityResult------------------");}
public interface ServiceInterface {/** * 付款 * @param view */public void pay(View view);/** * 收款 * @param view */public void collection(View view);}
public class MySercvice extends Service {@Overridepublic IBinder onBind(Intent intent) {return new MyBinder();//返回一个可以调用pay()和collection()的对象}class MyBinder extends Binder implements ServiceInterface{@Overridepublic void pay(View view) {System.out.println("支付宝付款");}@Overridepublic void collection(View view) {System.out.println("支付宝收款");}}}
复习使用aidl
创建一个单独的服务程序(单独的进程)
package com.example.remoteserver.invokeinterface;interface InvokeInterface {void pay();void collection();}
package com.example.remoteserver;import android.app.Service;import android.content.Intent;import android.os.IBinder;import com.example.remoteserver.invokeinterface.InvokeInterface.Stub;public class RemoteServer extends Service {@Overridepublic IBinder onBind(Intent arg0) {System.out.println("onBind");return new MyBinder();}class MyBinder extends Stub {@Overridepublic void pay() {System.out.println("远程支付");}@Overridepublic void collection() {System.out.println("远程收款");}}@Overridepublic void onCreate() {super.onCreate();System.out.println("onCreate onCreate");}@Overridepublic void onDestroy() {super.onDestroy();System.out.println("onDestroy onDestroy");}}
RemoteActivity调用其他进程的方法package com.example.remoteserver.invokeinterface;interface InvokeInterface { void pay(); void collection(); }
package com.example.remoteactivity;import android.app.Activity;import android.content.ComponentName;import android.content.Context;import android.content.Intent;import android.content.ServiceConnection;import android.os.Bundle;import android.os.IBinder;import android.os.RemoteException;import android.view.View;import com.example.remoteserver.invokeinterface.InvokeInterface;import com.example.remoteserver.invokeinterface.InvokeInterface.Stub;public class MainActivity extends Activity {private Intent intent;private InvokeInterface ik;private ServiceConnection conn=new ServiceConnection(){@Overridepublic void onServiceConnected(ComponentName name, IBinder service) {// 绑定成功//ik=(InvokeInterface) service;ik=Stub.asInterface(service);}@Overridepublic void onServiceDisconnected(ComponentName name) {}};@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);intent=new Intent("itheima.service.REMOTE_SERVICE");}@Overrideprotected void onResume() {super.onResume();bindService(intent, conn,Context.BIND_AUTO_CREATE);}@Overrideprotected void onPause() {super.onPause();unbindService(conn);//解绑服务}public void pay(View view){//ik.pay();try {ik.pay();} catch (RemoteException e) {e.printStackTrace();}}public void collection(View view){try {ik.collection();} catch (RemoteException e) {e.printStackTrace();}}}
- Android的跨进程通信
- Android的跨进程通信
- android 跨进程通信
- Android 跨进程通信
- Android跨进程通信
- Android跨进程通信
- Android跨进程通信
- Android跨进程通信
- Android跨进程通信
- Android跨进程通信
- 关于Android 跨进程通信的文章?
- Android Messenger简单的跨进程通信
- Android跨进程通信-AIDL的使用
- android 跨进程通信的代表Binder
- Android中跨进程通信的方式
- android Messenger 跨进程通信
- android Messenger 跨进程通信
- Android跨进程通信-AIDL
- codeforces 228(div1) C
- PHP多功能图片处理类
- 原来新浪的blog的地址
- PHP文件缓存的性能测试
- 黑马程序员_Java基础_网络编程
- Android的跨进程通信
- 数据结构基础(一)
- 数据结构基础(二)
- 再牛逼的算法也算不出女神的忧伤……
- 树莓派 Archlinux 的国内更新源以及 8188EU 的网卡驱动
- iOS Dev (45) iOS图标与切片处理工具Prepo
- eclipse不能启动的解决方案
- CentOS硬盘安装方法
- 使用 FSharpx.TypeProviders.Excel 类型提供程序的方法