如何将数据从 activity 中的函数传递到 Notifcation Receiver 广播接收器?
How to pass data from function in activity to a Notifcation Receiver Broadcast Receiver?
如何正确地将数据从一个 activity 传递到另一个扩展 BroadcastReceiver 的数据?
我正在尝试将两个字符串从 callNotification 函数传递到 NotificationReceiver activity。我在 class 开始前 10 分钟调用我的函数,让用户知道他们有哪个讲座以及在哪个演讲厅。
这是我在函数中传输两个字符串的位置:
Intent intentNotif = new Intent(this, NotificationReceiver.class);
intentNotif.putExtra("MODULE", module10 );
intentNotif.putExtra("LOCATION", location10 );
startActivity(intentNotif);
但是,在我的 NotificationReceiver activity 中,我被告知无法解析方法 getIntent。我是否在 activity 中正确调用了意图?
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 Activity:
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");
如何正确地将数据从一个 activity 传递到另一个扩展 BroadcastReceiver 的数据? 我正在尝试将两个字符串从 callNotification 函数传递到 NotificationReceiver activity。我在 class 开始前 10 分钟调用我的函数,让用户知道他们有哪个讲座以及在哪个演讲厅。 这是我在函数中传输两个字符串的位置:
Intent intentNotif = new Intent(this, NotificationReceiver.class);
intentNotif.putExtra("MODULE", module10 );
intentNotif.putExtra("LOCATION", location10 );
startActivity(intentNotif);
但是,在我的 NotificationReceiver activity 中,我被告知无法解析方法 getIntent。我是否在 activity 中正确调用了意图?
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 Activity:
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");