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