1.接口回调是什么?
接口回调是指:可以把使用某一接口的类创建的对象的引用赋给该接口声明的接口变量,那么该接口变量就可以调用被类实现的接口的方法。实际上,当接口变量调用被类实现的接口中的方法时,就是通知相应的对象调用接口的方法,这一过程称为对象功能的接口回调。看下面示例。
接口回调的实现步骤: 简单的实现例子
1、首先定义一个接口(即回调接口)
- package test.ban.com.callback.SimpleCallBack;
- /**
- * Created by apple on 16/8/30.
- */
- public interface CallBack {
-
- void doSomeThing(String string);
- }
2、定义一个接口帮助类,接口帮助类有方法里面有个参数以是这个接口类型的(即:CallBack)
- package test.ban.com.callback.SimpleCallBack;/**
- * Created by apple on 16/8/30.
- */
- /**
- * 作者:ban on 16/8/30 16:58
- */
- public class CallBackUtils {
- private static CallBack mCallBack;
- public static void setCallBack(CallBack callBack) {
- mCallBack = callBack;
- }
- public static void doCallBackMethod(){
- String info = "这里CallBackUtils即将发送的数据。";
- mCallBack.doSomeThing(info);
- }
- }
- package test.ban.com.callback.SimpleCallBack;/**
- * Created by apple on 16/8/30.
- */
- import android.app.Activity;
- import android.os.Bundle;
- import android.os.Handler;
- import android.util.Log;
- /**
- * 作者:ban on 16/8/30 16:58
- */
- public class SimpleCallBackTest extends Activity implements CallBack {
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- CallBackUtils.setCallBack(this);
- //1s后调用CallBackUtils的doCallBackMethod()方法。
- new Handler().postDelayed(new Runnable() {
- @Override
- public void run() {
- /**
- * 调用doCallBackMethod方法,目的是将SimpleCallBackTest注册到接口那里,
- * 使接口知道,需要调用的是哪个类中的,实现该接口的方法。
- * 调用CallBackUtils.doCallBackMethod()即调用了CallBack.doSomeThing(info)方法;
- * 通过接口就可以把数据传输到这个类里的doSomeThing()方法里。
- */
- //
- CallBackUtils.doCallBackMethod();
- }
- }, 1000);
- }
- @Override
- public void doSomeThing(String string) {
- Log.e("========", "拿到CallBackUtils的传递过来的数据=====" + string);
- }
- }
个人总结:1,先定义一个接口interface A,里面有方法m();
2,在新建一个类 class B;在B中实例化一个A a; 创建一个方法b1()将a作为参数;再创建一个方法b2调用a.m();
3 , 再创建一个类 class C实现接口A,调用B里面的方法b1(this);再实现b2;
复杂点的实现例子
2、帮助类
3、两种实现方式。第二种方式 即找个匿名接口实现类帮助我们()
1、首先也是定义一个接口(即回调接口)
- <pre name="code" class="java">package test.ban.com.callback;/**
- * Created by apple on 16/8/30.
- */
- import android.graphics.Bitmap;
- /**
- * 作者:ban on 16/8/30 16:10
- */
- public interface ImageStateInterface {
- void onImageStart();
- void onImageSuccess(Bitmap bitmap);
- void onImageFailed();
- void OnEnd();
- }
2、帮助类
- package test.ban.com.callback;/**
- * Created by apple on 16/8/30.
- */
- import android.content.Context;
- import android.graphics.Bitmap;
- import android.graphics.BitmapFactory;
- /**
- * 作者:ban on 16/8/30 16:11
- */
- public class DownloadImageUtil {
- public static void StartDownLoad(final ImageStateInterface imageStateInterface,
- final Context context) {
- //该imageStateInterface使其得知,是从哪里注册过来的,然后根据其来源调用其实现的接口方法。
- //如下,此时调用的就是MainActivity.this中实现的onImageStart方法。
- imageStateInterface.onImageStart();
- new Thread(new Runnable() {
- @Override
- public void run() {
- try {
- new Thread().sleep(5000);
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- Bitmap bitmap = BitmapFactory.decodeResource(
- context.getResources(), R.mipmap.ic_launcher);
- imageStateInterface.onImageSuccess(bitmap);
- }
- }).start();
- imageStateInterface.OnEnd();
- }
- }
3、两种实现方式。第二种方式 即找个匿名接口实现类帮助我们()
- package test.ban.com.callback;/**
- * Created by apple on 16/8/30.
- */
- import android.annotation.SuppressLint;
- import android.app.Activity;
- import android.graphics.Bitmap;
- import android.graphics.drawable.BitmapDrawable;
- import android.os.Bundle;
- import android.util.Log;
- import android.view.View;
- import android.widget.Button;
- import android.widget.TextView;
- import android.widget.Toast;
- /**
- * 作者:ban on 16/8/30 16:10
- */
- public class MainActivity extends Activity implements ImageStateInterface {
- private Button button;
- private TextView mTextView;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- /**
- * 实例化控件
- */
- //实现方式一 (推荐)
- onLincoln();
- //实现方式二
- //onLincolnTwo();
- }
- /**
- * 实现方式一 这个需要 implements ImageStateInterface 接口
- */
- private void onLincoln() {
- button = (Button) findViewById(R.id.button);
- mTextView = (TextView) findViewById(R.id.tv);
- button.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- DownloadImageUtil downloadImageUtil = new DownloadImageUtil();
- /**
- 调用StartDownLoad方法,目的是将MainActivity注册到接口那里,
- 使接口知道,需要调用的是哪个类中的实现该接口的方法。
- */
- downloadImageUtil.StartDownLoad(MainActivity.this,
- getApplicationContext());
- }
- });
- }
- @Override
- public void onImageStart() {
- Log.d("lincoln", "onImageStart");
- button.setText("onImageStart");
- mTextView.setText("onImageStart");
- }
- @Override
- public void onImageSuccess(final Bitmap bitmap) {
- Log.d("lincoln", "onImageSuccess");
- runOnUiThread(new Runnable() {
- @SuppressLint("NewApi")
- @Override
- public void run() {
- button.setText("onImageSuccess");
- mTextView.setText("onImageSuccess");
- button.setBackground(new BitmapDrawable(bitmap));
- }
- });
- }
- @Override
- public void onImageFailed() {
- }
- @Override
- public void OnEnd() {
- Toast.makeText(MainActivity.this, "完成啦!!", Toast.LENGTH_SHORT).show();
- }
- /**---------------------实现方式一 结束------------------------*/
- /**
- * --------------------------实现方式二-----------------------------
- */
- private void onLincolnTwo() {
- button = (Button) findViewById(R.id.button);
- button.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- DownloadImageUtil.StartDownLoad(imageInterface,
- MainActivity.this);
- }
- });
- }
- ImageStateInterface imageInterface = new ImageStateInterface() {
- @Override
- public void onImageStart() {
- Log.d("lincoln", "onImageStart");
- button.setText("onImageStart");
- }
- @Override
- public void onImageSuccess(final Bitmap bitmap) {
- Log.d("lincoln", "onImageSuccess");
- runOnUiThread(new Runnable() {
- @SuppressLint("NewApi")
- @Override
- public void run() {
- button.setText("onImageSuccess");
- button.setBackground(new BitmapDrawable(bitmap));
- }
- });
- }
- @Override
- public void onImageFailed() {
- }
- @Override
- public void OnEnd() {
- Toast.makeText(MainActivity.this, "哈哈!!", Toast.LENGTH_SHORT).show();
- }
- };
- /**-----------------------------------------------------------------------------*/
- }