2014-06-22 52 views
0

我想在活動生命週期中的通知欄上顯示來自每一個方法的通知NotificationCompat生成器顯示通知的問題

這樣

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()); 

} 

}

回答

0

的代碼NotificationManager「的第一個參數s通知方法是通知的ID。如果您想要顯示多個通知,則您需要做的只是傳遞0,而不是通知的ID爲每個生命週期方法使用不同的ID。