在我的应用中,我将图片上传到网络服务器,所以我想在通知抽屉上显示进度条和通知进度条,我尝试了一些方法,但进度对话框和通知栏显示不同的上传百分比,我不知道,如何解决这个问题。感谢任何帮助。任何人都可以帮忙吗?
进度条的方法。 在这个方法中,我调用方法在Notification抽屉上调用进度条。我在这里执行我的AsyncTask任务。
ProgressDialogueBox
public void progressDialogBox(View view){
progress=new ProgressDialog(this);
progress.setMessage("Uploading Image");
progress.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
progress.setIndeterminate(false);
progress.setCancelable(false);
progress.setCanceledOnTouchOutside(true);
progress.setProgress(0);
progress.show();
final int totalProgressTime = 100;
final Thread t = new Thread() {
@Override
public void run() {
int jumpTime = 0;
while(jumpTime < totalProgressTime) {
try {
ProgressBarNotification();
new ImageUploadTask().execute();
sleep(2000);
jumpTime += 5;
progress.setProgress(jumpTime);
}
catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
};
t.start();
}
进度通知方法
public void ProgressBarNotification(){
mNotifyManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
mBuilder = new NotificationCompat.Builder(ImageUploadActivity.this);
mBuilder.setContentTitle("Upload")
.setContentText("Upload in progress")
.setSmallIcon(R.drawable.ic_launcher);
// mBuilder.setAutoCancel(true);
Intent myIntent = new Intent(this, ImageUploadActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(ImageUploadActivity.this, 0, myIntent, Intent.FILL_IN_ACTION);
mBuilder.setContentIntent(pendingIntent);
}
图片上传活动 - 图片上传在这里完成
class ImageUploadTask extends AsyncTask<Void,Integer, String> {
@Override
protected void onPreExecute() {
super.onPreExecute();
// Displays the progress bar for the first time.
mBuilder.setProgress(100, 0, false);
mNotifyManager.notify(id, mBuilder.build());
}
@Override
protected void onProgressUpdate(Integer... values) {
// Update progress
mBuilder.setProgress(100, values[0], false);
mNotifyManager.notify(id, mBuilder.build());
super.onProgressUpdate(values);
}
@Override
protected String doInBackground(Void... unsued) {
int i;
for (i = 0; i <= 100; i += 5) {
// Sets the progress indicator completion percentage
publishProgress(Math.min(i, 100));
try {
Thread.sleep(2 * 1000);
HttpClient httpClient = new DefaultHttpClient();
HttpContext localContext = new BasicHttpContext();
HttpPost httpPost = new HttpPost("http://11.10.10.14/test/upload.php");
MultipartEntity entity = new MultipartEntity(
HttpMultipartMode.BROWSER_COMPATIBLE);
ByteArrayOutputStream bos = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, bos);
byte[] data = bos.toByteArray();
entity.addPart("uploaded_file", new ByteArrayBody(data,
"myImage.jpg"));
// String newFilename= filename.concat("file");
// newFilename=filename+newFilename;
/* entity.addPart("uploaded_file", new ByteArrayBody(data,
filename));*/
Log.e(TAG, "Method invoked");
httpPost.setEntity(entity);
HttpResponse response = httpClient.execute(httpPost,
localContext);
BufferedReader reader = new BufferedReader(
new InputStreamReader(
response.getEntity().getContent(), "UTF-8"));
StringBuilder builder = new StringBuilder();
String aux = "";
while ((aux = reader.readLine()) != null) {
builder.append(aux);
if (isCancelled()) break;
}
String sResponse = builder.toString();
return sResponse;
} catch (Exception e) {
/* if (dialog.isShowing())
dialog.dismiss();
Toast.makeText(getApplicationContext(), "Exception Message 1", Toast.LENGTH_LONG).show();
Log.e(e.getClass().getName(), e.getMessage(), e);
return null;*/
}}
// }
return null;
}
@Override
protected void onPostExecute(String sResponse) {
if (sResponse != null) {
Toast.makeText(getApplicationContext(),
"Photo uploaded successfully",
Toast.LENGTH_SHORT).show();
}
// super.onPostExecute(result);
mBuilder.setContentText("Upload complete");
// Removes the progress bar
// mBuilder.setProgress(0, 0, false);
//mNotifyManager.notify(1, mBuilder.build());
mNotifyManager.cancel( id);
}
}}
上传按钮
upload = (Button) findViewById(R.id.upload);
upload.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
if (bitmap == null) {
Toast.makeText(getApplicationContext(),
"Please select image", Toast.LENGTH_SHORT).show();
} else {
progressDialogBox(v);//ProgressBar on the Activity
}
}
});
}