Android NotificationCompat生成器显示通知问题
我想在通知栏上显示活动生命周期中每个方法的通知 像这样 我编辑了代码,因为我下面的示例实现了这一功能,但问题是只出现了一个通知,而以前的通知被替换了 像这样 以下是我正在使用的代码Android NotificationCompat生成器显示通知问题,android,Android,我想在通知栏上显示活动生命周期中每个方法的通知 像这样 我编辑了代码,因为我下面的示例实现了这一功能,但问题是只出现了一个通知,而以前的通知被替换了 像这样 以下是我正在使用的代码 package com.qcs.vivek.training_demo; import android.app.Activity; import android.app.NotificationManager; import android.content.Context; import android.os.
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());
}
}通知管理器的通知方法的第一个参数是通知的id。如果您想显示多个通知,那么您需要做的就是不要总是传递0,因为通知的id对每个生命周期方法使用不同的id