Issue
To download some files on lots of activities, I thought it will be much better to integrate all the same codes into one activity like (DownloadFiles.class), but here is the problem. I have to get a progress value into my main activity (SetupActivity.class), it is impossible to do that using AsyncTask. The original code was:
private class DownloadFiles extends AsyncTask<String, Integer, String> {
private Context context;
private PowerManager.WakeLock mWakeLock;
public DownloadFiles(Context context) {
this.context = context;
}
@Override
protected String doInBackground(String... input_value) {
InputStream input = null;
OutputStream output = null;
HttpURLConnection connection = null;
try {
URL url = new URL(input_value[0]);
connection = (HttpURLConnection) url.openConnection();
connection.connect();
int fileLength = connection.getContentLength();
input = connection.getInputStream();
output = new FileOutputStream(new File(input_value[1]));
byte data[] = new byte[4096];
long total = 0;
int count;
while ((count = input.read(data)) != -1) {
if (isCancelled()) {
input.close();
return null;
}
total += count;
if (fileLength > 0)
publishProgress((int) (total * 100 / fileLength));
output.write(data, 0, count);
}
} catch (Exception e) {
return e.toString();
} finally {
try {
if (output != null) output.close();
if (input != null) input.close();
} catch (IOException ignored){
ignored.printStackTrace();
}
return "Download Complete.";
}
}
@Override
protected void onPreExecute() {
super.onPreExecute();
PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
mWakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK,
getClass().getName());
mWakeLock.acquire();
}
@Override
protected void onProgressUpdate(Integer... progress) {
super.onProgressUpdate(progress);
}
@Override
protected void onPostExecute(String result) {
mWakeLock.release();
if (!result.equals("Download Complete.")) {
} else {
}
}
}
It was unable to use onProgressUpdate to handle other activities' progressbar. The reason why I am not using ProgressDialog is since it is deprecated, it is much better to use the Progressbar which doesn't prevent user from interacting with user interfaces.
I heard that using services is one of the answers, but there are no ways to update progressbar with my knowledge.
Solution
You can also pass a callback value to the AsyncTask like this:
private class DownloadFiles extends AsyncTask<String, Integer, String> {
private Context context;
private PowerManager.WakeLock mWakeLock;
private ProgressCallback mProgressCallback
public DownloadFiles(Context context, ProgressCallback callback) {
this.context = context;
mProgressCallback = callback
}
Then you create an interface like this:
public interface ProgressCallback{
void onProgressUpdate(int percentComplete);
Have every activity or fragment which needs to show the progress implement this interface and then you can call from onProgressupdate in the asyncTask:
@Override
protected void onProgressUpdate(Integer... progress) {
super.onProgressUpdate(progress);
mProgressCallback.onProgressUpdate(progress[0])
}
You can tweak the interface and method whichever way you like.
Answered By - KvdLingen
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.