Dunkey
Dunkey

Reputation: 1922

Returning data to other activity with notification

I have 3 activities, Act 1, Act 2, and Act 3. In my Activity 1, the user sets the title, description of his desired event, the date and the time it should notify the user. When the user clicks on the button DONE, the notification ID is passed to my Activity 2, to display the notification. When the time comes for it to notify, it then now displays the Activity which shows the details of the event the user created in the Activity 1. My problem is how to view the data that have been passed to Activity 3?

This is the code I'm working on:

Activity 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();
        }
    }); 

Activity 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();
}

and Activity 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 );
    }
}

In the Activity 3, I put this

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

But I can't seem to display the details. Any help is truly appreciated. THanks.

Upvotes: 1

Views: 468

Answers (1)

MarchingHome
MarchingHome

Reputation: 1204

Maybe not the best solution, but you can use the DefaultSharedPreferences to save the values.

Save the values in Activity 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();

Do something with the Strings from the events in Activity 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
    }
}

When deleting an event in Activity 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();

I hope this helps.

Upvotes: 2

Related Questions