First, why would the interface callback? What is the interface callback?
In fact, these two issues is a problem, know the principles of natural callback interface will know why there is such a thing. We know that java is not possible to create direct interfaces instance, then the question is, if I have an interface declared as a variable, then I execute the method of this interface, the interface instance it is not how to do it? Aha, here they change the natural characteristics of java in another appearance --- "polymorphic", then the java virtual machine will naturally find its subclasses, the method is called a subclass already overloaded, here is the interface callback nature! ! We just need to give the address of its subclasses variable points on the subclass can know when to call in the call. Then we can create the class A subclass instance of an interface, an interface to create a variable in class B, the class A address to the variable class B, A is called when the variable of the interface methods of class method override, which is the interface to perform the steps callback. We will use when network requests and other time-consuming operations to the mechanisms used to get the data back to the main thread.
Second, use case
Here we have to use an instance of a network request to demonstrate how to use this mechanism, in order to try to reduce the code to make the main code to highlight, which is unnecessary fault-tolerant code did not write, and we hope in a formal project good fault tolerance.
We use a button and a imageView to demonstrate, click the button when the time to download data, use a callback interface to download data after the completion of the data transfer is displayed in imageview back in. Results are as follows:
Layout file is simple not in the posted out, we have three categories, one is the callback interface, one is the main activity, as a download and execute the callback data back Callee class, interface class look at the following:
-
import android.graphics.Bitmap;
-
-
/**
-
* Created by JimLv on 2016/5/31.
-
*/
-
public
interface CallBackInterface {
-
void result(Bitmap bm);
-
}
It defines a method for communicating data to look Callee categories:
-
import java.io.IOException;
-
import java.net.HttpURLConnection;
-
import java.net.URL;
-
-
/**
-
* Created by JimLv on 2016/5/31.
-
*/
-
public
class Callee
{
-
private
final String IMGURL =
"http://f.hiphotos.baidu.com/zhidao/pic/item/b21bb051f8198618a323ac464bed2e738ad4e688.jpg";
-
// variable to hold the interface
-
CallBackInterface mInterface;
-
-
Callee(CallBackInterface theInterface) {
-
// Here is the key, the external interface instance a reference to the class, assign values to variables
-
mInterface = theInterface;
-
// create an object of the download time
-
executeDown();
-
}
-
-
public void executeDown() {
-
new Thread(
new Runnable() {
-
@Override
-
public void run() {
-
try {
-
HttpURLConnection conn = (HttpURLConnection)
new URL(IMGURL).openConnection();
-
conn.setConnectTimeout(
5 *
1000);
-
conn.setRequestMethod(
"GET");
-
mInterface.result(BitmapFactory.decodeStream(conn.getInputStream()));
-
}
catch (IOException e) {
-
e.printStackTrace ();
-
}
-
}
-
}).start();
-
}
-
}
Here we declare an interface variable initialization method of a class in the subclass interface address is assigned to the variable and call data download method
call interface variables which method the data to the data after the download is complete, the actual implementation of the method is to perform the method in a subclass, which is the interface to the callback really do.
Finally, we look MainActivity categories:
-
public
class MainActivity extends AppCompatActivity implements View.OnClickListener {
-
private ImageView imgv;
-
private Button downImgBtn;
-
-
@Override
-
protected void onCreate(Bundle savedInstanceState) {
-
super
.onCreate (savedInstanceState);
-
setContentView(R.layout.activity_main);
-
imgv = (ImageView) findViewById(R.id.imgv);
-
downImgBtn = (Button) findViewById(R.id.downImgBtn);
-
//点击按钮去下载
-
downImgBtn.setOnClickListener(
this);
-
}
-
-
@Override
-
public void onClick(View v) {
-
//从接口回调得到数据,因为下载是在子线程中,所以这里要变到主线程中设置图片
-
new Callee(
new CallBackInterface() {
-
@Override
-
public void result(final Bitmap bm) {
-
runOnUiThread(
new Runnable() {
-
@Override
-
public void run() {
-
imgv.setImageBitmap(bm);
-
}
-
});
-
}
-
});
-
}
-
}
Here we create anonymous inner class instance of a subclass of the interface, overloaded methods in the subclass because it is run in a sub-thread, we can not operate UI, all calls runOnUiThread method to update the data to imageview, the entire it's that point of the demo is not very simple?
Scanning concerned about my micro-channel public number: