2011-02-26 21 views
1

我使用一個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); 
}} 

你知道爲什麼它不能像這樣工作嗎?我得到的輸出只是空的,沒有錯誤。

+0

您是否嘗試過打印'n_message'和'n_title'到日誌,以查看它們是否包含你的信息,他們應該的事情嗎?一眼就可以看出你在'notification.setLatestEventInfo()'中正確設置了它們。 –

+0

是的,我已經嘗試過,但輸出只是空的!在通知中,我只能看到我在n_title和n_message之間設置的「 - 」。 – CTrox

回答

1

我使用一個BroadcastReceiver在給定的時間收到通知。

那句話沒有任何意義。 BroadcastReceivers根本不「收到通知」。

現在我試圖從一個EditText的輸入到該狀態欄通知。

你的EditText需要在一個活動。你沒有活動。

我現在用一個LayoutInflater試過了,但我無法讓它工作。

首先,BroadcastReceiver沒有用戶界面。

其次,只是因爲你膨脹的佈局與LayoutInflater,這並不意味着,佈局在屏幕上實際上出現。爲此,你需要一個活動。

+0

1.是的,我寫了一個錯誤。我使用AlarmManager在給定的時間顯示通知。 2.我的EditText處於活動。但我想從用戶那裏得到來自該活動的輸入到我的Notification中,這是由BroadcastReceiver觸發的。我知道,我只使用BroadcastReceiver來觸發通知。 – CTrox

+0

@cTrox:讓活動顯示「通知」。或者,讓活動將文本保存到數據庫或文件,並讓「BroadcastReceiver」啓動一個「IntentService」,它將讀取數據庫或文件並顯示「Notification」。 – CommonsWare

+0

好的,謝謝。我會稍後再嘗試。 – CTrox