我使用进度条从我的自定义布局创建通知。当任务完成后,我使进度条不可见,这适用于Android 2.3.3及更新版本。在旧版本中,整个通知变得不可见,没有图标,没有文本,没有任何内容,但“清除”按钮被激活,这意味着有通知。
我如何使用Android支持库中的NotificationCompat
创建通知并进行更新;
以下变量在AsyncTask类中声明为私有成员;
private NotificationCompat.Builder nBuilder;
private RemoteViews contentView;
private Notification n;
private int nId; //Notification ID
AsyncTask Functions;
下载开始时
protected void onPreExecute() {
super.onPreExecute();
contentView = new RemoteViews(getPackageName(),
R.layout.custom_notification);
contentView.setTextViewText(R.id.notification_title, nTitle);
contentView.setProgressBar(R.id.notification_progressbar, 100, 0,
false);
contentView.setImageViewResource(R.id.notification_image,
R.drawable.ic_launcher);
contentView.setTextViewText(
R.id.notification_progresstext,
"" + df.format(total) + " MB / "
+ df.format((float) fileLength) + " MB");
nBuilder = new NotificationCompat.Builder(getApplicationContext());
nBuilder.setSmallIcon(R.drawable.ic_launcher)
.setWhen(System.currentTimeMillis()).setTicker(mp3Name);
if (Build.VERSION.SDK_INT >= 11) {
// sets FLAG_ONGOING_EVENT
nBuilder.setOngoing(true).setContent(contentView)
.setAutoCancel(false);
n = nBuilder.build();
} else {
// required, otherwise will crash with an IllegalArgException
Intent intent = new Intent(getApplicationContext(),
MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
PendingIntent pend = PendingIntent.getActivity(
getApplicationContext(), 0, intent,
PendingIntent.FLAG_UPDATE_CURRENT);
nBuilder.setContentIntent(pend);
n = nBuilder.build();
n.contentView = contentView;
// since flags are ignored in earlier version set them directly
n.flags = Notification.FLAG_ONGOING_EVENT
| Notification.FLAG_NO_CLEAR;
}
nManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
nManager.notify(nId, n);
Toast.makeText(getApplicationContext(), R.string.download_started,
Toast.LENGTH_LONG).show();
}
进度更新
protected void onProgressUpdate(Integer... progress) {
super.onProgressUpdate(progress);
if (progress[0] != currentProgress) {
if (progress[0] >= 100) {
// PROGRESS 100
nManager.cancel(nId);
nBuilder = new NotificationCompat.Builder(
getApplicationContext());
nBuilder.setSmallIcon(R.drawable.ic_launcher)
.setWhen(System.currentTimeMillis())
.setTicker(mp3Name);
contentView.setProgressBar(R.id.notification_progressbar,
100, progress[0], false);
contentView.setTextViewText(
R.id.notification_progresstext,
"" + df.format(total / 1048576f) + " MB / "
+ df.format(fileLength / 1048576f) + " MB");
contentView.setViewVisibility(
R.id.notification_progressbar, View.GONE);
Intent intent = new Intent(
android.content.Intent.ACTION_VIEW);
intent.setDataAndType(Uri.parse("file://" + fullFilePath),
"audio/mpeg");
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
| Intent.FLAG_ACTIVITY_CLEAR_TASK);
PendingIntent pi = PendingIntent.getActivity(
getApplicationContext(), 0, intent,
PendingIntent.FLAG_UPDATE_CURRENT);
nBuilder.setContentIntent(pi);
if (Build.VERSION.SDK_INT >= 11) {
nBuilder.setOngoing(false).setContent(contentView)
.setAutoCancel(true);
n = nBuilder.build();
} else {
n = nBuilder.build();
n.contentView = contentView;
n.flags = Notification.FLAG_AUTO_CANCEL;
}
nManager.notify(nId, n);
} else {
// PROGRESS < 100
contentView.setProgressBar(R.id.notification_progressbar,
100, progress[0], false);
contentView.setTextViewText(
R.id.notification_progresstext,
"" + df.format(total / 1048576f) + " MB / "
+ df.format(fileLength / 1048576f) + " MB");
if (Build.VERSION.SDK_INT >= 11) {
nBuilder.setContent(contentView);
n = nBuilder.build();
} else {
n = nBuilder.build();
n.contentView = contentView;
}
nManager.notify(nId, n);
}
// nManager.notify(nId, n);
currentProgress = progress[0];
}
}
我必须删除contentView.setViewVisibility(R.id.notification_progressbar, View.GONE);
,然后它看起来很难看。
答案 0 :(得分:0)
修改强>
Android 2.3或更低版本无法实现。
可点击的Notification
按钮仅适用于Android 3或更高版本。