简体   繁体   English

如何将数据从活动中的功能传递到通知接收器广播接收器?

[英]How to pass data from function in activity to a Notifcation Receiver Broadcast Receiver?

How do you correctly pass data from one activity to another which extends BroadcastReceiver? 您如何正确地将数据从一个活动传递到另一个扩展了BroadcastReceiver的活动? I'm trying to pass two strings from a callNotification function to a NotificationReceiver activity. 我试图将两个字符串从callNotification函数传递到NotificationReceiver活动。 I call my function 10 minutes before a class starts to let a user know which lecture they have and in which lecture theatre. 我会在上课开始前10分钟致电我的函数,让用户知道他们参加了哪一场讲座以及在哪座演讲厅中。 This is what I place to transfer the two strings in my function: 这是我在函数中传输两个字符串的位置:

Intent intentNotif = new Intent(this, NotificationReceiver.class);
    intentNotif.putExtra("MODULE", module10 );
    intentNotif.putExtra("LOCATION", location10 );
    startActivity(intentNotif);

However, in my NotificationReceiver activity, I'm told that the method getIntent cannot be resolved. 但是,在我的NotificationReceiver活动中,我被告知无法解析方法getIntent。 Am I calling the intent properly in my activity? 我是否在活动中正确表达了意图?

Intent intentNotif = getIntent();
    String s = getIntent().getStringExtra("MODULE");
    String t = getIntent().getStringExtra("LOCATION");

This is my function: 这是我的功能:

 public void callNotification(int hour, int min, int sec, String module10, String location10 ){
    //new NotificationReceiver(id);

    Calendar calendar = Calendar.getInstance();
    calendar.set(Calendar.HOUR_OF_DAY, hour);
    calendar.set(Calendar.MINUTE, min);
    calendar.set(Calendar.SECOND, sec);

    //String extra = module + " - " + location;
    Intent intentNotif = new Intent(this, NotificationReceiver.class);
    intentNotif.putExtra("MODULE", module10 );
    intentNotif.putExtra("LOCATION", location10 );
    startActivity(intentNotif);
    PendingIntent pendingIntent = PendingIntent.getBroadcast(getApplicationContext(),100,intentNotif,PendingIntent.FLAG_UPDATE_CURRENT);
    AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);

    //RTC_WAKEUP makes sure that the alarm will be triggered even if the device goes into sleep mode
    //calendar.getTimeInMillis() will be the time that we want the alarm to go off at
    //alarmManager.INTERVAL_DAY is the interval, how often it should get called, INTERVAL_DAY makes it show up on a daily basis
    alarmManager.setRepeating(AlarmManager.RTC_WAKEUP,calendar.getTimeInMillis(),alarmManager.INTERVAL_DAY,pendingIntent);
}

This is my NotificationReceiver Activity: 这是我的NotificationReceiver活动:

public class NotificationReceiver extends BroadcastReceiver {
//int notify_id = 100;
//MainActivity obj = new MainActivity();
//int id = obj.notify_id;

//int id = (int)System.currentTimeMillis();

@Override
public void onReceive(Context context, Intent intent) {     //onReceive will always get called when the class ges triggered
    NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE); // provides an builder interface to create an Notification object.
    Intent intent1 = new Intent(context, MainActivity.class);    //When the notification is pressed, it will open ReceivingNotification
    intent1.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);   //FLAG..TOP means that if MainActivity is already open when the notification is pressed, it will close it and freshly open it again
    //String text_notif = intent.getStringExtra(Monday.MONDAY_KEY);
    //if we want ring on notifcation then uncomment below line//
    //Uri alarmSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
    //Pending intent specifies action performed when user clicks notifcation

    Intent intentNotif = getIntent();
    String s = getIntent().getStringExtra("MODULE");
    String t = getIntent().getStringExtra("LOCATION");
    PendingIntent pendingIntent = PendingIntent.getActivity(context,100,intent1,PendingIntent.FLAG_UPDATE_CURRENT);
    NotificationCompat.Builder builder = new NotificationCompat.Builder(context).

            setContentIntent(pendingIntent).
            setSmallIcon(R.drawable.ic_launcher).
            setContentText("You have Module"+s+"in Theatre"+t).
            setContentTitle("My notificaton - id " + String.valueOf(100)).
            addAction(R.drawable.ic_launcher, "Next module", null).
            //setSound(alarmSound).
                    setAutoCancel(true);    //This makes the notification dismissable when the user hits it away


    notificationManager.notify(100,builder.build());

    //notify_id++;

}

You already receive an intent in your onReceive method, you do not need to call getIntent() . 您已经在onReceive方法中收到了一个intent ,您不需要调用getIntent()

Instead of: 代替:

Intent intentNotif = getIntent();
String s = getIntent().getStringExtra("MODULE");
String t = getIntent().getStringExtra("LOCATION");

Try with: 尝试:

String s = intent.getStringExtra("MODULE");
String t = intent.getStringExtra("LOCATION");

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

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