0
你好,我做了我从网上下载文件的通知。下面的代码。我想处理通知的点击操作,关闭他并停止AsyncTask工作。目前,我对自己的行为感到难以理解。比如,当我点击通知时,它关闭了他,但是在调用AsyncTask的publishProgress方法时再次打开。我如何处理点击通知?可能我可以做得比现在有所不同吗?我把按钮放在xml layout
和setOnClickListener
中拨打电话cancel(boolean)
方法,但后来得知它不可能。通知在状态栏中,当通知关闭时停止asynctask
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();
}
}
只是有趣,为什么到处写使用'while'循环,如果我可以打电话取消,没有它'asynctask'休息。现在了解如何使用服务和广播。谢谢 – Mrusful 2011-12-23 19:14:03