1
我正在使用DownloadManaegr
类从服务器下载Android应用程序。当下载完成时,广播接收器被注册。当收到DownloadManager.ACTION_DOWNLOAD_COMPLETE
意图时,会显示一个栏通知。要安装该应用程序,通知应该被复制。这是我正在做的:Android:多次注册一个接收器
DownloadManager dm = (DownloadManager) DownloadApplicationActivity.this.getSystemService(Context.DOWNLOAD_SERVICE);
DownloadManager.Request req = new DownloadManager.Request(Uri.parse(MY_LINK));
req.setTitle(MY_TITLE)
.setDescription("Downloading ....")
// download the package to the /sdcard/downlaod path.
.setDestinationInExternalPublicDir(
Environment.DIRECTORY_DOWNLOADS,
MY_PATH);
long enqueue = dm.enqueue(req);
registerReceiver(receiver, new IntentFilter(0DownloadManager.ACTION_DOWNLOAD_COMPLETE));
BroadcastReceiver receiver= new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (DownloadManager.ACTION_DOWNLOAD_COMPLETE.equals(action)) {
int id = 0;
Query query = new Query();
query.setFilterById(enqueue);
Cursor c =dm.query(query);
if (c.moveToFirst()) {
int columnIndex = c.getColumnIndex(DownloadManager.COLUMN_STATUS);
if (DownloadManager.STATUS_SUCCESSFUL == c.getInt(columnIndex)) {
// show a notification bar.
NotificationManager notificationManager = (NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE);
Notification notification = new Notification(R.drawable.icon,"",System.currentTimeMillis());
notification.flags |= Notification.FLAG_AUTO_CANCEL;
notification.flags |= Notification.FLAG_NO_CLEAR;
Intent i = new Intent(Intent.ACTION_VIEW);
// when the notification is clicked, install the app.
i.setDataAndType(Uri.fromFile(new File(Environment
.getExternalStorageDirectory() + APP_PATH)),"application/vnd.android.package-archive");
PendingIntent pendingIntent = PendingIntent.getActivity(
activity, 0, i, 0);
notification.setLatestEventInfo(activity, MY_TEXT, MY_TEXT,pendingIntent);
notification.number += 1;
notificationManager.notify(id++, notification);
}
}
};
当两个应用程序同时下载时,只显示一个通知 - 第二个通知 - 。我错过了第一个。为什么?
我这样做,notificationManager.notify(id,notification); 我将编辑我的代码。问题是当两个应用程序在相同的时间下载时,即在收到任何通知之前,我错过了第一个通知。 – 2012-07-10 09:26:54
问题是:第二个注册时,第一个接收器未注册。 – 2012-07-10 09:33:59
所以阻止你注册第一个接收者,什么让你使用两个接收器! – AAnkit 2012-07-10 09:35:46