[英]Custom status bar notification for background download
I am new to android development and I try to create a background download feature for my app. 我是Android开发的新手,我尝试为我的应用创建后台下载功能。 I followed this http://developer.android.com/guide/topics/ui/notifiers/notifications.html#CustomExpandedView to create my custom notification.
我遵循了这个http://developer.android.com/guide/topics/ui/notifiers/notifications.html#CustomExpandedView来创建我的自定义通知。
The downloading is performed, I checked the downloaded file in the sdcard. 下载完成后,我在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:
这是私有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);
}
}
Note that my DownloadList class extends ListActivity. 请注意,我的DownloadList类扩展了ListActivity。
Do I need to do something more that just "notification.contentView = myContentView;" 我是否需要做更多的事情,而不仅仅是“ notification.contentView = myContentView;”。 in order to inflate the layout?
为了膨胀的布局?
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. 嗯...好吧,我将您的代码与已经可以工作的代码进行了比较...并且我看不到很多差异...但是,这些微小差异之一很可能很重要。
final Notification notification = new Notification(R.drawable.icon, "Downloading", System.currentTimeMillis());
notification.flags = notification.flags | Notification.FLAG_ONGOING_EVENT;
notification.contentView = new RemoteViews(getApplicationContext().getPackageName(), R.layout.download_progress);
notification.contentView.setImageViewResource(R.id.status_icon, R.drawable.ic_status);
notification.contentView.setTextViewText(R.id.status_text, "Downloading in progress");
notification.contentView.setProgressBar(R.id.status_progress, 100, progress, false);
Intent notificationIntent = new Intent(MainPage.mainActivity, MainPage.class);
PendingIntent contentIntent = PendingIntent.getActivity(MainPage.mainActivity, 0, notificationIntent, 0);
notification.contentIntent = contentIntent;
//getApplicationContext();
final NotificationManager notificationManager = (NotificationManager) getApplicationContext().getSystemService(
Context.NOTIFICATION_SERVICE);
notificationManager.notify(NOTIFICATION_MESSAGE, notification);
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. 首先,我查看了您的旧代码,发现NOTIF_ID = 1,我不确定这是一个好主意,因为如果其他人的ID为1,该怎么办。 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.
当然,我可能会误会这一点,但是我只是输入了792489743之类的数字,我希望没有人会使用相同的数字。 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 follows 第三,我将自己的任务称为
Service
种类如下
public class DownloadService extends IntentService {
//initializing code and stuff
private class DownloadTask extends AsyncTask<String, Void, Boolean> {
and I did it in the doInBackground
This way if the user kills the app or what not it wouldn't kill the download. 并且我在
doInBackground
这种方式,如果用户杀死了该应用程序,否则它不会杀死该下载。
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. 最后,我从来没有用过
apply
我不亲眼看到怎么会受伤,但是我还没有看到使用它或者一个例子。
Hope this helps some! 希望这对您有所帮助!
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.
我杀死了PC上的一些CPU大量进程,从而获得了更快的仿真器。
Lesson learned. 学过的知识。 Leave the heavy multitasking to pros or to another PC.
将繁重的多任务处理留给专业人士或另一台PC。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.