1

我有 3 个活动,第 1 幕、第 2 幕和第 3 幕。在我的 Activity 1 中,用户设置标题、他想要的事件的描述、通知用户的日期和时间。当用户单击按钮 DONE 时,通知 ID 将传递给我的 Activity 2,以显示通知。当它需要通知时,它现在会显示 Activity,其中显示了用户在 Activity 1 中创建的事件的详细信息。我的问题是如何查看已传递给 Activity 3 的数据?

这是我正在处理的代码:

活动一:

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

活动二:

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

但我似乎无法显示详细信息。任何帮助都非常感谢。谢谢。

4

1 回答 1

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

我希望这有帮助。

于 2012-10-27T15:59:53.070 回答