gpt4 book ai didi

java - Android 广播接收器中的 EditText

转载 作者:行者123 更新时间:2023-12-02 04:05:42 25 4
gpt4 key购买 nike

我正在尝试创建自定义通知。我的 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());
}
}

最佳答案

我将告诉您广播接收器的工作原理。

假设您已在 list 中正确注册它,您会发送一条“广播”消息(废话),就像蜂窝塔一样。

您的接收器应该“捕获”该广播消息。在广播消息中传递数据的方式是传递额外内容。

添加附加消息的一般方法是添加“extras”您可以通过添加以下内容来做到这一点:

alertIntent.putExtra("key", "value");

键和值有许多不同的数据类型可供选择,例如字符串、数组、 boolean 值等

关于java - Android 广播接收器中的 EditText,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34299834/

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