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 ...
随机推荐
- 【Eclipse】修改 编码格式
eclipse 默认编码居然是GBK,js文件默认编码是ISO-....怎么可以这样呢? 都修改成UTF8的方法: 1.windows->Preferences...打开"首选项&qu ...
- IOS本地通知
发送通知: UILocalNotification *newNotification = [[UILocalNotification alloc] init]; if (newNotifica ...
- ios修改产品名
在创建项目的时候,会设置一个项目名,以后生成的APP名字也就是这个了,但由于某种原因,我想修改APP名字,也就是屏幕程序图标下面显示的那个,这该怎么办呢? 下面有三种方法都可以: 修改Product ...
- 如何在weka中连接数据库(转)
相关准备: Weka.mysql已安装 MYSQL Driver for JDBC 1.进入weka的安装目录 1)新建文件夹lib和文件夹weka,然后将mysql-connector-java-5 ...
- [CareerCup] 17.3 Factorial Trailing Zeros 求阶乘末尾零的个数
LeetCode上的原题,讲解请参见我之前的博客Factorial Trailing Zeroes. 解法一: int trailing_zeros(int n) { ; while (n) { re ...
- 常用JS表单验证方法
/*输入:str返回:如果全是空返回true,否则返回false*/function isNull(str) {if (str == "") return true;var reg ...
- css小记(3)
1.只有块级元素可以设置背景图,行内元素要变成块级元素才可以:2.盒子不设置宽度,只设置高度,默认为100%,并自适应屏幕大小变化,为这个盒子设置margin可以在保证margin效果的同时默认调整盒 ...
- Android错误:W/ResourceType(2411): No package identifier when getting value for resource number 0x
报错信息: 07-04 11:14:43.064: W/ResourceType(2411): No package identifier when getting value for resourc ...
- POM.xml的配置实例
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/20 ...
- svn服务配置和日常维护命令
Subversion独立服务和与apache整合服务. 一 .Svn独立服务安装 操作系统: Redhat Linux AS3 AS 4 ContOS AS 4 安装包获取: 下载[url]ht ...