网上找的图片 感觉不错,通过图片可以很清晰的理解接口回调。
1.接口回调是什么?
接口回调是指:可以把使用某一接口的类创建的对象的引用赋给该接口声明的接口变量,那么该接口变量就可以调用被类实现的接口的方法。实际上,当接口变量调用被类实现的接口中的方法时,就是通知相应的对象调用接口的方法,这一过程称为对象功能的接口回调。看下面示例。
接口回调的实现步骤: 简单的实现例子
1、首先定义一个接口(即回调接口)
public interface CallBack {
void doSomeThing(String string);
}
2、定义一个接口帮助类,接口帮助类有方法里面有个参数以是这个接口类型的(即:CallBack)
package bawei.com.inteerface;
/**
* Created by Administrator on 2017/11/6.
*/
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);
}
}
3、具体调用类,及通过接口传递数据,回调到哪个地方
package bawei.com.inteerface;
import android.os.Bundle;
import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
public class MainActivity extends AppCompatActivity 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、首先也是定义一个接口(即回调接口)
package bawei.com.interface2;
import android.graphics.Bitmap;
/**
* Created by Administrator on 2017/11/6.
*/
public interface ImageStateInterface {
void onImageStart();
void onImageSuccess(Bitmap bitmap);
void onImageFailed();
void OnEnd();
}
2、帮助类
package bawei.com.interface2;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
/**
* Created by Administrator on 2017/11/6.
*/
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 bawei.com.interface2;
import android.annotation.SuppressLint;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity 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();
}
}