如何正确地将数据从一个活动传递到另一个扩展BroadcastReceiver的活动? 我正在尝试将两个字符串从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++;
}
答案 0 :(得分:0)
您已在intent
方法中收到onReceive
,无需致电getIntent()
。
而不是:
Intent intentNotif = getIntent();
String s = getIntent().getStringExtra("MODULE");
String t = getIntent().getStringExtra("LOCATION");
尝试:
String s = intent.getStringExtra("MODULE");
String t = intent.getStringExtra("LOCATION");