Android组件间交互
四大组件相信大家都不陌生了吧,今天咱们就组件间通信做个说明:
首先:
主要今天的目的是为了说明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组件间交互的更多相关文章
- 解决SpannableString在Android组件间传递时显示失效的问题
问题:在A activity中传递一个SpannableString到B activity中,并最终传递到B activity中的TextView中,但是没有展示出Span效果. 解决:阅读TextV ...
- 【Android开发日记】之入门篇(十二)——Android组件间的数据传输
组件我们有了,那么我们缺少一个组件之间传递信息的渠道.利用Intent做载体,这是一个王道的做法.还有呢,可以利用文件系统来做数据共享.也可以使用Application设置全局数据,利用组件来进行控制 ...
- Android 组件间通信--事件驱动
在android中,组件间通信常用的方式: 1.使用广播机制:在主页面中监听特定的广播事件,进行业务逻辑的操作,其他页面只需要根据需求发送广播即可 例如:常用app结构中,左边通常为菜单栏,点击菜单栏 ...
- android组件间共享数据的常用方法
使用Intent在激活组件的时候携带数据,以进行数据的传递 使用广播进行组件间数据的伟递 使用外部存储(sharedPreference,文件,数据库,网络)进行组件间数据共享 使用Static静态成 ...
- Android组件间的数据传输
组件我们有了,那么我们缺少一个组件之间传递信息的渠道.利用Intent做载体,这是一个王道的做法.还有呢,可以利用文件系统来做数据共享.也可以使用Application设置全局数据,利用组件来进行控制 ...
- Android组件间通信库EventBus学习
项目地址: https://github.com/greenrobot/EventBus EventBus主要特点 1. 事件订阅函数不是基于注解(Annotation)的,而是基于命名约定的,在 ...
- Android消息传递之组件间传递消息
前言: 上篇学习总结了Android通过Handler消息机制实现了工作线程与UI线程之间的通信,今天来学习一下如何实现组件之间的通信.本文依然是为学习EventBus做铺垫,有对比才能进步,今天主要 ...
- Android Service、IntentService,Service和组件间通信
Service组件 Service 和Activity 一样同为Android 的四大组件之一,并且他们都有各自的生命周期,要想掌握Service 的用法,那就要了解Service 的生命周期有哪些方 ...
- Android组件内核之间组件间通信方案(四)下篇
阿里P7Android高级架构进阶视频免费学习请点击:https://space.bilibili.com/474380680本篇文章将继续从以下两个内容来介绍通信方案: [ViewModel 与 V ...
随机推荐
- js小效果-轮播图
<!DOCTYPE html><html><head lang="en"> <meta charset="UTF-8" ...
- Windows环境下的NodeJS+NPM+Bower安装配置步骤
Windows下的NodeJS安装是比较方便的(v0.6.0版本之后,支持windows native),只需要登陆官网(http://nodejs.org/),便可以看到首页的“INSTALL”按钮 ...
- 李洪强iOS经典面试题上
李洪强iOS经典面试题上 1. 风格纠错题 修改完的代码: 修改方法有很多种,现给出一种做示例: // .h文件 // http://weibo.com/luohanchenyilong/ / ...
- [LintCode] Swap Nodes in Pairs 成对交换节点
Given a linked list, swap every two adjacent nodes and return its head. Example Given 1->2-> ...
- nginx连接php fastcgi配置
匹配到php结尾的文件抛到后端 后端php端口9000
- FK JavaScript:ArcGIS JavaScript类库加载不成功而导致的程序异常
现象:页面加载时,有时候成功,有时候出错,出错的地方为init.js 加载esri的类库 经过仔细对比,发现出错时dojo加载的类库中,对象的很多属性都为undefined,而加载成功时,该对象的相关 ...
- 把验证码和生成时间负值给$_SESSION[vCode]生成图像给浏览器
php 图片 中文验证码 <img src="verify_image.php" alt="点此刷新验证码" name="verify_code ...
- 一个xib钟多个Cell
在实际开发中,有时候需要在一个xib钟拖几个cell,注意连线过程的object选择,不然出现没反应的现象. 例如: 在下图中,我在一个xib钟创建了五个cell,他们分别是:收货地址.订单信息.支付 ...
- Larbin初试
前阵子找工作的时候经常会看到epoll多路复用的知识点,无奈自己一点都不懂.慌忙之际也只能去了解个大概.所以最近闲下来之后想要基于epoll机制实现一个比较有用的东西,刚好最近又想爬些东西,希望这次能 ...
- Read4096
Given API: int Read4096(char* buf); It reads data from a file and records the position so that the n ...