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