1

我创建了一个带有警报管理器和广播接收器的应用程序,我想将字符串从主要活动传递到公共类 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();
}
4

2 回答 2

0

尝试这个:

 where u want to set value:
 i.putExtra("data",yourstring); 
 and for recieve: 
 String value = intent.getStringExtra("data");
于 2013-01-30T12:14:26.340 回答
0

我认为您应该使用 BroadCastReceiver 发送要保存的信息,然后用 SP 将其保存在那里。

这是从接收者向活动发送信息的示例:

  1. 创建一个内部类,该类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");
    
        }
    };
    
  2. 将此添加到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;
            }
        }
    }
    
  3. 将此添加到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.
    }
    
  4. 在接收器/服务中创建一个意图,像这样开始广播:

    Intent i = new Intent("android.intent.action.SmsReceiver").putExtra("incomingSms", message);
    i.putExtra("incomingPhoneNumber", phoneNumber);
    context.sendBroadcast(i);
    

希望它会帮助你...

于 2014-03-09T21:04:10.860 回答