我想獲得一個簡單的通知。我見過很多使用舊的API方法的例子,但我想用新的方法。我已經從API資源中將這些代碼放在一起,並且運行時沒有錯誤。但是,我沒有看到任何通知。我有最低限度的要求查看通知,至少,我相信從我讀過的內容。這裏是我的代碼:通知方法NotificationCompat - 沒有錯誤 - 沒有通知
public class Login extends Activity {
public static Context ctx;
public void onCreate(Bundle savedInstanceState) {
ctx = this;
}
private static void notice(String msgfrom, String msg) {
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(ctx);
mBuilder.setSmallIcon(R.drawable.ic_launcher);
mBuilder.setContentTitle("New message from " + msgfrom.toString());
mBuilder.setContentText(msg);
mBuilder.build();
}
}
感謝以下的幫助下,我修改了代碼,這
NotificationCompat.Builder nb = new NotificationCompat.Builder(ctx);
nb.setSmallIcon(R.drawable.ic_launcher);
nb.setContentTitle("New message from " + msgfrom.toString());
nb.setContentText(msg);
nb.setAutoCancel(true);
Notification notification = nb.build();
NotificationManager NM = (NotificationManager)
ctx.getSystemService(NOTIFICATION_SERVICE);
NM.notify(0, notification);
最優秀的..謝謝你...我縮短了我的代碼爲我的情況,但它是你的代碼讓我在那裏! – iBoston 2013-04-09 13:43:07
@ user2250168:歡迎..,接受這個答案,如果它可以幫助你... – 2013-04-09 13:45:50