应用程序应该做的是从editReminder EditText
获取一个String并使用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类中,我们将字符串拆分为两个意图,因此当您单击“暂停(通知操作)”按钮时,它会触发与单击通知时不同的意图)
我们认为它与AlarmManager有关,不会更新意图或附带的字符串。因为当我们在没有AlarmManager的情况下发出通知时,应用程序的工作完全正常。 如果您需要其他代码,请与我们联系。
答案 0 :(得分:0)
我们找到了!您不需要取消警报管理器,但是您需要为intent提供一个标志FLAG_UPDATE_CURRENT
,让警报管理器使用您放入intent中的新字符串更新intent。
所以在这种情况下,PendingIntent displayIntent
变为
PendingIntent displayIntent = PendingIntent.getActivity(
getBaseContext(), 0,
intent, PendingIntent.FLAG_UPDATE_CURRENT);
这解决了更新字符串的问题。