Android下载管理器通知未在下载完成时显示

时间:2016-06-22 10:42:43

标签: android android-notifications android-download-manager

我正在使用带按钮的通知实施后台下载,该按钮建议用户下载内容。点击该按钮,它将触发下载管理器下载提供的链接,下载完成后,新的通知应显示相同的内容,但按钮文本和其功能都会更改。现在发生的事情是,在完全下载时会触发日志,但通知不会显示。这是IntentService

中的代码

注册并取消注册接收方:

@Override
    public void onStart(Intent intent, int startid) {
        super.onStart(intent, startid);
        //Define the receiver

        downloadReceiver = new BroadcastReceiver() {
            public void onReceive(Context ctxt, Intent intent) {
                String action = intent.getAction();
                if (DownloadManager.ACTION_DOWNLOAD_COMPLETE.equals(action)) {
                    Log.e("Download", "Download complete!");
                    createNotification("Download complete!", "Click here to view the content.");
                }
            }
        };

        registerReceiver(downloadReceiver,  new IntentFilter(Commons.downloadManager.ACTION_DOWNLOAD_COMPLETE));
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        unregisterReceiver(downloadReceiver);
    }

显示通知的方法:

@TargetApi(Build.VERSION_CODES.JELLY_BEAN)
    public void createNotification(String title, String message) {
        mNotificationManager = (NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE);
        NotificationCompat.BigPictureStyle notiStyle = new NotificationCompat.BigPictureStyle();

        notiStyle.setSummaryText(message);
        notiStyle.bigPicture(remote_picture);

        Intent splash = new Intent(this, MainActivity.class);
        PendingIntent contentIntent = PendingIntent.getActivity(this, 0, splash, PendingIntent.FLAG_UPDATE_CURRENT);

        // build notification
        Uri alarmSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);

        NotificationCompat.Builder mBuilder =
                new NotificationCompat.Builder(this)
                        .setSmallIcon(R.mipmap.ic_launcher)
                        .setContentTitle(title)
                        .setStyle(new NotificationCompat.BigTextStyle().bigText(message))
                        .setColor(getResources().getColor(android.R.color.black))
                        .setContentText(message)
                        .setSound(alarmSound)
                        .setAutoCancel(true)
                        .setDefaults(NotificationCompat.DEFAULT_LIGHTS)
                        .setStyle(notiStyle);

        mBuilder.setContentIntent(contentIntent);
        mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());
        NOTIFICATION_ID+=1;
    }

remote_picture是第一个通知的位图,在我开始下载内容后被解雇。 downloadManagerCommons也是静态的。

请注意,Log.e("Download", "Download complete!");会在完成时触发,但不会显示任何通知。如何在完整下载时显示通知?

0 个答案:

没有答案