通过通知将数据返回到其他活动

时间:2012-10-27 15:09:29

标签: android android-intent android-activity

我有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 );
    }**

但我似乎无法显示细节。真的很感激任何帮助。感谢。

1 个答案:

答案 0 :(得分: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();

我希望这会有所帮助。