I became familiar with the Android and Java platforms and wanted to create a common "NetworkHelper" class that would handle most network codes, allowing me to simply call web pages from it.
I followed this article from developer.android.com to create my network class: http://developer.android.com/training/basics/network-ops/connecting.html
the code:
package com.example.androidapp; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.Reader; import java.io.UnsupportedEncodingException; import java.net.HttpURLConnection; import java.net.URL; import android.content.Context; import android.net.ConnectivityManager; import android.net.NetworkInfo; import android.os.AsyncTask; import android.util.Log; public class NetworkHelper { private Context mContext; public NetworkHelper(Context mContext) {
}
In my activity class, I use the class as follows:
connHelper = new NetworkHelper(this);
...
if (connHelper.checkConnection()) {
The problem I am facing is that I have to somehow return the callback to the activity, and it must be defined in the "downloadUrl ()" function. For example, when the download ends, the publicoid function "handleWebpage (String data)" in the activity is called with the loaded string as its parameter.
I did some search queries and found that I need to use interfaces in some way to achieve this functionality. After considering several similar stackoverflow questions / answers, I didn’t get the job, and I'm not sure if I understood the interfaces correctly: How to pass a method as a parameter in Java? Honestly, anonymous classes are new to me, and I'm not quite sure where and how I should apply the code fragments of the example in the mentioned stream.
So my question is how to pass the callback function to my network class and call it after the download is complete? Where does the interface declaration go, implement the keyword and so on? Please note that I am starting with Java (I have a different programming background), so I will be grateful for the explanation :) Thanks!
java function android parameters interface
Tumetsu May 28 '13 at 20:16 2013-05-28 20:16
source share