作者热门文章
- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
<分区>
我有一个 BroadcastReceiver
类,它读取传入的 sms
并从 sms
中提取一个 pin。我想将该 sms
传递给用户已经可见的 Activity 。用户应该在 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);
}
我是一名优秀的程序员,十分优秀!