我如何将字符串从主要活动传递到带有警报管理器的广播接收器?
Posted
技术标签:
【中文标题】我如何将字符串从主要活动传递到带有警报管理器的广播接收器?【英文标题】:How i pass string from main activity to broadcast receiver with alarm manager? 【发布时间】:2013-01-30 11:21:23 【问题描述】:我创建了一个带有警报管理器和广播接收器的应用程序,我想将字符串从主要活动传递到公共类 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();
【问题讨论】:
你必须在待处理的意图中添加字符串值并从接收时获取该值 我该怎么做?一些代码? 你已经完成了意图并在接收器中接收 i.putExtra("data", "1"); 在你将它传递给 PendingIntent 之前,我需要将附加内容添加到 Intent 中 【参考方案1】:试试这个:
where u want to set value:
i.putExtra("data",yourstring);
and for recieve:
String value = intent.getStringExtra("data");
【讨论】:
【参考方案2】:我认为您应该使用 BroadCastReceiver 发送要保存的信息并使用 SP 保存 在那里。
这是一个从接收者向活动发送信息的示例:
在您想要获取数据的活动中创建一个扩展 BroadcastReceiver
的内部类:
private BroadcastReceiver ReceivefromService = new BroadcastReceiver()
@Override
public void onReceive(Context context, Intent intent)
//get the data using the keys you entered at the service
String IncomingSms=intent.getStringExtra("incomingSms");//
String phoneNumber=intent.getStringExtra("incomingPhoneNumber");
;
将此添加到onPause()
:
@Override
protected void onPause()
super.onPause();
try
unregisterReceiver(ReceivefromService);
catch (IllegalArgumentException e)
if (e.getMessage().contains("Receiver not registered"))
Log.i("TAG","Tried to unregister the reciver when it's not registered");
else
throw e;
将此添加到onResume()
:
protected void onResume()
super.onResume();
filter.addAction("android.intent.action.SmsReceiver");
registerReceiver(ReceivefromService, filter);
//the first parameter is the name of the inner class we created.
在接收器/服务中创建一个意图,像这样开始广播:
Intent i = new Intent("android.intent.action.SmsReceiver").putExtra("incomingSms", message);
i.putExtra("incomingPhoneNumber", phoneNumber);
context.sendBroadcast(i);
希望对你有所帮助...
【讨论】:
以上是关于我如何将字符串从主要活动传递到带有警报管理器的广播接收器?的主要内容,如果未能解决你的问题,请参考以下文章
MainActivity 中的 Android 类没有收到来自警报管理器的广播