BroadcastReceiver 中的 EditText Android
EditText in BroadcastReceiver Android
我正在尝试创建自定义通知。我的 XML 文件中有两个 EditText 属性。我无法理解如何将 EditText 的值从 ReminderFragment.java 传递到 AlertReceiver.java 或者更确切地说,我可以在 AlertReceiver 本身中声明 EditText 吗?
ReminderFragment.java
声明
eText = (EditText) findViewById(R.id.edittext);
findViewById(R.id.btnSetReminder).setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
String str = eText.getText().toString();
//how to return the string to createNotification method in AlertReceiver.java
setAlarm();
}
});
单击按钮设置提醒时调用的方法
public void setAlarm() {
calcal = new GregorianCalendar();
calcal.set(pYear, pMonth, pDay, pHour, pMinute);
Intent alertIntent = new Intent(ReminderFragment.this, AlertReceiver.class);
AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC_WAKEUP, calcal.getTimeInMillis(),
PendingIntent.getBroadcast(ReminderFragment.this, 1, alertIntent, PendingIntent.FLAG_UPDATE_CURRENT));
}
和AlertReceiver.java
public class AlertReceiver extends BroadcastReceiver {
public AlertReceiver() {
}
@Override
public void onReceive(Context context, Intent intent) {
createNotification(context, "Good morning",
"You have a meeting with Mr. C today!", "Alert");
//this is where the custom text must appear
}
public void createNotification(Context context, String s, String s1, String alert) {
PendingIntent notificIntent = PendingIntent.getActivity(context, 0,
new Intent(context, ReminderFragment.class), 0);
NotificationCompat.Builder nBuilder = (NotificationCompat.Builder) new NotificationCompat.Builder(context)
.setSmallIcon(R.drawable.icon)
.setContentTitle(s)
.setTicker(alert)
.setContentText(s1);
nBuilder.setContentIntent(notificIntent);
nBuilder.setDefaults(NotificationCompat.DEFAULT_SOUND);
nBuilder.setAutoCancel(true);
NotificationManager mNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(1, nBuilder.build());
}
}
您可以在 Intent
中放置一个 extra
(或多个):
在 setAlarm()
中只需添加
alertIntent.putExtra(<key>, <string>);
用您喜欢的任何字符串替换<key>
,例如"text"
或只是 "key"
和 <string>
以及您要发送到 AlarmReceiver
.
的字符串
在 AlarmReceiver
中,您可以使用
获取 onReceive
中的字符串
String text = intent.getExtras().getString(<key>);
<key>
当然必须和你在putExtra()
中使用的完全一样,否则是行不通的。
如果你愿意,你甚至可以用多个不同的键放置多个 Extras。
我将告诉您广播接收器的工作原理。
假设您已在清单中正确注册它,您将发送一条 'broadcast' 消息 (duh),就像蜂窝塔一样。
你的接收者应该'catch'广播消息。您在该广播消息中传递数据的方式是传递附加信息。
放置附加消息的一般方法是放置 'extras'
你可以通过添加:
alertIntent.putExtra("key", "value");
键和值有多种不同的数据类型可供选择,例如字符串、数组、布尔值等
我正在尝试创建自定义通知。我的 XML 文件中有两个 EditText 属性。我无法理解如何将 EditText 的值从 ReminderFragment.java 传递到 AlertReceiver.java 或者更确切地说,我可以在 AlertReceiver 本身中声明 EditText 吗?
ReminderFragment.java
声明
eText = (EditText) findViewById(R.id.edittext);
findViewById(R.id.btnSetReminder).setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
String str = eText.getText().toString();
//how to return the string to createNotification method in AlertReceiver.java
setAlarm();
}
});
单击按钮设置提醒时调用的方法
public void setAlarm() {
calcal = new GregorianCalendar();
calcal.set(pYear, pMonth, pDay, pHour, pMinute);
Intent alertIntent = new Intent(ReminderFragment.this, AlertReceiver.class);
AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC_WAKEUP, calcal.getTimeInMillis(),
PendingIntent.getBroadcast(ReminderFragment.this, 1, alertIntent, PendingIntent.FLAG_UPDATE_CURRENT));
}
和AlertReceiver.java
public class AlertReceiver extends BroadcastReceiver {
public AlertReceiver() {
}
@Override
public void onReceive(Context context, Intent intent) {
createNotification(context, "Good morning",
"You have a meeting with Mr. C today!", "Alert");
//this is where the custom text must appear
}
public void createNotification(Context context, String s, String s1, String alert) {
PendingIntent notificIntent = PendingIntent.getActivity(context, 0,
new Intent(context, ReminderFragment.class), 0);
NotificationCompat.Builder nBuilder = (NotificationCompat.Builder) new NotificationCompat.Builder(context)
.setSmallIcon(R.drawable.icon)
.setContentTitle(s)
.setTicker(alert)
.setContentText(s1);
nBuilder.setContentIntent(notificIntent);
nBuilder.setDefaults(NotificationCompat.DEFAULT_SOUND);
nBuilder.setAutoCancel(true);
NotificationManager mNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(1, nBuilder.build());
}
}
您可以在 Intent
中放置一个 extra
(或多个):
在 setAlarm()
中只需添加
alertIntent.putExtra(<key>, <string>);
用您喜欢的任何字符串替换<key>
,例如"text"
或只是 "key"
和 <string>
以及您要发送到 AlarmReceiver
.
在 AlarmReceiver
中,您可以使用
onReceive
中的字符串
String text = intent.getExtras().getString(<key>);
<key>
当然必须和你在putExtra()
中使用的完全一样,否则是行不通的。
如果你愿意,你甚至可以用多个不同的键放置多个 Extras。
我将告诉您广播接收器的工作原理。
假设您已在清单中正确注册它,您将发送一条 'broadcast' 消息 (duh),就像蜂窝塔一样。
你的接收者应该'catch'广播消息。您在该广播消息中传递数据的方式是传递附加信息。
放置附加消息的一般方法是放置 'extras' 你可以通过添加:
alertIntent.putExtra("key", "value");
键和值有多种不同的数据类型可供选择,例如字符串、数组、布尔值等