Android Interface Callback Instance

Source: Internet
Author: User

The Android interface callback method is involved everywhere, such as the common button click event is an interface callback, can be seen to master the importance of using interface callback method.

The simple explanation for the interface callback is this: for example, my class implements a method dosomething in an interface, and then registers it with you, and then I do something else, and you turn back at some trigger time to call my DoSomething method.

The interface callback generally has two kinds of wording, the implementation form is different, but the concrete internal implementation logic is the same.


Give the code directly:

Method One:

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 Mainactivity extends Activity imple ments 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 the mainactivity with the interface, so that the interface knows what class is required to invoke the method that implements the interface. 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.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;/** * In this class, call the method in the Mainactivity implemented interface. * @author Zhongyao */public class Downloadimageutil {public void Startdownload (final imagestateinterface Imagestateinterface,final context Context) {//The imagestateinterface makes it known where it was registered from, and then calls its implemented interface method according to its source. As below, this is called the Onimagestart method implemented in Mainactivity.this. 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 Two: The principle of implementation, just the realization of the form is not the same.

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 Mainactivity extends Activity{privat E 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.startdownload (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 ("onimagesuccess"); 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;/** * In this class, call the method in the Mainactivity implemented interface. * @author Zhongyao */public class Downloadimageutil {public static void Startdownload (final imagestateinterface Imageinte Rface,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:


Android Interface Callback Instance

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.