我创建了一个带有警报管理器和广播接收器的应用程序,我想将字符串从主要活动传递到公共类 MyScheduleReceiver 扩展 BroadcastReceiver(我在里面有警报管理器),然后到公共类 MyStartServiceReceiver 扩展 BroadcastReceiver 以将数据传递给静态函数......在主要活动使用共享首选项从编辑框中保存此变量。但我不能在接收器内使用共享首选项。我该怎么做?
public class MyScheduleReceiver extends BroadcastReceiver {
public void onReceive(Context context, Intent intent) {
int theHour = intent.getIntExtra("AlarmHour", 0);
int theMinute= intent.getIntExtra("AlarmMin", 0);
AlarmManager service = (AlarmManager) context
.getSystemService(Context.ALARM_SERVICE);
Intent i = new Intent(context, MyStartServiceReceiver.class);
PendingIntent pending = PendingIntent.getBroadcast(context, 0, i,
PendingIntent.FLAG_CANCEL_CURRENT);
i.putExtra("data","1");
Calendar cal = Calendar.getInstance();
cal.setTimeInMillis(System.currentTimeMillis());
cal.set(Calendar.HOUR_OF_DAY, theHour); // set user selection
cal.set(Calendar.MINUTE, theMinute);
cal.set(Calendar.SECOND, 0);
service.set(AlarmManager.RTC_WAKEUP,
cal.getTimeInMillis(), pending);
}
public class MyStartServiceReceiver extends BroadcastReceiver {
public void onReceive(Context context, Intent intent) {
Intent service = new Intent(context, AlarmService.class);
context.startService(service);
String result = intent.getStringExtra("data");
String msg=result;
try {
ArduBtTimer.sendData(msg);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}