Android 接口回调实例

Android接口回调方法处处涉及到,比如常用的Button点击事件就是一个接口回调,可见掌握熟练使用接口回调方法的重要性。

接口回调的简单解释就是:比如我这个类实现了一个接口里的方法doSomething,然后注册到你这里,然后我就去做别的事情去了, 你在某个触发的时机回头来调用我doSomething的方法。

接口回调一般有两种写法,实现形式不一样,但是具体的内部的实现逻辑是一样。


直接给出代码:

方法一:

  1. package com.callbackdemo;
  2. import android.graphics.Bitmap;
  3. /**
  4. * 接口
  5. *
  6. * @author zhongyao
  7. */
  8. public interface ImageStateInterface {
  9. void onImageStart();
  10. void onImageSuccess(Bitmap bitmap);
  11. void onImageFailed();
  12. void OnEnd();
  13. }

  1. package com.callbackdemo;
  2. import android.annotation.SuppressLint;
  3. import android.app.Activity;
  4. import android.graphics.Bitmap;
  5. import android.graphics.drawable.BitmapDrawable;
  6. import android.os.Bundle;
  7. import android.util.Log;
  8. import android.view.View;
  9. import android.view.View.OnClickListener;
  10. import android.widget.Button;
  11. import android.widget.Toast;
  12. /**
  13. * Android 接口回调实例
  14. *
  15. * @author zhongyao
  16. */
  17. public class MainActivity extends Activity implements ImageStateInterface {
  18. private Button button;
  19. @Override
  20. protected void onCreate(Bundle savedInstanceState) {
  21. super.onCreate(savedInstanceState);
  22. setContentView(R.layout.activity_main);
  23. /**
  24. * 实例化控件
  25. */
  26. onLincoln();
  27. }
  28. private void onLincoln() {
  29. button = (Button) findViewById(R.id.button1);
  30. button.setOnClickListener( new OnClickListener() {
  31. @Override
  32. public void onClick(View v) {
  33. DownloadImageUtil downloadImageUtil = new DownloadImageUtil();
  34. // 调用StartDownLoad方法,目的是将MainActivity注册到接口那里,使接口知道,需要调用的是哪个类中的实现该接口的方法。
  35. downloadImageUtil.StartDownLoad(MainActivity. this,
  36. getApplicationContext());
  37. }
  38. });
  39. }
  40. @Override
  41. public void onImageStart() {
  42. Log.d( "lincoln", "onImageStart");
  43. button.setText( "onImageStart");
  44. }
  45. @Override
  46. public void onImageSuccess(final Bitmap bitmap) {
  47. Log.d( "lincoln", "onImageSuccess");
  48. runOnUiThread( new Runnable() {
  49. @SuppressLint( "NewApi")
  50. @Override
  51. public void run() {
  52. button.setText( "onImageSuccess");
  53. button.setBackground( new BitmapDrawable(bitmap));
  54. }
  55. });
  56. }
  57. @Override
  58. public void onImageFailed() {
  59. }
  60. @Override
  61. public void OnEnd() {
  62. Toast.makeText(MainActivity. this, "哈哈!!", 0).show();
  63. }
  64. }

  1. package com.callbackdemo;
  2. import android.content.Context;
  3. import android.graphics.Bitmap;
  4. import android.graphics.BitmapFactory;
  5. /**
  6. * 在该类中调用MainActivity已实现的接口中的方法。
  7. * @author zhongyao
  8. */
  9. public class DownloadImageUtil {
  10. public void StartDownLoad(final ImageStateInterface imageStateInterface,
  11. final Context context) {
  12. //该imageStateInterface使其得知,是从哪里注册过来的,然后根据其来源调用其实现的接口方法。
  13. //如下,此时调用的就是MainActivity.this中实现的onImageStart方法。
  14. imageStateInterface.onImageStart();
  15. new Thread( new Runnable() {
  16. @Override
  17. public void run() {
  18. try {
  19. new Thread().sleep( 5000);
  20. } catch (InterruptedException e) {
  21. e.printStackTrace();
  22. }
  23. Bitmap bitmap = BitmapFactory.decodeResource(
  24. context.getResources(), R.drawable.icon);
  25. imageStateInterface.onImageSuccess(bitmap);
  26. }
  27. }).start();
  28. imageStateInterface.OnEnd();
  29. }
  30. }

方法二:实现原理一样,只是实现形式不一样而已。

  1. package com.callbackdemo;
  2. import android.graphics.Bitmap;
  3. /**
  4. * 接口
  5. *
  6. * @author zhongyao
  7. */
  8. public interface ImageStateInterface {
  9. void onImageStart();
  10. void onImageSuccess(Bitmap bitmap);
  11. void onImageFailed();
  12. void OnEnd();
  13. }

  1. package com.callbackdemo;
  2. import android.annotation.SuppressLint;
  3. import android.app.Activity;
  4. import android.graphics.Bitmap;
  5. import android.graphics.drawable.BitmapDrawable;
  6. import android.os.Bundle;
  7. import android.util.Log;
  8. import android.view.View;
  9. import android.view.View.OnClickListener;
  10. import android.widget.Button;
  11. import android.widget.Toast;
  12. /**
  13. * Android 接口回调实例
  14. *
  15. * @author zhongyao
  16. */
  17. public class MainActivity extends Activity{
  18. private Button button;
  19. @Override
  20. protected void onCreate(Bundle savedInstanceState) {
  21. super.onCreate(savedInstanceState);
  22. setContentView(R.layout.activity_main);
  23. /**
  24. * 实例化控件
  25. */
  26. onLincoln();
  27. }
  28. private void onLincoln() {
  29. button = (Button) findViewById(R.id.button1);
  30. button.setOnClickListener( new OnClickListener() {
  31. @Override
  32. public void onClick(View v) {
  33. DownloadImageUtil.StartDownLoad(imageInterface,
  34. MainActivity. this);
  35. }
  36. });
  37. }
  38. ImageStateInterface imageInterface = new ImageStateInterface() {
  39. @Override
  40. public void onImageStart() {
  41. Log.d( "lincoln", "onImageStart");
  42. button.setText( "onImageStart");
  43. }
  44. @Override
  45. public void onImageSuccess(final Bitmap bitmap) {
  46. Log.d( "lincoln", "onImageSuccess");
  47. runOnUiThread( new Runnable() {
  48. @SuppressLint( "NewApi")
  49. @Override
  50. public void run() {
  51. button.setText( "onImageSuccess");
  52. button.setBackground( new BitmapDrawable(bitmap));
  53. }
  54. });
  55. }
  56. @Override
  57. public void onImageFailed() {
  58. }
  59. @Override
  60. public void OnEnd() {
  61. Toast.makeText(MainActivity. this, "哈哈!!", 0).show();
  62. }
  63. };
  64. }

  1. package com.callbackdemo;
  2. import android.content.Context;
  3. import android.graphics.Bitmap;
  4. import android.graphics.BitmapFactory;
  5. /**
  6. * 在该类中调用MainActivity已实现的接口中的方法。
  7. * @author zhongyao
  8. */
  9. public class DownloadImageUtil {
  10. public static void StartDownLoad(final ImageStateInterface imageInterface,final Context context) {
  11. imageInterface.onImageStart();
  12. new Thread( new Runnable() {
  13. @Override
  14. public void run() {
  15. try {
  16. new Thread().sleep( 5000);
  17. } catch (InterruptedException e) {
  18. e.printStackTrace();
  19. }
  20. Bitmap bitmap = BitmapFactory.decodeResource(context.getResources(), R.drawable.icon);
  21. imageInterface.onImageSuccess(bitmap);
  22. }
  23. }).start();
  24. imageInterface.OnEnd();
  25. }
  26. }

效果:

猜你喜欢

转载自blog.csdn.net/hdhhd/article/details/80985737