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

Java 如何将数据从活动中的函数传递到通知接收器广播接收器?,java,android,android-studio,android-intent,broadcastreceiver,Java,Android,Android Studio,Android Intent,Broadcastreceiver,如何正确地将数据从一个活动传递到另一个活动,从而扩展广播接收器? 我试图将两个字符串从callNotification函数传递到NotificationReceiver活动。在课程开始前10分钟,我调用我的函数,让用户知道他们有哪些讲座,在哪个演讲厅。 这是我在函数中传输两个字符串的位置: Intent intentNotif = new Intent(this, NotificationReceiver.class); intentNotif.putExtra("MODULE", mo

如何正确地将数据从一个活动传递到另一个活动,从而扩展广播接收器? 我试图将两个字符串从callNotification函数传递到NotificationReceiver活动。在课程开始前10分钟,我调用我的函数,让用户知道他们有哪些讲座,在哪个演讲厅。 这是我在函数中传输两个字符串的位置:

Intent intentNotif = new Intent(this, NotificationReceiver.class);
    intentNotif.putExtra("MODULE", module10 );
    intentNotif.putExtra("LOCATION", location10 );
    startActivity(intentNotif);
但是,在我的NotificationReceiver活动中,我被告知无法解析getIntent方法。我在活动中是否正确地调用了意图

Intent intentNotif = getIntent();
    String s = getIntent().getStringExtra("MODULE");
    String t = getIntent().getStringExtra("LOCATION");
这是我的职责:

 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);
}
这是我的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++;

}

您已经在
onReceive
方法中接收到
intent
,您不需要调用
getIntent()

而不是:

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

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