0

我从带有进度条的自定义布局创建通知。任务完成后,我使进度条不可见,这适用于 Android 2.3.3 和更新版本。在旧版本中,整个通知变得不可见,没有图标,没有文字,没有任何东西,但是“清除”按钮被激活,这意味着有一个通知。

NotificationCompat我是如何使用Android 支持库创建和更新通知的;

以下变量在 AsyncTask 类中声明为私有成员;

private NotificationCompat.Builder nBuilder;
private RemoteViews contentView;
private Notification n;
private int nId; //Notification ID

异步任务函数;

下载开始时

protected void onPreExecute() {
            super.onPreExecute();
            contentView = new RemoteViews(getPackageName(),
                    R.layout.custom_notification);
            contentView.setTextViewText(R.id.notification_title, nTitle);
            contentView.setProgressBar(R.id.notification_progressbar, 100, 0,
                    false);
            contentView.setImageViewResource(R.id.notification_image,
                    R.drawable.ic_launcher);
            contentView.setTextViewText(
                    R.id.notification_progresstext,
                    "" + df.format(total) + " MB / "
                            + df.format((float) fileLength) + " MB");

            nBuilder = new NotificationCompat.Builder(getApplicationContext());
            nBuilder.setSmallIcon(R.drawable.ic_launcher)
                    .setWhen(System.currentTimeMillis()).setTicker(mp3Name);
            if (Build.VERSION.SDK_INT >= 11) {
                // sets FLAG_ONGOING_EVENT
                nBuilder.setOngoing(true).setContent(contentView)
                        .setAutoCancel(false);
                n = nBuilder.build();
            } else {
                // required, otherwise will crash with an IllegalArgException
                Intent intent = new Intent(getApplicationContext(),
                        MainActivity.class);
                intent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
                PendingIntent pend = PendingIntent.getActivity(
                        getApplicationContext(), 0, intent,
                        PendingIntent.FLAG_UPDATE_CURRENT);
                nBuilder.setContentIntent(pend);
                n = nBuilder.build();
                n.contentView = contentView;
                // since flags are ignored in earlier version set them directly
                n.flags = Notification.FLAG_ONGOING_EVENT
                        | Notification.FLAG_NO_CLEAR;
            }

            nManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
            nManager.notify(nId, n);


            Toast.makeText(getApplicationContext(), R.string.download_started,
                    Toast.LENGTH_LONG).show();
        }

关于进度更新

protected void onProgressUpdate(Integer... progress) {
        super.onProgressUpdate(progress);
        if (progress[0] != currentProgress) {
            if (progress[0] >= 100) {
                // PROGRESS 100
                nManager.cancel(nId);

                nBuilder = new NotificationCompat.Builder(
                        getApplicationContext());
                nBuilder.setSmallIcon(R.drawable.ic_launcher)
                        .setWhen(System.currentTimeMillis())
                        .setTicker(mp3Name);

                contentView.setProgressBar(R.id.notification_progressbar,
                        100, progress[0], false);
                contentView.setTextViewText(
                        R.id.notification_progresstext,
                        "" + df.format(total / 1048576f) + " MB / "
                                + df.format(fileLength / 1048576f) + " MB");
                contentView.setViewVisibility(
                        R.id.notification_progressbar, View.GONE);

                Intent intent = new Intent(
                        android.content.Intent.ACTION_VIEW);
                intent.setDataAndType(Uri.parse("file://" + fullFilePath),
                        "audio/mpeg");
                intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
                        | Intent.FLAG_ACTIVITY_CLEAR_TASK);
                PendingIntent pi = PendingIntent.getActivity(
                        getApplicationContext(), 0, intent,
                        PendingIntent.FLAG_UPDATE_CURRENT);
                nBuilder.setContentIntent(pi);

                if (Build.VERSION.SDK_INT >= 11) {
                    nBuilder.setOngoing(false).setContent(contentView)
                            .setAutoCancel(true);
                    n = nBuilder.build();
                } else {
                    n = nBuilder.build();
                    n.contentView = contentView;
                    n.flags = Notification.FLAG_AUTO_CANCEL;

                }
                nManager.notify(nId, n);
            } else {
                // PROGRESS < 100
                contentView.setProgressBar(R.id.notification_progressbar,
                        100, progress[0], false);
                contentView.setTextViewText(
                        R.id.notification_progresstext,
                        "" + df.format(total / 1048576f) + " MB / "
                                + df.format(fileLength / 1048576f) + " MB");
                if (Build.VERSION.SDK_INT >= 11) {
                    nBuilder.setContent(contentView);
                    n = nBuilder.build();
                } else {
                    n = nBuilder.build();
                    n.contentView = contentView;
                }
                nManager.notify(nId, n);
            }

            // nManager.notify(nId, n);
            currentProgress = progress[0];
        }

    }

我必须删除contentView.setViewVisibility(R.id.notification_progressbar, View.GONE);,然后它看起来很难看。

4

1 回答 1

0

编辑:

Android 2.3 或更低版本是不可能的。

可点击Notification按钮仅适用于 Android 3 或更高版本。

于 2013-06-24T09:23:31.400 回答