Callback (interface Callback) mechanism in Android
Source: Internet
Author: User
<span id="Label3"></p><p><p><span style="font-size:18px">In fact, the callback mechanism is ubiquitous in android, <span style="font-family:Arial; line-height:26px"> <span style="color:#362e2b">especially</span> at the end of the three words <span style="background-color:rgb(255,255,255)"><span style="color:#ff0000">Handler, callback, and Listener</span></span> <span style="color:#362e2b">, all implemented using the callback Mechanism. </span></span> for example, Click event Onclicklistener is an already encapsulated callback Case:</span></p></p><p><p><span style="font-size:18px"><br></span></p></p><p><p><span style="font-size:18px"></span></p></p><pre name="code" class="java"><pre name="code" class="java">Tv.setonclicklistener (new onclicklistener () {@Overridepublic void OnClick (View v) {//TODO auto-generated method Stub}}) ;</pre></pre><p><p><br></p></p><p><p><span style="font-size:14px">For the concept and significance of CALLBACK. Wikipedia explains This: pass a piece of code as a parameter, and the code will be run at some point;</span></p></p><pre><pre></pre></pre><span style="font-size:14px"><span style="font-size:14px">Personal Understanding Is: for the Code neat. In order to make it easy to invoke and run some variables and functions that are not in the same Java file, in order to facilitate the external interface, it is convenient to add new functions later;</span></span><p><p></p></p><p><p>Myself also wrote a simpler demo such as the Following:</p></p><p><p></p></p><pre name="code" class="java"><pre name="code" class="java">/** * @author Kern * @data 2015-6-12 PM 4:21:29 * @TODO callback First: define a interface includes one or more callback methods with no method body */public class Mainacti Vity extends actionbaractivity {@Overrideprotected void onCreate (Bundle Savedinstancestate) {super.oncreate ( savedinstancestate); setcontentview (r.layout.activity_main);//TextView TV = (TextView) Findviewbyid (r.id.tv);// Tv.setonclicklistener (new onclicklistener () {////@Override//public void OnClick (View v) {////TODO auto-generated metho D stub////}//}); Utils Utils = new Utils () utils.execu (new contrllistener () {@Overridepublic void Contrl (String str) {//TODO auto-generate D Method STUBLOG.E ("sos", str);});} Define interface public interface Contrllistener {public void Contrl (String str);}</pre></pre><br><span style="font-size:18px"><span style="font-size:18px">here: <span style="background-color:rgb(240,240,240)">method </span> <span style="background-color:rgb(240,240,240)">Contrl (String Str) in Interface contrllistener, no method body, late maintenance, the ability to directly replicate the control function when new functionality is Required. It is OK to call your own new feature implementation code in the method body. </span></span></span><p><p></p></p><p><p><span style="background-color:rgb(240,240,240)"><br></span></p></p><p><p></p></p><pre name="code" class="java"><pre name="code" class="java">/** * @author Kern * @data 2015-6-12 pm 5:07:21 * @TODO second step: Get interface object, call method via object, * */public class Utils {contrllistener listener;public void Execu (contrllistener listener) {this.listener = listener;new MyThread (). start (); Class MyThread extends Thread {@Overridepublic void run () {//TODO auto-generated method Stubsuper.run (); Try {thread.sleep for (int i = 0; i < I++) {listener.contrl ("the phone is being switched on----" + i + 1); Thread.Sleep (3000);} Listener.contrl ("hello, who are You?") ");} Catch (interruptedexception e) {//TODO auto-generated catch Blocke.printstacktrace ();}}}</pre></pre><p><p><br></p></p><pre><pre></pre></pre> <p><p>Callback (interface Callback) mechanism in Android</p></p></span>
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.