简体   繁体   English

Android BroadcastReceiver中的EditText

[英]EditText in BroadcastReceiver Android

I am trying to create custom notifications. 我正在尝试创建自定义通知。 I have two EditText attributes in my XML file. 我的XML文件中有两个EditText属性。 I'm unable to understand how to pass the value of EditText from ReminderFragment.java to AlertReceiver.java or rather, can I declare EditText in AlertReceiver itself? 我无法理解如何将EditText的值从ReminderFragment.java传递到AlertReceiver.java,还是可以在AlertReceiver本身中声明EditText?

ReminderFragment.java

Declaration 宣言

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();
        }
});

Method called when Button Set Reminder is clicked 单击“按钮设置提醒”时调用的方法

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));
}

and AlertReceiver.java 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());
    }
}

You can put an extra (or multiple) in your Intent : in setAlarm() simply add 你可以把一个extra (或多个)在你的Intent :在setAlarm()只需添加

alertIntent.putExtra(<key>, <string>);

replace <key> with any string you like, eg "text" or just "key" and <string> with the string you want to send to the AlarmReceiver . <key>替换为您喜欢的任何字符串,例如"text"或仅将"key"替换为<string>并将<string>替换为您要发送给AlarmReceiver的字符串。

In AlarmReceiver you can then get the string in onReceive using AlarmReceiver您可以使用以下命令在onReceive获取字符串

String text = intent.getExtras().getString(<key>);

<key> of course has to be the exact same you used in putExtra() , otherwise it won't work. <key>当然必须与在putExtra()使用的putExtra() ,否则它将无法工作。 You can even put multiple Extras with multiple different keys if you like. 如果愿意,您甚至可以使用多个不同的键放置多个Extras。

I will tell you how a broadcast receiver works. 我将告诉您广播接收机的工作方式。

Assuming you have registered it properly in manifest, you send a 'broadcast' message (duh), much like a cellular tower. 假设您已在清单中正确注册它,则发送“广播”消息(duh),就像蜂窝塔一样。

And your receiver is supposed to 'catch' that broadcast message. 并且您的接收器应该“捕获”该广播消息。 The way you pass data in that broadcast message is by passing extras. 在该广播消息中传递数据的方式是传递附加信息。

The general way to put an additional message is by putting 'extras' you can do that by adding: 放置附加消息的一般方法是添加“ extras”,您可以添加以下内容:

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

there are many different data types to choose from for key and value, like strings, arrays , booleans , etc 有许多不同的数据类型可用于键和值,例如字符串,数组,布尔值等

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM