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

同時に 2 つのアプリをダウンロードした場合、通知は 1 つ (2 つ目の通知) のみ表示されます。私は最初のものを見逃しています。なんで?

4

1 に答える 1

0

あなたのケースでは同じID、つまり「0」でnotifyを呼び出しているため、ドキュメントでは毎回一意である必要があると書かれています。

0の代わりに毎回違う数字を与えるべきだと思います。

notificationManager.notify( 0, notification);

以下はドキュメントの内容です..

public void notify (int id, Notification notification)

ステータスバーに表示される通知を投稿します。同じIDの通知がアプリケーションによってすでに投稿されていて、まだキャンセルされていない場合は、更新された情報に置き換えられます.

于 2012-07-10T09:22:48.507 に答える