自定义状态栏通知后台下载
我是 Android 开发新手,我尝试为我的应用程序创建后台下载功能。我遵循了这个 http://developer.android.com/guide/ topic/ui/notifiers/notifications.html#CustomExpandedView 创建我的自定义通知。
下载完成,我检查了sd卡中下载的文件。此外,状态栏图标和标题也已正确更改。
问题是我为通知提供的自定义布局没有出现(在栏下展开)。以下是私有 AsyncTask 类中的相关代码部分:
@Override
protected void onPreExecute() {
// create and configure the notification
notification = new Notification(R.drawable.download, "Downloading map..", System.currentTimeMillis());
notification.flags = notification.flags | Notification.FLAG_ONGOING_EVENT;
//create a custom layout for the notification
myContentView = new RemoteViews(appContext.getPackageName(), R.layout.download_progress);
myContentView.setImageViewResource(R.id.status_icon, R.drawable.ic_menu_save);
myContentView.setTextViewText(R.id.status_text, "download in progress");
myContentView.setProgressBar(R.id.status_progress, 100, 0, false);
notification.contentView = myContentView;
notification.contentView.apply(appContext, dl.getListView());
//instantiate the pending intent
Intent myIntent = new Intent(appContext, DownloadList.class);
myIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
int requestID = (int) System.currentTimeMillis();
PendingIntent myPendingIntent = PendingIntent.getActivity(appContext, requestID, myIntent, 0);
notification.contentIntent = myPendingIntent;
//add the Notification object to the notification manager
notificationManager = (NotificationManager) appContext.getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(NOTIF_ID, notification);
}
@Override
protected void onProgressUpdate(Integer... progress) {
//update progress bar
notification.contentView.setProgressBar(R.id.status_progress, 100, progress[0], false);
notificationManager.notify(NOTIF_ID, notification);
}
}
请注意,我的 DownloadList 类扩展了 ListActivity。
除了“notification.contentView = myContentView;”之外,我还需要做更多的事情吗?为了扩大布局?
I am new to android development and I try to create a background download feature for my app. I followed this http://developer.android.com/guide/topics/ui/notifiers/notifications.html#CustomExpandedView to create my custom notification.
The downloading is performed, I checked the downloaded file in the sdcard. Also,the status bar icon and title are changed properly.
The problem is that the custom layout I provide for the notification does not appear (expand under the bar). Here is the related code parts inside private AsyncTask class:
@Override
protected void onPreExecute() {
// create and configure the notification
notification = new Notification(R.drawable.download, "Downloading map..", System.currentTimeMillis());
notification.flags = notification.flags | Notification.FLAG_ONGOING_EVENT;
//create a custom layout for the notification
myContentView = new RemoteViews(appContext.getPackageName(), R.layout.download_progress);
myContentView.setImageViewResource(R.id.status_icon, R.drawable.ic_menu_save);
myContentView.setTextViewText(R.id.status_text, "download in progress");
myContentView.setProgressBar(R.id.status_progress, 100, 0, false);
notification.contentView = myContentView;
notification.contentView.apply(appContext, dl.getListView());
//instantiate the pending intent
Intent myIntent = new Intent(appContext, DownloadList.class);
myIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
int requestID = (int) System.currentTimeMillis();
PendingIntent myPendingIntent = PendingIntent.getActivity(appContext, requestID, myIntent, 0);
notification.contentIntent = myPendingIntent;
//add the Notification object to the notification manager
notificationManager = (NotificationManager) appContext.getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(NOTIF_ID, notification);
}
@Override
protected void onProgressUpdate(Integer... progress) {
//update progress bar
notification.contentView.setProgressBar(R.id.status_progress, 100, progress[0], false);
notificationManager.notify(NOTIF_ID, notification);
}
}
Note that my DownloadList class extends ListActivity.
Do I need to do something more that just "notification.contentView = myContentView;" in order to inflate the layout?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
嗯...好吧,我将您的代码与已经运行的代码进行了比较...并且我没有看到太多差异...但是,这些细微差异之一可能很重要。
首先,我查看了您的旧代码,注意到 NOTIF_ID = 1 我不太确定这是一个好主意,因为如果其他人有一个 ID 该怎么办。当然,我可能会弄错,但我只是输入了 792489743 这样的数字,我预计没有人会拥有相同的数字。我想这只是一个预防措施。
其次,我没去看看资源是否正确?堆栈跟踪说什么?我想如果那里出现问题的话它就会退出。
第三,我将我的任务作为
Service
放在自己的任务中,如下所示,我在
doInBackground
中完成了它,这样如果用户杀死应用程序或其他什么,它就不会杀死下载。最后,我从未使用过
apply
我个人并不认为它会带来什么伤害,但我也没有看到使用它的示例。希望这对一些人有帮助!
Hmm... Well I compared your code to my code that already works... and I don't see many differences... But, it is possible that one of these minor differences is important.
First, I looked at your old code and noticed that the NOTIF_ID = 1 I'm not so sure that is a good idea because what if someone else has an ID of one. Of course I could be mistaken about that, but I just pounded in a number like 792489743 and I expect no one else would have the same number. Just a precaution I suppose.
Second, I didn't get to see if the resources were correct? What does the stack trace say? I suppose that it would've just quit out on it if there was a problem there though.
Third, I put my in its own task as
Service
kinda as followsand I did it in the
doInBackground
This way if the user kills the app or what not it wouldn't kill the download.Lastly, I've never used
apply
I don't personally see how it would hurt, but I haven't seen an example that uses it either.Hope this helps some!
毕竟这是一个模拟器问题......
当我“拖下”通知时它滞后了!我在 PC 上杀死了一些占用大量 CPU 的进程,从而获得了更快的模拟器。
吸取教训。将繁重的多任务处理留给专业人士或另一台电脑。
It was an emulator problem after all.....
It lagged when I "dragged down" the notification! I killed some CPU extensive processes on my PC resulting to a faster emulator.
Lesson learned. Leave the heavy multitasking to pros or to another PC.