gpt4 book ai didi

android - 通过通知将数据返回到其他 Activity

转载 作者:行者123 更新时间:2023-11-29 21:56:28 25 4
gpt4 key购买 nike

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

这是我正在处理的代码:

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

和 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 );
}
}

在 Activity 三中,我把这个

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

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

最佳答案

也许不是最好的解决方案,但您可以使用 DefaultSharedPreferences 来保存这些值。

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

对来自 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
}
}

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

希望对您有所帮助。

关于android - 通过通知将数据返回到其他 Activity ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13101364/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com