2

我正在使用DownloadManaegr该类从服务器下载一个 Android 应用程序。下载完成时注册广播接收器。当DownloadManager.ACTION_DOWNLOAD_COMPLETE收到意图时,会显示一个条形通知。要安装应用程序,应单击通知。这就是我正在做的事情:

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

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

4

1 回答 1

0

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

我认为你应该每次都给出不同的数字来代替 0。

notificationManager.notify( 0, notification);

以下是文件所说的..

public void notify (int id, Notification notification)

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

于 2012-07-10T09:22:48.507 回答