简体   繁体   中英

EditText in BroadcastReceiver Android

I am trying to create custom notifications. I have two EditText attributes in my XML file. 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?

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

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

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 .

In AlarmReceiver you can then get the string in onReceive using

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

<key> of course has to be the exact same you used in putExtra() , otherwise it won't work. You can even put multiple Extras with multiple different keys if you like.

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.

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:

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

there are many different data types to choose from for key and value, like strings, arrays , booleans , etc

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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