Android interface callback instance

Source: Internet
Author: User

Android interface callback instance

Android interface callback methods are involved everywhere. For example, a commonly used Button click event is an interface callback. You can see that you are familiar with the importance of using the interface callback method.

The simple explanation of the interface callback is: for example, my class implements the doSomething method in an interface, registers it to you, and then I will do other things, you can call my doSomething method at a certain trigger time.

Interface callback is generally written in two ways, with different implementation forms, but the specific internal implementation logic is the same.

 

Directly give the code:

Method 1:

 

Package com. callbackdemo; import android. graphics. bitmap;/*** interface ** @ 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 interface callback instance ** @ author zhongyao */public class M AinActivity extends Activity implements ImageStateInterface {private Button button; @ Overrideprotected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main);/*** instantiate control */onLincoln ();} private void onLincoln () {button = (Button) findViewById (R. id. button1); button. setOnClickListener (new OnClickListener () {@ Overridepublic void onClick (View v) {DownloadImageUtil downloadImageUtil = new DownloadImageUtil (); // call the StartDownLoad method to register MainActivity to the interface so that the interface knows, the method that implements the interface in the class to be called. DownloadImageUtil. startDownLoad (MainActivity. this, getApplicationContext () ;}}) ;}@ Overridepublic void onImageStart () {Log. d ("lincoln", "onImageStart"); button. setText ("onImageStart") ;}@ Overridepublic void onImageSuccess (final Bitmap bitmap) {Log. d ("lincoln", "onImageSuccess"); runOnUiThread (new Runnable () {@ SuppressLint ("NewApi") @ Overridepublic void run () {button. setText ("onImageSuccess"); button. setBa Ckground (new BitmapDrawable (bitmap) ;}}) ;}@ Overridepublic void onImageFailed () {}@ Overridepublic void OnEnd () {Toast. makeText (MainActivity. this, "Haha !! ", 0). show ();}}

Package com. callbackdemo; import android. content. context; import android. graphics. bitmap; import android. graphics. bitmapFactory;/*** call the method in the interface implemented by MainActivity in this class. * @ Author zhongyao */public class DownloadImageUtil {public void StartDownLoad (final ImageStateInterface imageStateInterface, final Context context) {// Where the imageStateInterface was registered, then, call the implemented interface method based on its source. // As follows, the onImageStart method implemented in MainActivity. this is called. ImageStateInterface. onImageStart (); new Thread (new Runnable () {@ Overridepublic 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 ();}}

Method 2: The implementation principle is the same, but the implementation form is different.

 

 

Package com. callbackdemo; import android. graphics. bitmap;/*** interface ** @ 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 interface callback instance ** @ author zhongyao */public class M AinActivity extends Activity {private Button button; @ Overrideprotected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main);/*** instantiate control */onLincoln ();} private void onLincoln () {button = (Button) findViewById (R. id. button1); button. setOnClickListener (new OnClickListener () {@ Overridepublic void onClick (View v) {DownloadImageUtil. startDownLo Ad (imageInterface, MainActivity. this) ;}}) ;}imagestateinterface imageInterface = new ImageStateInterface () {@ Overridepublic void onImageStart () {Log. d ("lincoln", "onImageStart"); button. setText ("onImageStart") ;}@ Overridepublic void onImageSuccess (final Bitmap bitmap) {Log. d ("lincoln", "onImageSuccess"); runOnUiThread (new Runnable () {@ SuppressLint ("NewApi") @ Overridepublic void run () {button. setText ("on ImageSuccess "); button. setBackground (new BitmapDrawable (bitmap) ;}}) ;}@ Overridepublic void onImageFailed () {}@ Overridepublic void OnEnd () {Toast. makeText (MainActivity. this, "Haha !! ", 0). show ();}};}

Package com. callbackdemo; import android. content. context; import android. graphics. bitmap; import android. graphics. bitmapFactory;/*** call the method in the interface implemented by MainActivity in this class. * @ Author zhongyao */public class DownloadImageUtil {public static void StartDownLoad (final ImageStateInterface imageInterface, final Context context) {imageInterface. onImageStart (); new Thread (new Runnable () {@ Overridepublic 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 ();}}

Effect:

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.