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. svn is already under version control问题解决

    svn ci 时出现 xx is already under version control,然后无法提交,出现这个问题的原因是你所提交的文件或目录是其他SVN的东西,即下面有.svn的目录,需要先把 ...

  2. hdu4576 概率dp n^2的矩阵

    这个题目看网上好多题解都是直接O(n*m)卡过.我是这么做的. 对于m次操作,统计每个w的次数.然后对每个w做矩阵乘法. 这样直接做矩阵乘法是会TLE的. 又由于这里的矩阵很特殊,一次乘法可以降维成O ...

  3. 使用截图方式将Excel导出为PNG图片的不可行性

    博主前面一篇文章使用了JAVA的Robot机制 模拟打开Excel然后Robot移动到指定区域,截图并生成PNG格式图片 试图使用这种方式将复杂的Excel报表转化成无差别的PNG图片 但是这种方式遇 ...

  4. linux shell ls -1 列显示文件

    /******************************************************************************* * linux shell ls -1 ...

  5. MySQL基础之第10章 查询数据

    10.1.基本查询语句 SELECT 属性列表 FROM 表名和视图列表[WHERE条件表达式1][GROUPBY 属性名1 [HAVING条件表达式2]][ORDERBY 属性名2[ASC|DESC ...

  6. CocoaPods一个Objective-C第三方库的管理利器

    转:http://blog.csdn.net/totogo2010/article/details/8198694 介绍: 开发应用的时候第三方的库是不可缺少的,能提高开发的效率. 一些经常用到的库, ...

  7. Android 的实现TextView中文字链接的4种方法

    Android 的实现TextView中文字链接的方式有很多种. 总结起来大概有4种: 1.当文字中出现URL.E-mail.电话号码等的时候,可以将TextView的android:autoLink ...

  8. WEB安全性测试测试用例(基础)

    建立整体的威胁模型,测试溢出漏洞.信息泄漏.错误处理.SQL 注入.身份验证和授权错误. 输入验证 客户端验证服务器端验证(禁用脚本调试,禁用Cookies) 1.输入很大的数(如4,294,967, ...

  9. 扩展Fitnesse的ScriptTable:支持if-then

    Fitnesse的ScriptTable只能顺序执行所有行,本博文介绍如何让ScriptTable支持if-then,来条件执行一行. 首先普及一下概念,什么是Fitnesse,听一听.NET版Cuc ...

  10. 使用std::function 把类成员函数指针转换为普通函数指针

    前言 这是改造前一篇 设计模式 的基础,使通知者不必知道观察者的类名和函数名,只需要知道更新函数的原型即可. 开发环境:WIN7 32位 + VS2010 发现在VS2005中使用std::funti ...