状态栏中的通知,通知关闭时停止异步任务
您好,我收到了从网络下载文件的通知。代码如下。我想处理通知上的点击操作,关闭它并停止 AsyncTask 作业。目前我的行为是不可理解的。就像,当我单击通知时,它关闭了它,但在调用 AsyncTask 的publishProgress 方法时再次打开。我如何处理点击通知?也许我可以采取与以前不同的方式?我将按钮放在 xml 布局
和 setOnClickListener
中以调用 cancel(boolean)
方法,但后来发现这是不可能的。
public class DownloadFileNotification extends AsyncTask<String, Integer, String> {
public DownloadVkVideoFiles(Context c, String title) {
//constructor
}
public void createNotification() {
//create notification
notificationManager = (NotificationManager) context
.getSystemService(context.NOTIFICATION_SERVICE);
Intent notificationIntent = new Intent();
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0,
notificationIntent, PendingIntent.FLAG_CANCEL_CURRENT);
RemoteViews contentView = new RemoteViews(context.getPackageName(),
R.layout.download_notification);
// TODO change to shows title
tickerText = context.getResources().getText(R.string.downloadTitle);
icon = android.R.drawable.stat_sys_download;
time = System.currentTimeMillis();
notification = new Notification(icon, tickerText, time);
notification.flags |= Notification.FLAG_ONGOING_EVENT;
notification.flags |= Notification.FLAG_AUTO_CANCEL;
contentView.setImageViewResource(R.id.downloadImage,
R.drawable.download);
contentView.setTextColor(R.id.title, notification_text_color);
contentView.setFloat(R.id.title, "setTextSize",
notification_text_size - 3);
contentView.setTextViewText(R.id.title, title);
contentView.setProgressBar(R.id.progressBar, 100, 0, false);
notification.contentIntent = pendingIntent;
notification.contentView = contentView;
notificationManager.notify(HELLO_ID, notification);
}
@Override
protected void onPreExecute() {
// execute the status bar notification
createNotification();
super.onPreExecute();
}
@Override
protected String doInBackground(String... params) {
//download file
}
@Override
public void onProgressUpdate(Integer... progress) {
//update progress bar notification
}
@Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
// result processing
}
@Override
protected void onCancelled() {
super.onCancelled();
}
}
Hello i did my notification of download file from web. Code below. I want handle click action on notification, close him and stop AsyncTask job. At current time i have incomprehensible to me actions. Like, when i click on notification its closed him but open again when call publishProgress method of AsyncTask. How i handle click on notification? May be i can do it differently than has? I put button in xml layout
and setOnClickListener
for call cancel(boolean)
method but later learned that it impossible.
public class DownloadFileNotification extends AsyncTask<String, Integer, String> {
public DownloadVkVideoFiles(Context c, String title) {
//constructor
}
public void createNotification() {
//create notification
notificationManager = (NotificationManager) context
.getSystemService(context.NOTIFICATION_SERVICE);
Intent notificationIntent = new Intent();
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0,
notificationIntent, PendingIntent.FLAG_CANCEL_CURRENT);
RemoteViews contentView = new RemoteViews(context.getPackageName(),
R.layout.download_notification);
// TODO change to shows title
tickerText = context.getResources().getText(R.string.downloadTitle);
icon = android.R.drawable.stat_sys_download;
time = System.currentTimeMillis();
notification = new Notification(icon, tickerText, time);
notification.flags |= Notification.FLAG_ONGOING_EVENT;
notification.flags |= Notification.FLAG_AUTO_CANCEL;
contentView.setImageViewResource(R.id.downloadImage,
R.drawable.download);
contentView.setTextColor(R.id.title, notification_text_color);
contentView.setFloat(R.id.title, "setTextSize",
notification_text_size - 3);
contentView.setTextViewText(R.id.title, title);
contentView.setProgressBar(R.id.progressBar, 100, 0, false);
notification.contentIntent = pendingIntent;
notification.contentView = contentView;
notificationManager.notify(HELLO_ID, notification);
}
@Override
protected void onPreExecute() {
// execute the status bar notification
createNotification();
super.onPreExecute();
}
@Override
protected String doInBackground(String... params) {
//download file
}
@Override
public void onProgressUpdate(Integer... progress) {
//update progress bar notification
}
@Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
// result processing
}
@Override
protected void onCancelled() {
super.onCancelled();
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我可以想象像广播接收器之类的东西,甚至可能是一个活动或服务,旨在接收通知点击触发的意图。该实体又可用于取消任务。
只需在正在运行的 Async 对象上调用 cancel(true) 方法即可。
我希望它有帮助..
I would imagine something like a broadcast receiver or maybe even an activity or a service being designed to receive the intent fired by the notification click. This entity in turn might be used to cancel the task.
Just call the cancel(true) method on the running Async object.
I hope it helps..