如何从通知抽屉中点击和取消进度条,如 facebook 的照片上传?
How to tap and cancel progress bar from the Notification drawer like facebook's photo upload?
在我的应用程序中,我在通知栏上有一个进度条来指示照片上传,我想在用户点击进度条(如 facebook 照片)时取消上传过程 upload.How 我可以这样做吗?感谢任何帮助。
我的代码显示进度条。
mNotifyManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
mBuilder = new NotificationCompat.Builder(ImageUploadActivity.this);
mBuilder.setContentTitle("Upload")
.setContentText("Upload in progress")
.setSmallIcon(R.drawable.ic_launcher);
new ImageUploadTask().execute();
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://10.1.1.1/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);
}
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 result) {
super.onPostExecute(result);
mBuilder.setContentText("Upload completed");
// Removes the progress bar
mBuilder.setProgress(0, 0, false);
mNotifyManager.notify(id, mBuilder.build());
}
}}
从 api 级别 4.1 开始,您可以向通知添加操作按钮。要查看有关通知的基础知识,请查看 android doc.
如需更多帮助,您可以查看此 so answer and this tutorial
在我的应用程序中,我在通知栏上有一个进度条来指示照片上传,我想在用户点击进度条(如 facebook 照片)时取消上传过程 upload.How 我可以这样做吗?感谢任何帮助。
我的代码显示进度条。
mNotifyManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
mBuilder = new NotificationCompat.Builder(ImageUploadActivity.this);
mBuilder.setContentTitle("Upload")
.setContentText("Upload in progress")
.setSmallIcon(R.drawable.ic_launcher);
new ImageUploadTask().execute();
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://10.1.1.1/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);
}
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 result) {
super.onPostExecute(result);
mBuilder.setContentText("Upload completed");
// Removes the progress bar
mBuilder.setProgress(0, 0, false);
mNotifyManager.notify(id, mBuilder.build());
}
}}
从 api 级别 4.1 开始,您可以向通知添加操作按钮。要查看有关通知的基础知识,请查看 android doc.
如需更多帮助,您可以查看此 so answer and this tutorial