我正在努力自动验证 OTP。在低于 lollipop 的版本中,它运行良好但在 5.0 及更高版本中不起作用我尝试了教程中的代码。这是代码:
传入短信.java
public class IncomingSms extends BroadcastReceiver
{
@Override
public void onReceive(Context context, Intent intent)
{
final Bundle bundle = intent.getExtras();
try {
if (bundle != 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();
String senderNum = phoneNumber ;
String message = currentMessage .getDisplayMessageBody();
try
{
if (senderNum .equals("+917760882587"))
{
MainActivity Sms = new MainActivity();
Sms.recivedSms(message );
}
}
catch(Exception e){}
}
}
} catch (Exception e)
{
}
}
}
MainActivity.java
public class MainActivity extends AppCompatActivity {
static EditText OtpNumber;
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
OtpNumber= (EditText) findViewById(R.id.editText);
}
public void recivedSms(String message)
{
try
{
OtpNumber.setText(message);
}
catch (Exception e)
{
}
}
}
谁能帮助为什么它不适用于棒棒糖及以上设备?