Android callback function two: application example

Source: Internet
Author: User

If the concept of Android callback is not clear, see: Android callback Function one: basic concepts

1. Define the interface

 Package Com.app.util;  Public Interface Zyjcallback {   publicvoid  dataresult (String resultstring);        Public void Progress (Integer progress);        Public void start ();}


2. Entity

Package Com.app.util;import Android.os.AsyncTask; Public classZyjasynctask extends asynctask< string, Integer, String >{zyjcallback zyjcallback; //Start@Overrideprotected voidOnPreExecute () {super.onpreexecute ();    Zyjcallback.start (); }    /** * Special attention: publishprogress (10); * This sentence must be written, otherwise onprogressupdate (), this method will not be callback*/@OverrideprotectedString Doinbackground (String ...params) {publishprogress (Ten  ); Publishprogress ( -  ); Publishprogress ( -  ); Publishprogress ( -  ); return "ha ha, the operation is complete." ; }    //Onprogressupdate method for updating progress information@Overrideprotected voidonprogressupdate (Integer ... progresses) {zyjcallback.progress (progresses[0] ); } @Overrideprotected voidOnPostExecute (String result) {Super.onpostexecute (result);    Zyjcallback.dataresult (result); }     Public voidSetcallbacklistener (Zyjcallback zyjcallback) { This. Zyjcallback =Zyjcallback; }}

3. Callback

 PackageCom.android10;ImportCom.app.util.ZYJAsyncTask;ImportCom.app.util.ZYJCallBack;Importandroid.app.Activity;ImportAndroid.os.Bundle; Public classMainactivityextendsActivity {@Override Public voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate);        Setcontentview (R.layout.activity_main); Zyjasynctask Zyjasynctask=NewZyjasynctask (); Zyjasynctask.setcallbacklistener (NewZyjcallback () {@Override Public voidstart () {System.out.println ("SSSs is starting." ) ; } @Override Public voidProgress (Integer progress) {System.out.println ("SSSs Progress is:" +progress); } @Override Public voidDataresult (String resultstring) {System.out.println ("The ssss result is:" +resultstring);                }        }); Zyjasynctask.execute ("" ) ; }}


4. Operation result

SSSs started.
SSSs Progress is: 10
SSSs Progress is: 20
SSSs Progress is: 50
SSSs Progress is: 100

SSSs The result: hahaha, the operation is complete.

Android callback function two: application example

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.