【发布时间】:2011-10-08 19:07:01
【问题描述】:
我是 android 开发的新手,我尝试为我的应用创建后台下载功能。我按照这个http://developer.android.com/guide/topics/ui/notifiers/notifications.html#CustomExpandedView 创建了我的自定义通知。
下载完成,我检查了sdcard中的下载文件。此外,状态栏图标和标题已正确更改。
问题是我为通知提供的自定义布局没有出现(在栏下展开)。这是私有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;”之外,我还需要做更多的事情吗?为了让布局膨胀?
【问题讨论】:
-
那是一大堆代码要在那里整理!尝试更仔细地查明问题;它会帮助你找到答案
-
按照您的建议,我隔离了与通知相关的代码部分以帮助解决问题。
标签: android layout notifications progress-bar status