我正在尝试制作应用detect when a new SMS is received
为此,我注册programatically a new BroadcastReceiver
,我处理传入的短信,然后它应返回一个值,注册 BroadcastReceiver的片段应该检索。登记/>
I can't get to retrieve the value returned from the BroadcastReceiver
。我已经看到有关将值返回到Activities的其他StackOverflow问题,但它们总是最终创建一个新的Activity。我不希望这样。的 I want to get the value in the same place where I registered the BroadcastReceiver
即可。我该怎么做?
这是BroadcastReceiver的代码:
public class RegisterSms extends BroadcastReceiver {
public void onReceive(Context context, Intent intent) {
// stuff...
this.setResultData("data to return");
}
}
在片段中我正在这样做:
BroadcastReceiver smsReceiver = new RegisterSms();
IntentFilter filter = new IntentFilter("android.provider.Telephony.SMS_RECEIVED");
this.getContext().registerReceiver(smsReceiver, filter);
// here I should be receiving the data sent by the BroadcastReceiver
答案 0 :(得分:2)
将您的广播接收器设置为匿名内部类。
private BroadcastReceiver smsReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
//here you can parse intent and get sms fields.
}
};
IntentFilter filter = new IntentFilter("android.provider.Telephony.SMS_RECEIVED");
this.getContext().registerReceiver(smsReceiver, filter);