我使用一個BroadcastReceiver在給定的時間收到通知。現在我試圖從EditText獲取輸入到狀態欄通知中。我現在用一個LayoutInflater嘗試了它,但我無法使它工作。的EditText在一個BroadcastReceiver
public class AlarmBroadcastReceiver extends BroadcastReceiver {
public void onReceive(Context context, Intent intent) {
notificationStatus(context);
}
private void notificationStatus(Context context) {
final NotificationManager mNotificationManager = (NotificationManager) context
.getSystemService(Context.NOTIFICATION_SERVICE);
LayoutInflater mInflater = LayoutInflater.from(context);
View myView = mInflater.inflate(R.layout.main, null);
EditText etxt_title = (EditText) myView.findViewById(R.id.etxt_title);
String n_title = etxt_title.getText().toString();
EditText etxt_message = (EditText) myView.findViewById(R.id.etxt_message);
String n_message = etxt_message.getText().toString();
final long when = System.currentTimeMillis();
final int icon = R.drawable.notification_icon;
final Notification notification = new Notification(icon, n_title + " - " + n_message, when);
final Intent notificationIntent = new Intent(context
.getApplicationContext(), Main.class);
final PendingIntent contentIntent = PendingIntent.getActivity(context
.getApplicationContext(), 0, notificationIntent, 0);
notification.defaults |= Notification.DEFAULT_SOUND;
notification.defaults |= Notification.DEFAULT_LIGHTS;
notification.setLatestEventInfo(context, n_title, n_message,
contentIntent);
mNotificationManager.notify(1, notification);
}}
你知道爲什麼它不能像這樣工作嗎?我得到的輸出只是空的,沒有錯誤。
您是否嘗試過打印'n_message'和'n_title'到日誌,以查看它們是否包含你的信息,他們應該的事情嗎?一眼就可以看出你在'notification.setLatestEventInfo()'中正確設置了它們。 –
是的,我已經嘗試過,但輸出只是空的!在通知中,我只能看到我在n_title和n_message之間設置的「 - 」。 – CTrox