2012-10-27 23 views
1

我有3項活動,第1項,第2項和第3項。在我的活動1中,用戶設置標題,所需活動的描述,日期和時間用戶。當用戶點擊DONE按鈕時,通知ID被傳遞給我的活動2,以顯示通知。當需要通知時,它現在顯示活動,它顯示用戶在活動1中創建的事件的詳細信息。我的問題是如何查看已傳遞到活動3的數據?通過通知返回數據到其他活動

這是我工作的代碼:

活動1:

//---Button view--- 
    Button btnOpen = (Button) findViewById(R.id.button1); 
    btnOpen.setOnClickListener(new View.OnClickListener() { 

     public void onClick(View v) {     
      timePicker = (TimePicker) findViewById(R.id.timePicker1); 
      datePicker = (DatePicker) findViewById(R.id.datePicker1);  
      title = (EditText) findViewById (R.id.editText1); 
      description = (EditText) findViewById (R.id.editText2); 

      //---use the AlarmManager to trigger an alarm--- 
      AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);     

      //---get current date and time--- 
      Calendar calendar = Calendar.getInstance();  

      //---sets the time for the alarm to trigger--- 
      calendar.set(Calendar.YEAR, datePicker.getYear()); 
      calendar.set(Calendar.MONTH, datePicker.getMonth()); 
      calendar.set(Calendar.DAY_OF_MONTH, datePicker.getDayOfMonth());     
      calendar.set(Calendar.HOUR_OF_DAY, timePicker.getCurrentHour()); 
      calendar.set(Calendar.MINUTE, timePicker.getCurrentMinute()); 
      calendar.set(Calendar.SECOND, 0); 

      //---PendingIntent to launch activity when the alarm triggers---      
      Intent i = new Intent(NotifyActivity.this, DisplayNotification.class); 

      //---assign an ID of 1--- 
      i.putExtra("NotifID", 1); 

      Intent d = new Intent(NotifyActivity.this, AlarmDetails.class); 
      d.putExtra("Title", title.getText().toString()); 
      d.putExtra("Description", description.getText().toString()); 

      PendingIntent displayIntent = PendingIntent.getActivity(
       getBaseContext(), 0, i, 0);    

      //---sets the alarm to trigger--- 
      alarmManager.set(AlarmManager.RTC_WAKEUP, 
       calendar.getTimeInMillis(), displayIntent); 
      finish(); 
     } 
    }); 

活動2:

//---get the notification ID for the notification; 
// passed in by the MainActivity--- 
int notifID = getIntent().getExtras().getInt("NotifID"); 

//---PendingIntent to launch activity if the user selects 
// the notification--- 
Intent i = new Intent(DisplayNotification.this, AlarmDetails.class); 
i.putExtra("NotifID", notifID); 

PendingIntent detailsIntent = 
    PendingIntent.getActivity(this, 0, i, 0); 

NotificationManager nm = (NotificationManager) 
    getSystemService(NOTIFICATION_SERVICE); 
Notification notif = new Notification(
    R.drawable.ic_launcher, 
    "iHealthFirst: Notification!", 
    System.currentTimeMillis()); 

CharSequence from = "AlarmManager - New Notification"; 
CharSequence message = "This is your alert, click to view";   
notif.setLatestEventInfo(this, from, message, detailsIntent); 

//---100ms delay, vibrate for 250ms, pause for 100 ms and 
// then vibrate for 500ms--- 
notif.vibrate = new long[] { 100, 250, 100, 500};   
nm.notify(notifID, notif); 
//---destroy the activity--- 
finish(); 
} 

和活動3:

@Override 
public void onCreate(Bundle savedInstanceState) 
{ 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.alarmdetails); 

    //---look up the notification manager service--- 
    NotificationManager nm = (NotificationManager) 
     getSystemService(NOTIFICATION_SERVICE); 

    //---cancel the notification--- 
    nm.cancel(getIntent().getExtras().getInt("NotifID")); 

    Bundle extras = getIntent().getExtras(); 
    if(extras !=null) 
    { 
    String strTitle = extras.getString("Title"); 
    String strDescription = extras.getString("Description"); 
    title.setText(strTitle); 
    description.setText(strDescription); 
    } 
} 

在活動3,我把這個

 **Bundle extras = getIntent().getExtras(); 
    if(extras !=null) 
    { 
    String strTitle = extras.getString("Title"); 
    String strDescription = extras.getString("Description"); 
    title.setText(strTitle); 
    description.setText(strDescription); 
    }** 

但我似乎無法顯示細節。任何幫助是真正的讚賞。謝謝。

回答

2

也許不是最好的解決方案,但您可以使用DefaultSharedPreferences來保存這些值。

保存在活動3的值:

//Determine the String values 
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this); 
Int numberOfEvents = preferences.getInt("numberofevents", 0); 
numberOfEvents++; 
preferences.putString("title" + numberOfEvents, titleVariable); 
preferences.putString("description" + numberOfEvents, descriptionVariable); 
preferences.putInt("numberofevents", numberOfEvents); 
preferences.commit(); 

請與絃樂從事件東西在活動1:

SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this); 
int numberOfEvents = preferences.getInt("numberofevents", 0); 
for(int i = 0; i < numberOfEvents; i++) { 
    String titleVariable = preferences.getString("title" + i, ""); 
    String descriptionVariable = preferences.getString("description" + i, ""); 
    if(!titleVariable.equals("") && !descriptionVariable.equals("")) { 
     //use the String variables for you notification 
    } 
} 

當活動3刪除事件:

SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this); 
//get id number of event to delete and store it in variable variableNameOfEventNumber 
preferences.putString("title" + variableNameOfEventNumber, ""); 
preferences.putString("description" + variableNameOfEventNumber, ""); 
preferences.commit(); 

我希望這有助於。

+0

謝謝,但如果我要創建另一個事件呢? – Dunkey

+0

我明白你的意思了。您可以像這樣調整首選項的名稱:'preferences.putString(「title」+ i,titleVariable);',其中'i'是事件的ID。然後,您可以使用循環從共享首選項文件中讀取它們。如果你稍後要刪除一個事件,只要將'''''''''''''''''''''''''''''''''''''''''你的代碼}。 – MarchingHome

+0

你能爲我打補丁嗎?謝謝 – Dunkey