我有一个
BroadcastReceiver
类,它读取传入的 sms
并从中提取出一个 PIN。
我想把这个 sms
传递给一个已经对用户可见的活动。
用户应该在一个 EditText
中输入 PIN,我正在从 EditText
中获取值,将用户输入的 PIN 与由 BroadcastReceiver
传递的 PIN 进行比较。如果两者相匹配,则用户可以进入应用程序。但是我不知道如何将由 BroadcastReceiver
接收到的 PIN 传递到 Activity
。
以下是代码:BroadcastReceiver 类
public class IncomingMessage extends BroadcastReceiver {
final SmsManager sms = SmsManager.getDefault();
private OnSMSReceived onSMSReceived = null;
@Override
public void onReceive(Context context, Intent intent) {
// Retrieves a map of extended data from the intent.
final Bundle bundle = intent.getExtras();
try {
if (bundle != null) {
String senderNum = null;
String message = null;
final Object[] pdusObj = (Object[]) bundle.get("pdus");
for (int i = 0; i < pdusObj.length; i++) {
SmsMessage currentMessage = SmsMessage.createFromPdu((byte[]) pdusObj[i]);
String phoneNumber = currentMessage.getDisplayOriginatingAddress();
senderNum = phoneNumber;
message = currentMessage.getDisplayMessageBody();
Log.i("SmsReceiver", "senderNum: " + senderNum + "; message: " + message);
}
if (senderNum.equals("ideamart")) {
Log.d("MessageIdeaMart", message);
} else if (senderNum.equals("FindDroid")) {
if (message.startsWith("Welcome")) {
String[] splitArray = message.split(" ");
String pin = splitArray[7];
Log.d("PIN", pin);
}
}
}
} catch (Exception e) {
Log.e("SmsReceiver", "Exception smsReceiver" + e);
}
}
public void setOnSmsReceivedListener(Context context) {
this.onSMSReceived = (OnSMSReceived) context;
}
public interface OnSMSReceived {
void onSMSReceived(String pin);
}
}