后台下载自定义状态栏通知 [英] Custom status bar notification for background download
问题描述
我是新android开发,我试图创建我的应用程序后台下载功能。我跟着这个的http://developer.android.com/guide/topics/ui/notifiers/notifications.html#CustomExpandedView创建我的自定义通知。
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.
在下载完成,我检查下载的文件的SD卡。此外,状态栏图标和标题适当改变。
The downloading is performed, I checked the downloaded file in the sdcard. Also,the status bar icon and title are changed properly.
的问题是,自定义布局我提供该通知不会出现(横条下展开)这是私人的AsyncTask类中的相关code部分:
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);
}
}
请注意我的downloadlist类扩展ListActivity。
Note that my DownloadList class extends ListActivity.
我需要做更多的事情,只是notification.contentView = myContentView;为了膨胀的布局?
Do I need to do something more that just "notification.contentView = myContentView;" in order to inflate the layout?
推荐答案
这是一个模拟器的问题毕竟.....
It was an emulator problem after all.....
它落后时,我拖累的通知!我杀导致以更快的仿真器在我的电脑上的一些CPU广泛的进程。
It lagged when I "dragged down" the notification! I killed some CPU extensive processes on my PC resulting to a faster emulator.
经验教训。离开沉重的多任务处理的优点或到另一台PC。
Lesson learned. Leave the heavy multitasking to pros or to another PC.
这篇关于后台下载自定义状态栏通知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!