【问题标题】:Android How to control the progressbar in a Notification (without Notification.Builder)?Android 如何控制通知中的进度条(没有 Notification.Builder)?
【发布时间】:2013-08-06 10:15:26
【问题描述】:

出于某种原因,我无法在我的代码中使用 android-support-library。为了执行一个符合 Android 4.0 风格的通知,我这样写:

private void noti() {
    NotificationManager notificationManager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
    int icon = R.drawable.ic_launcher;
    CharSequence text = "Notification Text";
    CharSequence contentTitle = "Notification Title";
    CharSequence contentText = "Sample notification text.";
    long when = System.currentTimeMillis();
    Intent intent = new Intent(this, MainActivity.class);
    PendingIntent contentIntent = PendingIntent.getActivity(this, 0, intent, 0);
    Notification notification = new Notification(icon,text,when);
    notification.setLatestEventInfo(this, contentTitle, contentText, contentIntent);
    notificationManager.notify(1, notification);
}

如何控制进度条呢? developer.google.com 中的方式是使用Notification.Builder,但 Builder 在 android-support-library 中。

我发现另一种方法是

notification.setProgressBar(int viewId, int max, int progress, boolean indeterminate)

但另一个问题是它需要一个包含进度条的自定义通知布局。如果我使用自定义布局,它将与 android 4.0 样式不匹配。

我现在能做什么?请帮助我,谢谢!

【问题讨论】:

  • 我不明白你在问什么:你想要自定义布局还是匹配 android 4.0 风格的布局?我通常使用notif.setProgress(max, progress, indeterminate) 来更新通知中的进度条。这里使用系统的默认样式。
  • @Joffrey 感谢您的评论。我需要我的通知来匹配 android 4.0 风格,因为女巫的原因我不能使用自定义布局。 notif.setProgress(max, progress, indeterminate) 是 Notification.Builder 的一种方法,女巫在 android-support-library 中。不过还是谢谢^
  • 我的错,我读的是“我需要一个自定义布局”而不是“它需要”^^ 那么NotificationCompat.Builder 在支持中,但Notification.Builder 不是直接在系统库中吗?如果您想要一些特殊功能,例如进度条,那么使用 Builder 的可能性对我来说似乎是合乎逻辑的。如果你想要向后兼容,那么支持库就在这里。

标签: android android-notifications android-progressbar android-support-library


【解决方案1】:

我通过使用来自android源代码here的自定义布局解决了这个问题,所以它看起来像一个系统通知。希望这可以帮助其他人。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多