2016-04-26 205 views
-2

我在我的應用程序中使用廣播接收器來讀取從服務器發送的OTP,我沒有提到manifest.xml中的任何權限,但它不讀取OTP。我不知道問題出在哪裏。有人可以幫我糾正嗎?請幫助我。廣播接收器不會在android中接收短信?

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; 

} 
+0

創建接收機類..把這個代碼的類中,並在寄存器清單它.. – Sanoop

+0

你沒有註冊接收器br。 –

+0

爲什麼?然後如何註冊 – John

回答

0

要通過BroadReciever讀取SMS,您可以在清單中或在YourActivity中註冊YourReciever。否則你不能聽廣播。

所需的權限

<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); 
       } 
      } 
+0

我正在註冊接收器onCreate最新的問題 – John

+0

如果你不註冊,那麼它會導致問題。 onPause&onResume是解決這些問題的最佳選擇。你也可以使用onCreate和onDestroy。 – shobhan

+0

如果我動態註冊廣播接收機,我怎麼能在menifest – John

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> 
0

您需要註冊才廣播接收機像下面和清單

 IntentFilter mIntentFilter = new IntentFilter(); 
    mIntentFilter.addAction("android.provider.Telephony.SMS_RECEIVED"); 
    // mIntentFilter.setPriority(2147483647); 
    registerReceiver(optReceiver, mIntentFilter); 
申報所需的權限