NotificationCompat Builder显示通知问题

时间:2014-06-22 12:21:29

标签: android

我想显示通知栏上活动生命周期中每个方法的通知

像这样

enter image description here

我编辑了代码作为我要遵循的示例,这与GB及以下版本有关,但问题是只有1个通知出现而前一个被替换 像这样

enter image description here

这是我正在使用的代码

package com.qcs.vivek.training_demo;

import android.app.Activity;
import android.app.NotificationManager;
import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.NotificationCompat;

public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    notify("onCreate");
}

@Override
protected void onPause() {
    super.onPause();
    notify("onPause");
}

@Override
protected void onResume() {
    super.onResume();
    notify("onResume");
}

@Override
protected void onStop() {
    super.onStop();
    notify("onStop");
}

@Override
protected void onDestroy() {
    super.onDestroy();
    notify("onDestroy");
}

@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
    super.onRestoreInstanceState(savedInstanceState);
    notify("onRestoreInstanceState");
}

@Override
protected void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(outState);
    notify("onSaveInstanceState");
}

private void notify(String methodName) {
    String name = this.getClass().getName();
    String strings = getResources().getString(R.string.app_name);

    NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(
            this).setSmallIcon(R.drawable.ic_launcher)
            .setContentTitle(methodName + " " + strings)
            .setContentText(name);

    NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    notificationManager.notify(0, mBuilder.build());

}

}

1 个答案:

答案 0 :(得分:0)

NotificationManager的通知方法的第一个参数是通知的id。如果要显示多个通知,则只需要传递0,而不是总是传递0,因为通知的id对每个生命周期方法使用不同的id。