1

我尝试像这个例子一样创建一个进度通知。
这是我的代码:

 NotificationManager mNotifyManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    Builder mBuilder = new NotificationCompat.Builder(MyActivity.this);
    mBuilder.setContentTitle(getString(R.string.download_title))
    .setContentText(getString(R.string.downloading))
    .setSmallIcon(R.drawable.ic_notify);

    Intent resultIntent = new Intent(this, MyActivity.class);
    TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
    // Adds the back stack for the Intent (but not the Intent itself)
    stackBuilder.addParentStack(MyActivity.class);
    // Adds the Intent that starts the Activity to the top of the stack
    stackBuilder.addNextIntent(resultIntent);
    PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
    mBuilder.setContentIntent(resultPendingIntent);

通知工作正常,但是当我想更新进度时,什么也没发生,它只显示通知文本。

public void onRequestProgressUpdate(RequestProgress progress) {             
    mBuilder.setProgress(size, (int)(progress.getProgress() / size), false);
    mNotifyManager.notify(Constants.NOTIFICATION_ID, mBuilder.build());
}

我的应用程序使用 android 支持库android-support-v4.jar并将 min Sdk 版本设置为 api 7。我想从 api 级别 7 显示所有 android 版本的进度通知。
它只适用于新版本吗?我是否必须为通知创建自定义视图?

4

1 回答 1

0

你必须使用“NotificationCompat”而不是“Notification”类。尝试这样的事情:

NotificationCompat.Builder builder = new NotificationCompat.Builder(this);

builder.setContentIntent(pendInt)
  .setSmallIcon(R.drawable.ic_someicon)
  .setTicker("some title")
  .setOngoing(true)
  .setContentTitle("some content description title")
  .setContentText("some content description description");
Notification not = builder.build();

startForeground(NOTIFY_ID, not);
于 2014-06-17T17:28:07.603 回答