旧版Android的进度通知

时间:2013-08-23 13:18:24

标签: android android-notifications

我尝试创建this example之类的进度通知。
这是我的代码:

 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 Version是api 7.我想从api级别7显示所有Android版本的进度通知。
它是否仅适用于新版本?我是否必须为通知创建自定义视图?

1 个答案:

答案 0 :(得分: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);