广播接收器在android中没有收到短信?

时间:2016-04-26 06:16:07

标签: android

我在我的应用程序中使用广播接收器来读取从服务器发送的OTP,我没有在manifest.xml中提及任何权限,但它没有读取OTP。我不知道问题出在哪里。有人可以帮我纠正吗?请帮帮我。

public BroadcastReceiver br = new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {
        final Bundle bundle = intent.getExtras();
        try {
            if (bundle != null) {
                Object[] pdusObj = (Object[]) bundle.get("pdus");
                assert pdusObj != null;
                for (Object aPdusObj : pdusObj) {
                    @SuppressWarnings("deprecation") SmsMessage currentMessage = SmsMessage.createFromPdu((byte[]) aPdusObj);
                    String phoneNumber = currentMessage.getDisplayOriginatingAddress();
                    String message = currentMessage.getDisplayMessageBody();

                    Log.e(s_szTAG, "Received SMS: " + message + ", Sender: " + phoneNumber);

                    // checking sms sender address....
                    if (phoneNumber.toLowerCase().contains("+919971599909".toLowerCase())) {
                        // verification code from sms
                        m_szOtpCode = getVerificationCode(message);
                        assert m_szOtpCode != null;
                        String input = m_szOtpCode.trim();

                        Log.e(s_szTAG, "OTP received: " + m_szOtpCode);
                        COTPVerificationDataStorage.getInstance().setM_szOtp(input);// getting otp from SMS and set to otpverificationstorage class

                    } else {
                        return;
                    }

                }
            }
        } catch (Exception e) {
            Log.e(s_szTAG, "Exception: " + e.getMessage());
        }
    }

    @SuppressWarnings("JavaDoc")
    private String getVerificationCode(String message) {
        String code;
        int index = message.indexOf(":");

        if (index != -1) {
            int start = index + 2;
            int length = 6;
            code = message.substring(start, start + length);
            return code;
        }
        COTPVerificationDataStorage.getInstance().setM_szOtp(m_szOtpCode);
        return null;
    }
};
private IntentFilter inf;

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    m_Main = inflater.inflate(R.layout.otp_auto_verified, container, false);

    inf = new IntentFilter();
    inf.addAction("android.provider.Telephony.SMS_RECEIVED");


    getUserDetails();// getuser deatails....
    init();// initialize controls...
    return m_Main;

}

3 个答案:

答案 0 :(得分:0)

尝试注册接收器

public class ReceiverSMS extends BroadcastReceiver {

   // your onReceive code
}

并在清单中......

    <receiver android:name=".ReceiverSMS">
        <intent-filter>
            <action android:name="android.provider.Telephony.SMS_RECEIVED" />
        </intent-filter>
    </receiver>

答案 1 :(得分:0)

您必须在下面注册广播接收器并在清单中声明必要的权限

        IntentFilter mIntentFilter = new IntentFilter();
    mIntentFilter.addAction("android.provider.Telephony.SMS_RECEIVED");
    // mIntentFilter.setPriority(2147483647);
    registerReceiver(optReceiver, mIntentFilter);

答案 2 :(得分:0)

要通过SMS阅读BroadReciever,您可以在清单或YourReciever中注册YourActivity。否则你不能听广播。

需要许可

<uses-permission android:name="android.permission.RECEIVE_SMS" />

静态注册

<receiver android:name=".YourSmsReciever">
    <intent-filter android:priority="99999">
        <action android:name="android.provider.telephony.SMS_RECIEVED"></action>
    </intent-filter>
</receiver>

动态注册

           class YourActivity extends Activity{
    //all remaining life cycle methods will goes here

                 YourReciever yourReciever = new YourReciever;
                 public void onResume(params){
                    IntentFilter mIntentFilter = new IntentFilter();
                    mIntentFilter.addAction("android.provider.Telephony.SMS_RECEIVED");
                    registerReceiver(yourReceiver, mIntentFilter);
                 }
                 public void onPause(params){
                    unRregisterReceiver(yourReceiver);
                 }
            }