I need your help! I am trying to update the progress bar in the status bar of a callback file upload procedure. I tried two and both network lib when calling the update progress bar interface cause the system to freeze. I understand this to mean that basically callback procedure works in the main thread no matter what I run them in service. How do I properly call the update progress bar, so as not to cause the system UI to freeze?
Note: if i update notification in main thread (Activity) all work fine! Both examples are working, and the server receives the file as expected.
OkHTTP code in service
mUBuilder.setContentTitle("Upload image")
.setContentText("")
.setAutoCancel(false)
.setContentIntent(PendingIntent.getActivity(getApplicationContext(), 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT))
.setSmallIcon(R.drawable.ic_launcher);
try {
final File file = new File(IMAGE_PATH);
final long totalSize = file.length();
RequestBody requestBody = new MultipartBody.Builder()
.addPart(Headers.of("Content-Disposition", "form-data; name=\"image\"; filename=\"" + file.getName() + "\""),
new CountingFileRequestBody(file, "image/*", new CountingFileRequestBody.ProgressListener() {
@Override
public void transferred(long num) {
final float progress = (num / (float) totalSize) * 100;
Log.d(TAG, "OUT THREAD: " + progress); //see in logs
new Thread(
new Runnable() {
@Override
public void run() {
Log.d(TAG, "IN THREAD: " + progress); //not visible in the logs
mUBuilder.setProgress(100,(int) progress, false);
mNotifyManager.notify(AppSettings.SEND_DATA_NOTIF, mUBuilder.build());
}
}
);
}
}))
.build();
Request request = new Request.Builder()
.url("http://posttestserver.com/post.php?dir=123")
.post(requestBody)
.build();
client.newCall(request).enqueue(new Callback() {
@Override public void onFailure(Call call, IOException e) {
e.printStackTrace();
}
@Override public void onResponse(Call call, Response response) throws IOException {
if (!response.isSuccessful()) throw new IOException("Unexpected code " + response);
Headers responseHeaders = response.headers();
for (int i = 0, size = responseHeaders.size(); i < size; i++) {
System.out.println(responseHeaders.name(i) + ": " + responseHeaders.value(i));
}
mUBuilder.setContentTitle("Upload complete!");
mNotifyManager.notify(AppSettings.SEND_DATA_NOTIF, mUBuilder.build());
System.out.println(response.body().string());
}
});
}catch(Exception e){
e.printStackTrace();
}
CountingFileRequestBody.java from this https://stackoverflow.com/a/26376724/2127124
Volley Plus code
mNotifyManager = (NotificationManager) getApplicationContext().getSystemService(Context.NOTIFICATION_SERVICE);
notificationIntent = new Intent(getApplicationContext(), NMainActivity.class);
final NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(getApplicationContext());
mBuilder.setContentTitle("Upload image")
.setContentText("")
.setAutoCancel(false)
.setContentIntent(PendingIntent.getActivity(getApplicationContext(), 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT))
.setSmallIcon(R.drawable.ic_launcher);
SimpleMultiPartRequest jsonRequest = new SimpleMultiPartRequest(Request.Method.POST, "http://posttestserver.com/post.php?dir=123",
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
Log.i(getClass().getName(), response);
mBuilder.setContentText("Upload image complete!");
mNotifyManager.notify(AppSettings.SEND_DATA_NOTIF, mBuilder.build());
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.e(getClass().getName(), error.toString());
}
}) {
@Override
protected Map<String, String> getParams() throws AuthFailureError {
return mParams;
}
};
jsonRequest.addFile("images", (IMAGE_PATH);
jsonRequest.setFixedStreamingMode(true);
jsonRequest.setShouldCache(false);
jsonRequest.setShouldRetryServerErrors(true);
jsonRequest.setOnProgressListener(new Response.ProgressListener() {
@Override
public void onProgress(long transferredBytes, long totalSize) {
final int percentage = (int) ((transferredBytes / ((float) totalSize)) * 100);
mBuilder.setProgress(100, percentage, false);
mNotifyManager.notify(AppSettings.SEND_DATA_NOTIF, mBuilder.build());
//freeze system UI
}
});
mRequestQueue = Volley.newRequestQueue(getApplicationContext());
mRequestQueue.add(jsonRequest);
mRequestQueue.start();