0
我需要您的幫助!我正在嘗試更新回調文件上傳過程的狀態欄中的進度條。我在調用更新進度欄界面時嘗試兩個和兩個網絡庫導致系統凍結。我明白這意味着基本上回調過程在主線程中工作,無論我在服務中運行它們。如何正確地調用更新進度欄,以免導致系統UI凍結?使用Volley plus或OkHttp的服務的Android更新通知進度
注意:如果我更新主線程(Activity)中的通知,一切正常!這兩個示例都在運行,並且服務器按預期接收文件。在服務
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
OkHTTP代碼從這個https://stackoverflow.com/a/26376724/2127124
凌空加上代碼
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();