gpt4 book ai didi

android - 通知中的字符串未更新

转载 作者:太空狗 更新时间:2023-10-29 14:53:16 24 4
gpt4 key购买 nike

应用程序应该做的是从 editReminder EditText 中获取一个字符串,然后使用一个 intent 将它发送到 AlarmManager。这么好用,第一次。但是当您关闭应用程序并再次尝试时,通知不会使用您刚刚在 EditText 中输入的字符串,而是使用您第一次运行该应用程序时输入的文本。

我们如何让新插入的文本而不是旧文本出现在通知中?

MainActivity:按钮点击时执行的方法

public void setAlarm(View v) {
//get user input
EditText editText = (EditText) findViewById(R.id.editReminder);
String reminder = editText.getText().toString();
String snoozeString = getString(R.string.snooze_result);


//the AlarmManager
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
//get date and time
Calendar c = Calendar.getInstance();

//sets time for alarm
c.set(Calendar.YEAR, year);
c.set(Calendar.MONTH, month);
c.set(Calendar.DAY_OF_MONTH, day);
c.set(Calendar.HOUR_OF_DAY, hour);
c.set(Calendar.MINUTE, minute);
c.set(Calendar.SECOND, 0);

//pIntent to launch activity when alarm triggers
Intent intent = new Intent("com.garden.DisplayNotification"); //(1)From here to DisplayNotification ...
//DisplayNotification is the activity that is intended to be evoked
//sometimes you have to use (this,DisplayNotification.class)?
//when the alarm is triggered

//assign an ID of 1, and add the text
intent.putExtra("NotifID", 1);
intent.putExtra("notification", reminder); //("STRING_I_NEED",strname)
intent.putExtra("notifyAction", snoozeString); //string for the action button
//set the flags so the mainactivity isn't started when the notification is triggered
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);


PendingIntent displayIntent = PendingIntent.getActivity(
getBaseContext(), 0,
intent, 0); //this intent instead of new Intent("com.garden.Reminder")


//sets alarm
alarmManager.set(AlarmManager.RTC_WAKEUP,
c.getTimeInMillis(), displayIntent);

showConfirmDialog(v);

}

DisplayNotification:当通知的警报触发时执行

package com.garden.gardenapp;

import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.os.Bundle;

public class DisplayNotification extends Activity {
/**
* This activity is to display a notification only. It is called when
* the system alarm goes off.
*/

//called when activity is first created
//don't forget to update the manifest!!
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);


//get notification ID passed by MainActivity
int notifID = getIntent().getExtras().getInt("NotifID");
//----Oh of course, have to pass on the strings again.....-----
//initialize strings (reminder is also used for notification text
String reminderText = getIntent().getStringExtra("notification");
String snoozeString = getIntent().getStringExtra("notifyAction");


//pIntent to launch activity if user selects notification
/** making a new Intent has to be done with (this, Reminder.class) instead
* of ("com.garden.Reminder")... why? (otherwise the reminder text is not shown)
*/
Intent reminderIntent = new Intent(this, Reminder.class); //(2)... and from here to Reminder ...
reminderIntent.putExtra("NotifID", notifID);
//pass on strings again in the intent
reminderIntent.putExtra("notification", reminderText);
//intent.putExtra("notifyAction",snoozeString); //---> in different intent


PendingIntent reminderPIntent = PendingIntent
.getActivity(this, 0, reminderIntent, 0);

Intent actionIntent = new Intent(this, Reminder.class);
actionIntent.putExtra("notifyAction", snoozeString); //("STRING_I_NEED", strName)
//don't forget to pass the notifID also to the second intent
actionIntent.putExtra("NotifID",notifID);
PendingIntent actionPIntent = PendingIntent.getActivity(this,
(int) System.currentTimeMillis(), actionIntent, 0);


//create notification
Notification notif = new Notification.Builder(this) //build the notification
.setContentTitle(getString(R.string.app_name)) //required
.setContentText(reminderText) //required
.setSmallIcon(R.drawable.garden) //required
.setContentIntent(reminderPIntent)
//associate pendingIntent with a gesture of NotificationCompat.Builder: click
.addAction(R.drawable.pixel, "Snooze me", actionPIntent)
//should be addAction(NotificationCompat.Action action)
.setAutoCancel(true) //to be dismissed in the Reminder activity
.setPriority(Notification.PRIORITY_MAX) //to show the action buttons by default
// .setVibrate(new long[] {200, 600, 200, 600})
.build();

NotificationManager nm = (NotificationManager)
getSystemService(NOTIFICATION_SERVICE);

nm.notify(notifID, notif); //(int id, Notification notification);

finish(); //because this doesn't have a GUI we don't need it anymore

}
}

(在 DisplayNotification 类中,我们将字符串分成两个 Intent ,因此当您单击暂停(通知操作)按钮时,它会触发与您单击通知时不同的 Intent )

我们认为这与 AlarmManager 没有更新 Intent 附带的字符串有关。因为当我们在没有 AlarmManager 的情况下发出通知时,应用程序运行得非常好。如果您需要其他代码,请告诉我。

最佳答案

我们找到了!你不需要取消警报管理器,但你需要给 Intent 一个标志 FLAG_UPDATE_CURRENT让警报管理器使用您放入 Intent 中的新字符串更新 Intent 。所以在这种情况下,PendingIntent displayIntent 变成了

 PendingIntent displayIntent = PendingIntent.getActivity(
getBaseContext(), 0,
intent, PendingIntent.FLAG_UPDATE_CURRENT);

这就解决了更新字符串的问题。

关于android - 通知中的字符串未更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33605730/

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