[英]Custom status bar notification for background download
我是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;”。 為了膨脹的布局?
嗯...好吧,我將您的代碼與已經可以工作的代碼進行了比較...並且我看不到很多差異...但是,這些微小差異之一很可能很重要。
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);
首先,我查看了您的舊代碼,發現NOTIF_ID = 1,我不確定這是一個好主意,因為如果其他人的ID為1,該怎么辦。 當然,我可能會誤會這一點,但是我只是輸入了792489743之類的數字,我希望沒有人會使用相同的數字。 我想只是預防措施。
其次,我看不到資源是否正確? 堆棧跟蹤說明了什么? 我想如果那里有問題的話,它就該放棄了。
第三,我將自己的任務稱為Service
種類如下
public class DownloadService extends IntentService {
//initializing code and stuff
private class DownloadTask extends AsyncTask<String, Void, Boolean> {
並且我在doInBackground
這種方式,如果用戶殺死了該應用程序,否則它不會殺死該下載。
最后,我從來沒有用過apply
我不親眼看到怎么會受傷,但是我還沒有看到使用它或者一個例子。
希望這對您有所幫助!
畢竟這是一個模擬器問題。
當我“拖延”通知時,它會滯后! 我殺死了PC上的一些CPU大量進程,從而獲得了更快的仿真器。
學過的知識。 將繁重的多任務處理留給專業人士或另一台PC。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.