Java Android中的EditText
我正在尝试创建自定义通知。我的XML文件中有两个EditText属性。我无法理解如何将EditText的值从rementerFragment.java传递到AlertReceiver.java,或者更确切地说,我可以在AlertReceiver中声明EditText吗Java Android中的EditText,java,android,android-edittext,android-notifications,android-broadcast,Java,Android,Android Edittext,Android Notifications,Android Broadcast,我正在尝试创建自定义通知。我的XML文件中有两个EditText属性。我无法理解如何将EditText的值从rementerFragment.java传递到AlertReceiver.java,或者更确切地说,我可以在AlertReceiver中声明EditText吗 rementerfragment.java public class AlertReceiver extends BroadcastReceiver { public AlertReceiver() { }
rementerfragment.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());
}
}
声明
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());
}
}
您可以在您的
意图中添加一个额外的(或多个)
:
在setAlarm()
中,只需添加
alertIntent.putExtra(<key>, <string>);
当然必须与您在putExtra()
中使用的完全相同,否则它将无法工作。
如果您愿意,您甚至可以使用多个不同的键放置多个附加功能。我将告诉您广播接收器的工作原理
假设您已经在清单中正确地注册了它,您将发送一条“广播”消息(duh),很像蜂窝塔
你的接收器应该“捕捉”广播信息。在该广播消息中传递数据的方式是通过传递额外数据
添加额外信息的一般方法是添加“额外信息”
您可以通过添加以下内容来实现:
alertIntent.putExtra("key", "value");
键和值有许多不同的数据类型可供选择,如字符串、数组、布尔值等