Android:多次注册一个接收器

Android:多次注册一个接收器,android,notifications,broadcastreceiver,download-manager,Android,Notifications,Broadcastreceiver,Download Manager,我正在使用DownloadManaegr类从服务器下载Android应用程序。下载完成后,将注册广播接收器。当收到DownloadManager.ACTION\u DOWNLOAD\u COMPLETE intent时,将显示一个bar通知。要安装应用程序,应点击通知。这就是我正在做的: DownloadManager dm = (DownloadManager) DownloadApplicationActivity.this.getSystemService(Context.DOWN

我正在使用DownloadManaegr类从服务器下载Android应用程序。下载完成后,将注册广播接收器。当收到DownloadManager.ACTION\u DOWNLOAD\u COMPLETE intent时,将显示一个bar通知。要安装应用程序,应点击通知。这就是我正在做的:

    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);
                   }
            }           
    }; 

同时下载两个应用程序时,只显示一个通知-第二个通知。我错过了第一个。为什么?

因为在您的案例中,您使用相同的id(即“0”)调用notify,并且文档说每次都应该是唯一的

我想你每次都应该用不同的数字来代替0

notificationManager.notify( 0, notification);
下面是文件所说的

public void notify (int id, Notification notification)

发布要在状态栏中显示的通知。如果应用程序已经发布了一个具有相同id的通知,但尚未取消,则该通知将被更新的信息替换。

因为在您的情况下,您使用相同id(即“0”)调用notify,并且文档说它每次都应该是唯一的

我想你每次都应该用不同的数字来代替0

notificationManager.notify( 0, notification);
下面是文件所说的

public void notify (int id, Notification notification)

发布要在状态栏中显示的通知。如果应用程序已经发布了具有相同id的通知,但尚未取消,则该通知将被更新的信息替换。

我正在这样做,notificationManager.notify id,notification;我将编辑我的代码。问题是,当两个应用程序同时下载时,即在收到任何通知之前,我丢失了第一个通知。问题是:第一个接收器在第二个接收器注册时被注销。那么,是什么阻止你也注册第一个接收器,以及是什么让你使用两个接收器!!我正在注册第一个接收者,但当第二个接收者注册后,第一个接收者将被注销。您不需要两次注册同一个接收者,我正在注册,notificationManager.notify id,notification;我将编辑我的代码。问题是,当两个应用程序同时下载时,即在收到任何通知之前,我丢失了第一个通知。问题是:第一个接收器在第二个接收器注册时被注销。那么,是什么阻止你也注册第一个接收器,以及是什么让你使用两个接收器!!我正在注册第一个接收者,但是当第二个接收者注册时,第一个接收者将被注销。您不需要注册同一个接收者两次