Android中回调接口的使用
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中回调接口的使用的更多相关文章
- Android中的接口回调技术
Android中的接口回调技术有很多应用的场景,最常见的:Activity(人机交互的端口)的UI界面中定义了Button,点击该Button时,执行某个逻辑. 下面参见上述执行的模型,讲述James ...
- Android 中的接口回调
http://blog.csdn.net/wangjinyu501/article/details/22052187 在Android中到处可见接口回调机制,尤其是UI事件处理方面.举一个最常见的 ...
- android 中调用接口发送短信
android中可以通过两种方式发送短信 第一:调用系统短信接口直接发送短信:主要代码如下: //直接调用短信接口发短信 SmsManager smsManager = SmsManager.getD ...
- Android中Parcelable接口
1. Parcelable接口 Interface for classes whose instances can be written to and restored from a Parcel. ...
- (转)Android中Parcelable接口用法
1. Parcelable接口 Interface for classes whose instances can be written to and restored from a Parcel. ...
- Android中Parcelable接口用法
from: http://www.cnblogs.com/renqingping/archive/2012/10/25/Parcelable.html Interface for classes wh ...
- Android中Parcelable接口的使用
在做开发的过程中,序列化是非常常见的.比如要将对象保存本地磁盘或者在网络上传输等.实现序列化有两种方式,一种是实现Serializable接口,第二种是实现Parcelable. Serializab ...
- Android中定义接口的方法
1.接口方法用于回调(这里定义接口是为了使用其接口方法): public interface ICallback { public void func(); } public class Caller ...
- android中回调函数机制完全解析
1.在要调用的业务操作中,创建一个接口,在接口中创建方法,这个方法表示的是我们原先要在业务类中执行的操作 public interface BackUpSmsListener { /** * 设置总进 ...
随机推荐
- codeforces 235 div2 C Team
题目:http://codeforces.com/contest/401/problem/C 题意:n个0,m个1,求没有00或111的情况. 这么简单的题..... 做题的时候脑残了...,今天,贴 ...
- [Android] ImageView.ScaleType设置图解
ImageView的Scaletype决定了图片在View上显示时的样子,如进行何种比例的缩放,及显示图片的整体还是部分,等等. 设置的方式包括: 1. 在layout xml中定义android ...
- 原创-兼容IE8的placeholder
!function (o) { o.fn.extend({ PlaceHolder: function () { var _isEmpty = function (val) { return (val ...
- Linux shell下批量创建缩略图
一.背景 今天,突然发现手机客户端上的最新新闻缩略图都不显示了,上服务器上看了看, 发现新的新闻图片根本没有生成缩略图. 这套新闻发布系统是很老的程序了,查了一下,问题的原因是不支持png格式的图片, ...
- java读取照片信息 获取照片拍摄时的经纬度
项目结构 源码:ImageInfo.zip 第一步:添加需要的架包metadate-extractor.jar 架包下载地址:https://code.google.com/p/metadata-ex ...
- Java [Leetcode 160]Intersection of Two Linked Lists
题目描述: Write a program to find the node at which the intersection of two singly linked lists begins. ...
- Nosql释义
NoSQL不是产品,是一项运动 ---->NoSQL(NoSQL = Not Only SQL ),意即反SQL运动,是一项全新的数据库革命性运动,早期就有人提出,发展至2009年 ...
- (3)Spring定时任务的几种实现
Spring定时任务的几种实现 近日项目开发中需要执行一些定时任务,比如需要在每天凌晨时候,分析一次前一天的日志信息,借此机会整理了一下定时任务的几种实现方式,由于项目采用spring框架,所以我都将 ...
- android学习笔记六
Android中Activity的Intent大全 Api Level 3: (SDK 1.5) android.intent.action.ALL_APPS android.intent.actio ...
- [转] ArcGIS engine中气泡标注的添加、修改
小生 原文 ArcGIS engine中气泡标注的添加.修改! 你微微地笑着,不同我说什么话.而我觉得,为了这个,我已等待得久了. ...