Several ways to Java/android interface calls

Source: Internet
Author: User

Although the use of handler is more common, but the interface has his unique, more intuitive, and then reduce the coupling

If there is an interface, you need to pass the data to the activity in use, the interface is as follows

 Public Interface pushvalue {        void push (byte V,byte  v2);    }

1: Determine if the context inherits and notifies the calling interface

if instanceof pushvalue) {                (Pushvalue) context). Push ((byte) (Crc16&0xff), (byte) ((0xff00 & CRC16) > > 8));            }

At this point, you only need to cast the interface and invoke it at the data passing point, and then implements the interface and implement its push method at the point of use (such as activity).

@Override      Public void push (byte V,byte  v2) {        tvcrcresult.settext ("");        Tvcrcresult.settext (converthexbytetostring (v)+ "  " +converthexbytetostring (v2));    

The way of 2:setcallback

1 method you will find that we need to send a context value to the data source, but the code after the transfer of the coupling becomes stronger, so the second method solves this problem

Private Pushvalue Pushvalue;  Public void Setpushvaluecallback (Pushvalue pushvaluecallback) {        this. pushvalue = pushvaluecallback;} 

First declare the interface in the establishment of a set method, the role of the external will be instantiated interface, and then directly at the source of the data generated in the interface method can be called

Pushvalue.push ((byte) (Crc16&0xff), (byte) ((0xff00 & CRC16) >> 8);

After using a class that uses the Setpushvaluecallback method, the external instance calls the Setpushvaluecallback method directly to

new XXX class. Setpushvaluecallback (new  Crc16.pushvalue () {            @Override            public  void push (bytebyte  v2) {                      // perform related actions             }        });        

This is modeled after the Android binding Sentinel listener written, this is similar to the listener, the data source has data transmission, the use of the place will be heard changes and then do the corresponding action

What is the difference between an interface and a handler?

Calling the handler system will put your handler in the main thread, but the interface we call is still running in the original thread.

Several ways to Java/android interface calls

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.