MainActivity如下:

package cn.testcallback;
import android.os.Bundle;
import android.app.Activity;
/**
* Demo描述:
* Android中回调接口的使用
*/
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
testCallBack();
}
private void testCallBack(){
AskQuestion askQuestion=new AskQuestion();
String questionString="Do you love me ?";
System.out.println("The question is : "+questionString);
askQuestion.askQuestion(questionString);
} }

CallBackInterface如下:

package cn.testcallback;
/**
* 定义一个接口
* 接口中有一方法
*/
public interface CallBackInterface {
public void answerQuestion(String result);
}

AskQuestion如下:

package cn.testcallback;

public class AskQuestion implements CallBackInterface{
public void askQuestion(final String questionString){
new Thread(){
public void run() {
AnswerQuestion answerQuestion=new AnswerQuestion();
answerQuestion.receiveQuestion(AskQuestion.this, questionString);
};
}.start();
}
@Override
public void answerQuestion(String result) {
System.out.println("The answter is : "+result);
} }

AnswerQuestion如下:

package cn.testcallback;

public class AnswerQuestion {
public void receiveQuestion(CallBackInterface callBackInterface,String questionString){
try {
Thread.sleep(3000);
} catch (Exception e) {
}
String result="YES!!!";
callBackInterface.answerQuestion(result);
}
}

main.xml如下:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
> <TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="回调的使用"
android:layout_centerInParent="true"
/> </RelativeLayout>

Android中回调接口的使用的更多相关文章

  1. Android中的接口回调技术

    Android中的接口回调技术有很多应用的场景,最常见的:Activity(人机交互的端口)的UI界面中定义了Button,点击该Button时,执行某个逻辑. 下面参见上述执行的模型,讲述James ...

  2. Android 中的接口回调

    http://blog.csdn.net/wangjinyu501/article/details/22052187   在Android中到处可见接口回调机制,尤其是UI事件处理方面.举一个最常见的 ...

  3. android 中调用接口发送短信

    android中可以通过两种方式发送短信 第一:调用系统短信接口直接发送短信:主要代码如下: //直接调用短信接口发短信 SmsManager smsManager = SmsManager.getD ...

  4. Android中Parcelable接口

    1. Parcelable接口 Interface for classes whose instances can be written to and restored from a Parcel. ...

  5. (转)Android中Parcelable接口用法

    1. Parcelable接口 Interface for classes whose instances can be written to and restored from a Parcel. ...

  6. Android中Parcelable接口用法

    from: http://www.cnblogs.com/renqingping/archive/2012/10/25/Parcelable.html Interface for classes wh ...

  7. Android中Parcelable接口的使用

    在做开发的过程中,序列化是非常常见的.比如要将对象保存本地磁盘或者在网络上传输等.实现序列化有两种方式,一种是实现Serializable接口,第二种是实现Parcelable. Serializab ...

  8. Android中定义接口的方法

    1.接口方法用于回调(这里定义接口是为了使用其接口方法): public interface ICallback { public void func(); } public class Caller ...

  9. android中回调函数机制完全解析

    1.在要调用的业务操作中,创建一个接口,在接口中创建方法,这个方法表示的是我们原先要在业务类中执行的操作 public interface BackUpSmsListener { /** * 设置总进 ...

随机推荐

  1. 函数buf_pool_init_instance

    buff_pool_t 结构体 详见 /********************************************************************//** Initial ...

  2. bzoj1562

    很明显是二分图匹配,关键是怎么求字典序最小 想到两种做法,首先是直接匹配,然后从第一位贪心调整 第二种是从最后一个倒着匹配,每次匹配都尽量选小的,这样一定能保证字典序最小 type node=reco ...

  3. 给你一个承诺 - 玩转 AngularJS 的 Promise(转)

    在谈论Promise之前我们要了解一下一些额外的知识:我们知道JavaScript语言的执行环境是“单线程”,所谓单线程,就是一次只能够执行一个任务,如果有多个任务的话就要排队,前面一个任务完成后才可 ...

  4. 多个SSH key对应多个Host: Github, Bitbucket

    https://confluence.atlassian.com/bitbucket/configure-multiple-ssh-identities-for-gitbash-mac-osx-lin ...

  5. table中嵌套table,如何用jquery来控制奇偶行颜色

    总是要趁着自己还有记忆的时候,把该记录下来的都记录下来,着实是不敢恭维自己的记性. 相信很多时候,我们前端人员,经常会用到table里面的某个td中还嵌套着table,而这个时候还总要去弄奇偶行的颜色 ...

  6. Top Android App使用的组件 3

    8684公交 AdChina:com.adchina:易传媒广告平台 AdsMogo:com.adsmogo:芒果移动广告平台 大姨吗 AChartEngine:org.achartengine:An ...

  7. store / cache 系列

    ### golang go-cache An in-memory key:value store/cache (similar to Memcached) library for Go, suitab ...

  8. JUC之Atomic系列12大类实例讲解和原理分解

    在java6以后我们不但接触到了Lock相关的锁,也接触到了很多更加乐观的原子修改操作,也就是在修改时我们只需要保证它的那个瞬间是安全的即可,经过相应的包装后可以再处理对象的并发修改,以及并发中的AB ...

  9. 【LeetCode 201】Bitwise AND of Numbers Range

    Given a range [m, n] where 0 <= m <= n <= 2147483647, return the bitwise AND of all numbers ...

  10. 《Python基础教程(第二版)》学习笔记 -> 第八章 异常

    什么是异常 Python用 异常对象(exception object)来表示异常情况.遇到错误后,会引发异常,如果异常对象并未被处理或者捕捉,程序就会用所谓的回溯(Traceback,一种错误信息) ...