我在Android应用程序中使用BroadcastReceiver自动获取OTP消息。但是BroadcastReceiver无法正常工作而且找不到错误所以请帮助我。我把 Log.d(" onReciver&# 34;," on")在 onReceive()中进行检查但不起作用。
IncomingMessage.java
public class IncomingMessage extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Log.d("onReciver","on");
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 ;
Log.d("sender NUm",senderNum);
String message = currentMessage .getDisplayMessageBody();
try
{
if (senderNum.equals("(727)594-3351"))
{
OtpVarificationActivity Sms = new OtpVarificationActivity();
Sms.recivedSms(message );
}
}
catch(Exception e){
Log.e("BR error",e.toString());
}
}
}
} catch (Exception e)
{
Log.e("BR error1",e.toString());
}
}
}
的Manifest.xml
<uses-permission android:name="android.permission.SEND_SMS" />
<uses-permission android:name="android.permission.READ_SMS" />
<uses-permission android:name="android.permission.RECEIVE_SMS" />
...
...
...
<receiver android:name="com.appsprotocol.dcntv.broadcast.IncomingMessage">
<intent-filter>
<action android:name="android.provider.Telephony.SMS_RECEIVED"/>
</intent-filter>
</receiver>
答案 0 :(得分:0)
这是我的工作示例
在AndoridMenifest.xml中注册Receiver:
<receiver android:name="com.example.tosc185.readincommingsmsdemo.SmsListener">
<intent-filter>
<action android:name="android.provider.Telephony.SMS_RECEIVED" />
</intent-filter>
</receiver>
接收者类:
public class SmsListener extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
if (intent.getAction().equals("android.provider.Telephony.SMS_RECEIVED")) {
Bundle bundle = intent.getExtras(); //---get the SMS message passed in---
SmsMessage[] msgs = null;
String msg_from;
if (bundle != null) {
//---retrieve the SMS message received---
try {
Object[] pdus = (Object[]) bundle.get("pdus");
msgs = new SmsMessage[pdus.length];
for (int i = 0; i < msgs.length; i++) {
if(Build.VERSION.SDK_INT <= 22) {
msgs[i] = SmsMessage.createFromPdu((byte[]) pdus[i]);
}
else {
msgs[i] = SmsMessage.createFromPdu((byte[]) pdus[i], String.format(Locale.US, null));
}
msg_from = msgs[i].getOriginatingAddress();
String msgBody = msgs[i].getMessageBody();
Toast.makeText(context, "From -" + msg_from + " : Body- " + msgBody, Toast.LENGTH_LONG).show();
if (msg_from.equalsIgnoreCase("5555")) {
MainActivity.insertCode(msgBody);
}
}
} catch (Exception e) {
// Log.d("Exception caught",e.getMessage());
}
}
}
}
}
答案 1 :(得分:0)
尝试在清单中为接收者添加优先级。这对我有用。
mongoDB
答案 2 :(得分:0)
在清单文件中添加启用和导出。您可以在下面找到清单的代码示例。
<receiver
android:name=".SmsReceiver"
android:enabled="true"
android:exported="true">
<intent-filter android:priority="999">
<action android:name="android.provider.Telephony.SMS_RECEIVED" />
</intent-filter>
</receiver>