Android接口回调方法处处涉及到,比如常用的Button点击事件就是一个接口回调,可见掌握熟练使用接口回调方法的重要性。
接口回调的简单解释就是:比如我这个类实现了一个接口里的方法doSomething,然后注册到你这里,然后我就去做别的事情去了, 你在某个触发的时机回头来调用我doSomething的方法。
接口回调一般有两种写法,实现形式不一样,但是具体的内部的实现逻辑是一样。
直接给出代码:
方法一:
-
package com.callbackdemo;
-
-
import android.graphics.Bitmap;
-
-
/**
-
* 接口
-
*
-
* @author zhongyao
-
*/
-
public interface ImageStateInterface {
-
void onImageStart();
-
-
void onImageSuccess(Bitmap bitmap);
-
-
void onImageFailed();
-
-
void OnEnd();
-
}
-
package com.callbackdemo;
-
-
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.view.View.OnClickListener;
-
import android.widget.Button;
-
import android.widget.Toast;
-
-
/**
-
* Android 接口回调实例
-
*
-
* @author zhongyao
-
*/
-
public class MainActivity extends Activity implements ImageStateInterface {
-
private Button button;
-
-
-
protected void onCreate(Bundle savedInstanceState) {
-
super.onCreate(savedInstanceState);
-
setContentView(R.layout.activity_main);
-
-
/**
-
* 实例化控件
-
*/
-
onLincoln();
-
-
}
-
-
private void onLincoln() {
-
button = (Button) findViewById(R.id.button1);
-
button.setOnClickListener( new OnClickListener() {
-
-
-
public void onClick(View v) {
-
-
DownloadImageUtil downloadImageUtil = new DownloadImageUtil();
-
// 调用StartDownLoad方法,目的是将MainActivity注册到接口那里,使接口知道,需要调用的是哪个类中的实现该接口的方法。
-
downloadImageUtil.StartDownLoad(MainActivity. this,
-
getApplicationContext());
-
}
-
});
-
-
}
-
-
-
public void onImageStart() {
-
Log.d( "lincoln", "onImageStart");
-
button.setText( "onImageStart");
-
-
}
-
-
-
public void onImageSuccess(final Bitmap bitmap) {
-
Log.d( "lincoln", "onImageSuccess");
-
runOnUiThread( new Runnable() {
-
-
"NewApi")(
-
-
public void run() {
-
button.setText( "onImageSuccess");
-
-
button.setBackground( new BitmapDrawable(bitmap));
-
}
-
});
-
}
-
-
-
public void onImageFailed() {
-
-
}
-
-
-
public void OnEnd() {
-
Toast.makeText(MainActivity. this, "哈哈!!", 0).show();
-
}
-
}
-
package com.callbackdemo;
-
-
import android.content.Context;
-
import android.graphics.Bitmap;
-
import android.graphics.BitmapFactory;
-
/**
-
* 在该类中调用MainActivity已实现的接口中的方法。
-
* @author zhongyao
-
*/
-
public class DownloadImageUtil {
-
public void StartDownLoad(final ImageStateInterface imageStateInterface,
-
final Context context) {
-
//该imageStateInterface使其得知,是从哪里注册过来的,然后根据其来源调用其实现的接口方法。
-
-
//如下,此时调用的就是MainActivity.this中实现的onImageStart方法。
-
imageStateInterface.onImageStart();
-
-
new Thread( new Runnable() {
-
-
-
public void run() {
-
try {
-
new Thread().sleep( 5000);
-
} catch (InterruptedException e) {
-
e.printStackTrace();
-
}
-
Bitmap bitmap = BitmapFactory.decodeResource(
-
context.getResources(), R.drawable.icon);
-
imageStateInterface.onImageSuccess(bitmap);
-
}
-
}).start();
-
-
imageStateInterface.OnEnd();
-
-
}
-
-
}
方法二:实现原理一样,只是实现形式不一样而已。
-
package com.callbackdemo;
-
-
import android.graphics.Bitmap;
-
-
/**
-
* 接口
-
*
-
* @author zhongyao
-
*/
-
public interface ImageStateInterface {
-
void onImageStart();
-
-
void onImageSuccess(Bitmap bitmap);
-
-
void onImageFailed();
-
-
void OnEnd();
-
}
-
package com.callbackdemo;
-
-
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.view.View.OnClickListener;
-
import android.widget.Button;
-
import android.widget.Toast;
-
-
/**
-
* Android 接口回调实例
-
*
-
* @author zhongyao
-
*/
-
public class MainActivity extends Activity{
-
private Button button;
-
-
-
protected void onCreate(Bundle savedInstanceState) {
-
super.onCreate(savedInstanceState);
-
setContentView(R.layout.activity_main);
-
-
/**
-
* 实例化控件
-
*/
-
onLincoln();
-
-
}
-
-
private void onLincoln() {
-
button = (Button) findViewById(R.id.button1);
-
button.setOnClickListener( new OnClickListener() {
-
-
-
public void onClick(View v) {
-
-
DownloadImageUtil.StartDownLoad(imageInterface,
-
MainActivity. this);
-
}
-
});
-
-
}
-
-
ImageStateInterface imageInterface = new ImageStateInterface() {
-
-
-
public void onImageStart() {
-
Log.d( "lincoln", "onImageStart");
-
button.setText( "onImageStart");
-
}
-
-
-
public void onImageSuccess(final Bitmap bitmap) {
-
Log.d( "lincoln", "onImageSuccess");
-
runOnUiThread( new Runnable() {
-
-
"NewApi")(
-
-
public void run() {
-
button.setText( "onImageSuccess");
-
-
button.setBackground( new BitmapDrawable(bitmap));
-
}
-
});
-
}
-
-
-
public void onImageFailed() {
-
-
}
-
-
-
public void OnEnd() {
-
Toast.makeText(MainActivity. this, "哈哈!!", 0).show();
-
}
-
};
-
-
}
-
package com.callbackdemo;
-
-
import android.content.Context;
-
import android.graphics.Bitmap;
-
import android.graphics.BitmapFactory;
-
/**
-
* 在该类中调用MainActivity已实现的接口中的方法。
-
* @author zhongyao
-
*/
-
public class DownloadImageUtil {
-
public static void StartDownLoad(final ImageStateInterface imageInterface,final Context context) {
-
imageInterface.onImageStart();
-
-
new Thread( new Runnable() {
-
-
-
public void run() {
-
try {
-
new Thread().sleep( 5000);
-
} catch (InterruptedException e) {
-
e.printStackTrace();
-
}
-
Bitmap bitmap = BitmapFactory.decodeResource(context.getResources(), R.drawable.icon);
-
imageInterface.onImageSuccess(bitmap);
-
}
-
}).start();
-
-
imageInterface.OnEnd();
-
-
}
-
-
}
效果: