四大组件相信大家都不陌生了吧,今天咱们就组件间通信做个说明:

首先:

  主要今天的目的是为了说明Android 提供的一个ResultReceiver类,这个类相信大家都不陌生吧》?但是你们层深入了解过么,这个类不可谓不强大,辣么,咱们就看看怎么使他吧,

  实例:Activity和service通信,484很屌,我也这么觉得,然并卵。

  这里直接就来源码看看吧!!!!

  牛逼的不行不行,

 /*
* Copyright (C) 2009 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/ package android.os; import com.android.internal.os.IResultReceiver; /**
* Generic interface for receiving a callback result from someone. Use this
* by creating a subclass and implement {@link #onReceiveResult}, which you can
* then pass to others and send through IPC, and receive results they
* supply with {@link #send}.
*/
public class ResultReceiver implements Parcelable {
final boolean mLocal;
final Handler mHandler; IResultReceiver mReceiver; class MyRunnable implements Runnable {
final int mResultCode;
final Bundle mResultData; MyRunnable(int resultCode, Bundle resultData) {
mResultCode = resultCode;
mResultData = resultData;
} public void run() {
onReceiveResult(mResultCode, mResultData);
}
} class MyResultReceiver extends IResultReceiver.Stub {
public void send(int resultCode, Bundle resultData) {
if (mHandler != null) {
mHandler.post(new MyRunnable(resultCode, resultData));
} else {
onReceiveResult(resultCode, resultData);
}
}
} /**
* Create a new ResultReceive to receive results. Your
* {@link #onReceiveResult} method will be called from the thread running
* <var>handler</var> if given, or from an arbitrary thread if null.
*/
public ResultReceiver(Handler handler) {
mLocal = true;
mHandler = handler;
} /**
* Deliver a result to this receiver. Will call {@link #onReceiveResult},
* always asynchronously if the receiver has supplied a Handler in which
* to dispatch the result.
* @param resultCode Arbitrary result code to deliver, as defined by you.
* @param resultData Any additional data provided by you.
*/
public void send(int resultCode, Bundle resultData) {
if (mLocal) {
if (mHandler != null) {
mHandler.post(new MyRunnable(resultCode, resultData));
} else {
onReceiveResult(resultCode, resultData);
}
return;
} if (mReceiver != null) {
try {
mReceiver.send(resultCode, resultData);
} catch (RemoteException e) {
}
}
} /**
* Override to receive results delivered to this object.
*
* @param resultCode Arbitrary result code delivered by the sender, as
* defined by the sender.
* @param resultData Any additional data provided by the sender.
*/
protected void onReceiveResult(int resultCode, Bundle resultData) {
} public int describeContents() {
return 0;
} public void writeToParcel(Parcel out, int flags) {
synchronized (this) {
if (mReceiver == null) {
mReceiver = new MyResultReceiver();
}
out.writeStrongBinder(mReceiver.asBinder());
}
} ResultReceiver(Parcel in) {
mLocal = false;
mHandler = null;
mReceiver = IResultReceiver.Stub.asInterface(in.readStrongBinder());
} public static final Parcelable.Creator<ResultReceiver> CREATOR
= new Parcelable.Creator<ResultReceiver>() {
public ResultReceiver createFromParcel(Parcel in) {
return new ResultReceiver(in);
}
public ResultReceiver[] newArray(int size) {
return new ResultReceiver[size];
}
};
}

  

  Line27:说明该类是Parcelable的SubClass,这就为我们今天Service和Act通信提供的可能

  Bundle是Android通信中参数的载体,同时提供了各种各样的set方法,其中主要的一个set方法,可以接受一个Parcelable子类对象。

  

  不到大家注意了木;

  Line99:protected void onReceiveResult(int resultCode, Bundle resultData)这个方法是个空实现,纳尼问题来了,咱们阔以重载了他么,

  下边是我自己的代码,来看俺的风骚;

 import android.os.Bundle;
import android.os.Handler;
import android.os.ResultReceiver; /**
* @author ArMn
* 859686819@qq.com
*/
public class MyResultReceiver extends ResultReceiver
{ public MyResultReceiver(Handler handler) {
super(handler);
// TODO Auto-generated constructor stub
} @Override
protected void onReceiveResult(int resultCode, Bundle resultData) {
// TODO Auto-generated method stub
if(null != mActCallBackListener)
mActCallBackListener.callBack();
} public void setReceiver(ActCallBackListener listener){
this.mActCallBackListener = listener;
} private ActCallBackListener mActCallBackListener = null; public interface ActCallBackListener{
void callBack();
}
}

  代码实现了ResultRecceview类,然后重载了onReceiveResult方法,同事自定义了内部interface,setReceiver方法用于指定所需要的回调对象,callback就是真正的回调方法了。

  现在是时候说怎么通信了 ,通信的简单代码我就不再复述了,上边说道Bundle可以接受一个Parcelable对象,当然了现在咱们的MyReceiver继承自Receiver,同事Receiver是Parcelable的子类,别的就不用说了,new MyREceiver给Bundle是没问题了,回头继续源码Line74方法send方法;

  核心源码:

 public void send(int resultCode, Bundle resultData) {
if (mLocal) {
if (mHandler != null) {
mHandler.post(new MyRunnable(resultCode, resultData));
} else {
onReceiveResult(resultCode, resultData);
}
return;
}
.........
}

  Line6:大家可以看到这里callback到咱们重载的方法onReceiveResul方法,咱们的callback484真的就完成了回调了哪?????

  饿死人了,粢饭!!!!!!

  有空继续吹,今天先到这<><><><><><><><><>66666666666666666

Android组件间交互的更多相关文章

  1. 解决SpannableString在Android组件间传递时显示失效的问题

    问题:在A activity中传递一个SpannableString到B activity中,并最终传递到B activity中的TextView中,但是没有展示出Span效果. 解决:阅读TextV ...

  2. 【Android开发日记】之入门篇(十二)——Android组件间的数据传输

    组件我们有了,那么我们缺少一个组件之间传递信息的渠道.利用Intent做载体,这是一个王道的做法.还有呢,可以利用文件系统来做数据共享.也可以使用Application设置全局数据,利用组件来进行控制 ...

  3. Android 组件间通信--事件驱动

    在android中,组件间通信常用的方式: 1.使用广播机制:在主页面中监听特定的广播事件,进行业务逻辑的操作,其他页面只需要根据需求发送广播即可 例如:常用app结构中,左边通常为菜单栏,点击菜单栏 ...

  4. android组件间共享数据的常用方法

    使用Intent在激活组件的时候携带数据,以进行数据的传递 使用广播进行组件间数据的伟递 使用外部存储(sharedPreference,文件,数据库,网络)进行组件间数据共享 使用Static静态成 ...

  5. Android组件间的数据传输

    组件我们有了,那么我们缺少一个组件之间传递信息的渠道.利用Intent做载体,这是一个王道的做法.还有呢,可以利用文件系统来做数据共享.也可以使用Application设置全局数据,利用组件来进行控制 ...

  6. Android组件间通信库EventBus学习

    项目地址:   https://github.com/greenrobot/EventBus EventBus主要特点 1. 事件订阅函数不是基于注解(Annotation)的,而是基于命名约定的,在 ...

  7. Android消息传递之组件间传递消息

    前言: 上篇学习总结了Android通过Handler消息机制实现了工作线程与UI线程之间的通信,今天来学习一下如何实现组件之间的通信.本文依然是为学习EventBus做铺垫,有对比才能进步,今天主要 ...

  8. Android Service、IntentService,Service和组件间通信

    Service组件 Service 和Activity 一样同为Android 的四大组件之一,并且他们都有各自的生命周期,要想掌握Service 的用法,那就要了解Service 的生命周期有哪些方 ...

  9. Android组件内核之间组件间通信方案(四)下篇

    阿里P7Android高级架构进阶视频免费学习请点击:https://space.bilibili.com/474380680本篇文章将继续从以下两个内容来介绍通信方案: [ViewModel 与 V ...

随机推荐

  1. vs2013单元测试第二部分

    上次的随笔说还没弄懂,现在已经弄懂,就让我说说我的方法吧. 1.点击文件——>新建——>项目——>c#——>控制台应用程序,确定,之后如图所示 2.在一定位置写上要进行单元检测 ...

  2. SDL实战,小游戏

    http://www.cppblog.com/sandy/archive/2005/12/28/2219.html sdl1教学 http://kelvmiao.info/sdl-tutorial-c ...

  3. minicom使用

    http://blog.chinaunix.net/uid-9525959-id-2001815.html 创建log文件 : minicom -C my_capturefile

  4. zk label控件内容换行

    Label控件本身无法换行,不过div却可以,只要设置了div的宽度,那么就想如果在Label控件外套个div会怎样,结果可喜可乐: <div width="80px"> ...

  5. c# 递归

    递归 函数体内调用自身函数,直到符合某一条件时不再继续调用两个需要满足的条件1.有反复调用自身函数的过程2.有函数的出口,有不再继续执行的条件 练习: 1.用递归函数做n的阶乘. 2.一群羊赶到村庄去 ...

  6. 调试WEB APP多设备浏览器

    方法:adobe shadow  \ opera远程调试\ weinre adobe shadow: 我们经常使用Firefox的firebug或者Chrome的开发人员工具进行Web调试页面,Jav ...

  7. Windows7无法访问(远程登录)Windows 2003共享问题解决

    解决方法: 1.直接按下win+r键,输入gpedit.msc,打开本地组策略编辑器. 2.找到“计算机配置”-->“Windows设置”-->“安全设置”-->“本地策略”--&g ...

  8. easyui datagrid 单选框 效果

    columns: [[{            field: 'oid',            title: '选择',            width: 20,            forma ...

  9. MySQL 启动时禁用了 InnoDB 引擎的解决方法

    今天在从本地数据库复制表数据到虚拟机 CentOS 6.6 上的数据库时,得到提示: Unknown table engine 'InnoDB' 于是在服务器 MySQL 中查看了引擎: mysql& ...

  10. TCP/IP协议分层

    TCP/IP协议从上而下,层层包装: (1)应用层:HTTP (2)传输层:TCP和UDP (3)网络层(网际互联层):IP (4)数据连接层(网络接入层):为IP模块发送和接收IP数据报. (5)硬 ...