我正在尝试在上传过程中更新通知栏。通知显示但其进度没有变化,最后在上传完成时,通知会中断。通知进度的可能原因没有改变?
我的代码:
`
class UploadFile extends AsyncTask<File, Integer, String> {
@Override
protected String doInBackground(File... params) {
String fileName = params[0].getName().replaceAll(" ", "_");
fileName = "gursahib".replaceAll(" ", "_") + ".mp4";
long currentTimeMillis = System.currentTimeMillis();
if (uploadFile(params[0], currentTimeMillis + "_" + fileName).equals("200")) {
/*
* if(postRequest("http://www.phando.com/user/media",
* getMediaRequestParameters(fileName, currentTimeMillis)) ==
* 200){
*
* }else { return "post media failed."; }
*/
} else
return "File Not Uploaded Successfully.";
// postRequest("http://www.phando.com/user/transcoding",
// getTranscodingRequest());
// postRequest("http://www.phando.com/user/ticket",
// getTicketRequest());
// Log.d("gursahib", "done");
return fileName;
}
@SuppressWarnings("deprecation")
private String uploadFile(File file, String fileName) {
String responseString = null;
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(Config.FILE_UPLOAD_URL);
int statusCode = 0;
try {
AndroidMultiPartEntity entity = new AndroidMultiPartEntity(new ProgressListener() {
@Override
public void transferred(long num) {
publishProgress((int) ((num / (float) totalSize) * 100));
}
});
File sourceFile = file;
// Adding file data to http body
entity.addPart("image", new FileBody(sourceFile, ContentType.DEFAULT_BINARY, fileName));
totalSize = entity.getContentLength();
httppost.setEntity(entity);
// Making server call
HttpResponse response = httpclient.execute(httppost);
HttpEntity r_entity = response.getEntity();
statusCode = response.getStatusLine().getStatusCode();
if (statusCode == 200) {
responseString = EntityUtils.toString(r_entity);
} else {
responseString = "Error occurred! Http Status Code: " + statusCode;
}
} catch (ClientProtocolException e) {
responseString = e.toString();
} catch (IOException e) {
responseString = e.toString();
}
return String.valueOf(statusCode);
}
private int postRequest(String url, List<NameValuePair> nameValuePairs) {
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(url);
try {
// Add your data
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
// Execute HTTP Post Request
HttpResponse response = httpclient.execute(httppost);
return response.getStatusLine().getStatusCode();
} catch (ClientProtocolException e) {
Log.e("error", e.getMessage());
} catch (IOException e) {
Log.e("error", e.getMessage());
}
return 0;
}
@Override
protected void onPreExecute() {
super.onPreExecute();
}
@Override
protected void onProgressUpdate(Integer... progress) {
notificationBuilder.setProgress(100, progress[0], false);
super.onProgressUpdate();
}
@Override
protected void onPostExecute(String result) {
notificationManager.cancel(notificationID);
super.onPostExecute(result);
}
}
`
控件转到onProgressUpdate
,但通知无法更改
答案 0 :(得分:0)
你还需要通知AsyncTask的onProgressUpdate
notificationBuilder.setProgress(100, values[0], false);
notificationBuilder.notify(id,notificationBuilder.build());
答案 1 :(得分:0)
您需要将build()调用到构建器。 之后,通知您的通知经理。
Notification notification =
notificationBuilder.setProgress(100, progress[0], false).build();
notificationManager.notify(notificationID, notification);
请注意,您还需要为构建器设置图标或其他人员。 此SDK代码可能对您有所帮助。