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

下面参见上述执行的模型,讲述James对Android接口回调技术的理解(结合前人的知识和自己的实践)。

使用一个比喻很形象地说明:客户端有个疑问打电话请教服务端,但服务端无法现场给出解答,相互之间约定:服务端一旦有答案,使用电话的方式反馈给客户端。

以上有三个主体:客户端、服务端和接口(方式)。

接口回调的原理框图说明:

Demo界面如下:

实际效果如下:

其中ToDoListActivity的布局XML文件设计为:使用fragment标签填充。

<LinearLayout 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"
android:orientation="vertical"
tools:context="com.demo.ToDoListActivity" >
<fragment
android:id="@+id/fragment_new_item"
android:name="com.demo.NewItemFragment"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<fragment
android:id="@+id/fragment_todo_list"
android:name="com.demo.ToDoListFragment"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>

Demo源代码:

package com.demo;
import android.app.Activity;
import android.app.Fragment;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnKeyListener;
import android.view.ViewGroup;
import android.widget.EditText;
/**
* <功能描述> 为New Item定义的Fragment
*
* @author Administrator
*/
public class NewItemFragment extends Fragment {
private static final String TAG = NewItemFragment.class.getSimpleName();
/**
* <功能描述> 步骤1:创建指定的接口
*
* @author Administrator
*/
public interface OnNewItemAddedListener {
// 步骤2:创建接口中的相关方法
public void onNewItemAdded(String newItem);
}
// 步骤3:声明回调接口的对象,接口类对象
private OnNewItemAddedListener onNewItemAddedListener;
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
try {
// 步骤4:获取父Activity,为声明的接口对象赋值
onNewItemAddedListener = (OnNewItemAddedListener) getActivity();
} catch (Exception e) {
throw new ClassCastException(activity.toString()
+ " must implement OnNewItemAddedListener");
}
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// 创建或填充Fragment的UI,并返回View对象
View contentView = inflater.inflate(R.layout.new_item_fragment,
container, false);
final EditText etContent = (EditText) contentView
.findViewById(R.id.et_content);
etContent.setOnKeyListener(new OnKeyListener() {
@Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
if (event.getAction() == KeyEvent.ACTION_DOWN) {
LogUtil.d(TAG, "KeyEvent.ACTION_DOWN");
if ((keyCode == KeyEvent.KEYCODE_DPAD_CENTER)
|| (keyCode == KeyEvent.KEYCODE_ENTER)) {
LogUtil.d(TAG, "KeyEvent.KEYCODE_ENTER");
if (!TextUtils.isEmpty(etContent.getText().toString())) {
LogUtil.d(TAG, "content:>"
+ etContent.getText().toString() + "<");
String content = etContent.getText().toString();
// 步骤5:创建某种场景,使用该接口回调方法
onNewItemAddedListener.onNewItemAdded(content);
etContent.setText("");
return true;
}
}
}
return false;
}
});
return contentView;
}
}

用户和界面UI交互的Activity源代码:

package com.demo;
import android.app.Activity;
import android.app.FragmentManager;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import com.demo.NewItemFragment.OnNewItemAddedListener;
import java.util.ArrayList;
/**
* <功能描述> 步骤6:在其他类中使用,并实现该接口
*
* @author Administrator
*/
public class ToDoListActivity extends Activity implements
OnNewItemAddedListener {
private static final String TAG = ToDoListActivity.class.getSimpleName();
private ArrayList<String> mTodoItem;
private ArrayAdapter<String> mArrayAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
initView();
initData();
// 获取FragmentManager实例,获取xml文件中的fragment
FragmentManager fragmentManager = getFragmentManager();
ToDoListFragment todoListFragment = (ToDoListFragment) fragmentManager
.findFragmentById(R.id.fragment_todo_list);
todoListFragment.setListAdapter(mArrayAdapter);
}
private void initView() {
setContentView(R.layout.main);
}
private void initData() {
mTodoItem = new ArrayList<String>();
mArrayAdapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, mTodoItem);
}
@Override
protected void onResume() {
super.onResume();
LogUtil.d(TAG, "onResume...");
}
@Override
public void onNewItemAdded(String newItem) {
// 步骤7:实现该接口,并使用其中的功能
mTodoItem.add(0, newItem);
mArrayAdapter.notifyDataSetChanged();
}
}

上述Demo的ToDoListActivity实现了自定义的NewItemFragment中的接口OnNewItemAddedListener,也就需要实现该接口中的抽象方法。通过下述方法:

// 步骤4:获取父Activity,为声明的接口对象赋值
onNewItemAddedListener = (OnNewItemAddedListener) getActivity();

将ToDoListActivity实例传递到了NewItemFragment中,也就是能够执行ToDoListActivity实例中的onNewItemAdded()方法,而这个方法由ToDoListActivity覆写了(实际执行的是覆写后的方法和逻辑)。

Android中接口回调技术总结:

Android接口回调方法总共需要执行以下7个步骤:

1. 创建指定的接口类,并创建接口中的相关方法;

2. 在某个类(一般是服务端)中声明接口对象(创建该接口类对象),即需要在该类中使用该接口;

3. 在使用到该接口对象的场景(一般也是服务端)中为该接口类对象赋值(也就是服务端一般都会实例化接口对象,使用客户端的该接口实例初始化);

5. 在该场景中(一般服务端中)使用接口回调中的方法(接口的方法);

6. 上述步骤4中的赋值对象(客户端传递的实例对象,方法有多种:new接口实例或者客户端实现该接口)需要实现该接口;

7. 上述步骤6中的类(客户端),覆写接口中的方法;

Android中的接口回调技术的更多相关文章

  1. Android 中的接口回调

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

  2. Android中通过进程注入技术改动广播接收器的优先级

    前言 这个周末又没有吊事,在家研究了怎样通过进程的注入技术改动广播接收器的优先级.关于这个应用场景是非常多的.并且也非常重要.所以就非常急的去fixed了. Android中的四大组件中有一个广播:B ...

  3. Android中通过进程注入技术修改广播接收器的优先级

    前言 这个周末又没有吊事,在家研究了如何通过进程的注入技术修改广播接收器的优先级,关于这个应用场景是很多的,而且也很重要,所以就很急的去fixed了. Android中的四大组件中有一个广播:Broa ...

  4. JAVA回调函数ANDROID中典型的回调地方

    在计算机中回调函数是指通过函数参数传递到其他代码类的,某一块可执行代码的引用,这以设计允许了底层代码调用者在高层定义的子程序. 在JAVA里面我们使用接口的方式来实现函数的回调. 回调的通俗就是:程序 ...

  5. Android中apk动态载入技术研究(2)android插件化及实现

    了解了android中类载入的前期知识点后,来看看android中DexClassLoader详细的实现     详细载入流程例如以下:     宿主程序会到文件系统比方SD卡中去载入APK[1],然 ...

  6. Android中通过进程注入技术修改系统返回的Mac地址

    致谢 感谢看雪论坛中的这位大神,分享了这个技术:http://bbs.pediy.com/showthread.php?t=186054,从这篇文章中学习到了很多内容,如果没有这篇好文章,我在研究的过 ...

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

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

  8. java中的接口回调

    [接口回调]接口回调是多态的另一种体现.接口回调是指:可以把使用某一个接口的类创建的对象的引用赋给该接口声明的接口变量中,那么该接口变量就可以调用被类实现的接口中的方法.当接口变量调用被类实现的接口中 ...

  9. Android中定义接口的方法

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

随机推荐

  1. 如何理解JS回调函数

    1.回调函数英文解释: A callback is a function that is passed as an argument to another function and is execut ...

  2. We have a problem with promises

    原文地址:http://fex.baidu.com/blog/2015/07/we-have-a-problem-with-promises/ 用Javascript的小伙伴们,是时候承认了,关于 p ...

  3. 记一次项目中的css样式复用

    本文同步至微信公众号:http://mp.weixin.qq.com/s?__biz=MzAxMzgwNDU3Mg==&mid=401616238&idx=1&sn=3c6e9 ...

  4. LoadRunner ---协议分析

    在做性能测试的时候,协议分析是困扰初学者的难题,选择错误的协议会导致Virtual User Generator 录制不到脚本:或录制的脚本不完整,有些应用可能需要选择多个协议才能完整的记录 客户端与 ...

  5. [原创.数据可视化系列之二]使用cesium三维地图展示美国全球军事基地分布

    基于浏览器的三维地图还算是一个比较高冷的东西,最主要的技术难点是如何在浏览器上 多快好省 的显示三维数据,很遗憾,还真的没有太好的的方案,只能说还有可行的方案. 很久之前用过skyline,使用CS居 ...

  6. eclipse color theme 选择一款适合你的代码样式

    1 点击eclipse菜单栏上的Help ->Install New Softwave , 点击add按钮 随便写个名字 ,添加  地址   http://eclipse-color-theme ...

  7. jquery版本清单

    PM> Install-Package jQuery -Version 1.11.2 jQuery 2.1.3 (latest stable) 315444 Wednesday, Decembe ...

  8. http之100-continue(转)

    1.http 100-continue用于客户端在发送POST数据给服务器前,征询服务器情况,看服务器是否处理POST的数据,如果不处理,客户端则不上传POST数据,如果处理,则POST上传数据.在现 ...

  9. inline(内联)函数

    1,为小操作定义一个函数的好处是:     a.可读性会强很多.     b.改变一个局部化的实现比更改一个应用中的300个出现要容易得多     c.函数可以被重用,不必为其他的应用重写代码     ...

  10. DFTX 笔试

    char aa[8] = "abcd"; printf("%d",strlen(aa));  4 printf("%d",sizeof(aa ...