gpt4 book ai didi

android - 如何将数据从 BroadcastReceiver 传递到 Activity?

转载 作者:搜寻专家 更新时间:2023-11-01 09:41:54 26 4
gpt4 key购买 nike

<分区>

我有一个 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);
}

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com