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

當兩個應用程序同時下載時,只顯示一個通知 - 第二個通知 - 。我錯過了第一個。爲什麼?

回答

0

因爲您打電話通知時使用相同的ID,即您的情況爲'0',並且文檔說每次都應該是唯一的。

我覺得你應該給不同的數字,每次代替0

notificationManager.notify(0, notification); 

以下是文件說什麼..

public void notify (int id, Notification notification) 

帖子的通知在狀態欄中顯示。 如果具有相同ID的通知已由您的應用程序發佈並且尚未取消,它將被更新的信息替換。

+0

我這樣做,notificationManager.notify(id,notification); 我將編輯我的代碼。問題是當兩個應用程序在相同的時間下載時,即在收到任何通知之前,我錯過了第一個通知。 – 2012-07-10 09:26:54

+0

問題是:第二個註冊時,第一個接收器未註冊。 – 2012-07-10 09:33:59

+0

所以阻止你註冊第一個接收者,什麼讓你使用兩個接收器! – AAnkit 2012-07-10 09:35:46