Android 将进度条可见性设置为View.GONE会使整个通知不可见
我使用进度条从自定义布局创建通知。当任务完成后,我会使进度条不可见,这在Android 2.3.3和更新版本中运行良好。在旧版本中,整个通知变得不可见,没有图标,没有文本,什么都没有,但是“清除”按钮被激活,这意味着有一个通知 我是如何使用Android支持库中的Android 将进度条可见性设置为View.GONE会使整个通知不可见,android,android-notifications,android-support-library,Android,Android Notifications,Android Support Library,我使用进度条从自定义布局创建通知。当任务完成后,我会使进度条不可见,这在Android 2.3.3和更新版本中运行良好。在旧版本中,整个通知变得不可见,没有图标,没有文本,什么都没有,但是“清除”按钮被激活,这意味着有一个通知 我是如何使用Android支持库中的NotificationCompat创建通知并进行更新的 以下变量在AsyncTask类中声明为私有成员 private NotificationCompat.Builder nBuilder; private RemoteViews
NotificationCompat
创建通知并进行更新的
以下变量在AsyncTask类中声明为私有成员
private NotificationCompat.Builder nBuilder;
private RemoteViews contentView;
private Notification n;
private int nId; //Notification ID
异步任务功能
下载何时开始
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];
}
}
受保护的void onProgressUpdate(整数…进度){
super.onProgressUpdate(进度);
如果(进度[0]!=currentProgress){
如果(进度[0]>=100){
//进展100
nManager.cancel(nId);
nBuilder=新通知compat.Builder(
getApplicationContext());
nBuilder.setSmallIcon(R.drawable.ic_启动器)
.setWhen(System.currentTimeMillis())
.setTicker(mp3Name);
contentView.setProgressBar(R.id.notification\u progressbar,
100,进度[0],假);
contentView.setTextViewText(
R.id.notification_progresstext,
“”+df.format(总计/1048576f)+“MB/”
+格式(文件长度/1048576f)+“MB”);
contentView.setViewVisibility(
R.id.notification_progressbar,View.go);
意图=新意图(
android.content.Intent.ACTION(视图);
intent.setDataAndType(Uri.parse(“文件:/”+fullFilePath),
“音频/mpeg”);
intent.setFlags(intent.FLAG\u活动\u新任务
|意图。标记活动(清除任务);
PendingEvent pi=PendingEvent.getActivity(
getApplicationContext(),0,intent,
PendingEvent.FLAG_UPDATE_CURRENT);
nBuilder.setContentIntent(pi);
如果(Build.VERSION.SDK_INT>=11){
nBuilder.setContinuous(false).setContent(contentView)
.setAutoCancel(真);
n=nBuilder.build();
}否则{
n=nBuilder.build();
n、 contentView=contentView;
n、 flags=Notification.FLAG\u AUTO\u CANCEL;
}
nManager.notify(nId,n);
}否则{
//进展<100
contentView.setProgressBar(R.id.notification\u progressbar,
100,进度[0],假);
contentView.setTextViewText(
R.id.notification_progresstext,
“”+df.format(总计/1048576f)+“MB/”
+格式(文件长度/1048576f)+“MB”);
如果(Build.VERSION.SDK_INT>=11){
setContent(contentView);
n=nBuilder.build();
}否则{
n=nBuilder.build();
n、 contentView=contentView;
}
nManager.notify(nId,n);
}
//nManager.notify(nId,n);
currentProgress=进度[0];
}
}
我必须删除contentView.setViewVisibility(R.id.notification\u progressbar,View.GONE)编码>,然后它看起来很难看。编辑:
这在Android 2.3或更低版本中是不可能的
可点击的通知
按钮仅在Android 3或更高版本上有效。这没有帮助,结果相同。顺便说一句,我可以将通知标题或通知进度文本设置为View.GONE,但不能设置进度条本身。我不明白,可点击通知与进度条可见性有什么关系?我的问题在Android 2.3.3上不存在。我不确定它是否有用,但你试过View.INVISIBLE吗?@uval是的,我试过了,结果是一样的。