四大组件相信大家都不陌生了吧,今天咱们就组件间通信做个说明:
首先:
主要今天的目的是为了说明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