2016-03-08 283 views
0

即使應用程序收到短信,我的onReceive()也不會被調用。它在消息到達時不會被調用。它不應該在後臺工作,只有在SMS到達時纔會被調用? 它可以在單獨的項目上正常工作,但不會在我與我的應用程序進行交互時運行。廣播接收器不能正常工作,onReceive()不會被調用。我應該如何解決它?

我的代碼:

import android.content.BroadcastReceiver; 
import android.content.Context; 
import android.content.Intent; 
import android.content.SharedPreferences; 
import android.os.Bundle; 
import android.telephony.SmsMessage; 
import android.util.Log; 


public class SmsBroadcastReciever extends BroadcastReceiver { 
    private static final String LOG_TAG = "SMSBroadRec"; 
    public static final String SMS_BUNDLE = "pdus"; 
    String SenderNo = "+92----------"; 
    String SenderNo2 = "+92----------"; 

    @Override 
    public void onReceive(Context context, Intent intent) { 

     Log.d(LOG_TAG, "In onReceive()"); 
     Bundle bundle = intent.getExtras(); 
     String smsBody; 
     String address; 

     try { 

      if (!bundle.isEmpty()){ 
       Log.d(LOG_TAG, "Sms received"); 
       String verificationCode = null; 
       Object[] sms = (Object[]) bundle.get(SMS_BUNDLE); 
       for (Object sm : sms) { 
        SmsMessage smsMessage = SmsMessage.createFromPdu((byte[]) sm); 
        smsBody = smsMessage.getMessageBody(); 
        address = smsMessage.getOriginatingAddress(); 
        Log.d(LOG_TAG, address); 
        if (SenderNo.equals(address) || SenderNo2.equals(address)) { 
         verificationCode = getVerificationCode(smsBody); 
         Log.e(LOG_TAG, "OTP received: " + verificationCode); 
         break; 
        } else { 
         Log.d(LOG_TAG, "wrong sender"); 
         break; 
        } 
       } 
       SharedPreferences prefs = context.getSharedPreferences(AppConfig.APP_SCRATCH, Context.MODE_PRIVATE); 
       SharedPreferences.Editor ed = prefs.edit(); 
       ed.putString(AppConfig.APP_SCRATCH, verificationCode); 
       if (ed.commit()) { 
        Log.d(LOG_TAG, "commit succesful");       //added to the shared preferences 
       } else { 
        Log.d(LOG_TAG, "commit unsuccessful"); 
       } 

      } else { 
       Log.d(LOG_TAG, "Intent must be empty!"); 
      } 

     } 
     catch (Exception e){ 
      e.printStackTrace(); 
     } 
    } 
    private String getVerificationCode(String message) { 
     String code = null; 
     int index = message.indexOf(AppConfig.OTP_DELIMITER); 

     if (index != -1) { 
      int start = index + 2; 
      int length = 8; 
      code = message.substring(start, start + length); 
      return code; 
     } 
     return code; 
    } 
} 

這是清單文件的一部分,我已經添加權限過於它們READ_SMSRECEIVE_SMSWRITE_SMS

清單文件:在清單

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
    package="com.ubroadkast.nayatel" 
    android:versionCode="4" 
    android:versionName="1.3"> 

    <uses-sdk 
     android:minSdkVersion="14" 
     android:targetSdkVersion="18" /> 

    <uses-feature 
     android:name="android.hardware.camera" 
     android:required="true" /> 
    <uses-feature 
     android:name="android.hardware.camera.autofocus" 
     android:required="false" /> 
    <uses-feature 
     android:name="android.hardware.telephony" 
     android:required="true" /> 

    <supports-screens 
     android:largeScreens="true" 
     android:normalScreens="true" 
     android:smallScreens="true" 
     android:xlargeScreens="true" /> 

    <uses-permission 
     android:name="android.permission.INTERNET" 
     android:required="true" /> 
    <uses-permission 
     android:name="android.permission.WRITE_EXTERNAL_STORAGE" 
     android:required="true" /> 
    <uses-permission 
     android:name="android.permission.RECORD_AUDIO" 
     android:required="true" /> 
    <uses-permission 
     android:name="android.permission.CAMERA" 
     android:required="true" /> 
    <uses-permission 
     android:name="android.permission.READ_PHONE_STATE" 
     android:required="true" /> 
    <uses-permission 
     android:name="android.permission.ACCESS_NETWORK_STATE" 
     android:required="true" /> 
    <uses-permission 
     android:name="android.permission.SEND_SMS" 
     android:required="true" /> 
    <uses-permission 
     android:name="android.permission.ACCESS_WIFI_STATE" 
     android:required="true" /> 
    <uses-permission 
     android:name="android.permission.CHANGE_WIFI_STATE" 
     android:required="true" /> 
    <uses-permission 
     android:name="android.permission.READ_CONTACTS" 
     android:required="true" /> 
    <uses-permission 
     android:name="android.permission.WRITE_CONTACTS" 
     android:required="true" /> 
    <uses-permission 
     android:name="android.permission.READ_EXTERNAL_STORAGE" 
     android:required="true" /> 
    <uses-permission android:name="android.permission.WRITE_SMS" 
     android:required="true"/> 
    <uses-permission android:name="android.permission.RECEIVE_SMS" 
     android:required="true"/> 
    <uses-permission android:name="android.permission.READ_SMS" 
     android:required="true"/> 



    <application 
     android:allowBackup="true" 
     android:icon="@drawable/ic_launcher" 
     android:label="@string/app_name" 
     android:theme="@style/Theme.Ubroadkast"> 
     <meta-data 
      android:name="com.facebook.sdk.ApplicationId" 
      android:value="@string/facebook_app_id" /> 

     <provider 
      android:name="com.facebook.FacebookContentProvider" 
      android:authorities="com.facebook.app.FacebookContentProvider912845522101212" 
      android:exported="true" /> 

     <activity 
      android:name=".MainActivity" 
      android:configChanges="keyboard|keyboardHidden|screenLayout|screenSize|orientation" 
      android:label="@string/app_name" 
      android:screenOrientation="landscape" /> 
     <activity 
      android:name=".Home" 
      android:label="@string/app_name" 
      android:screenOrientation="portrait" /> 
     <activity 
      android:name=".Settings" 
      android:label="Settings" 
      android:screenOrientation="portrait" /> 
     <activity 
      android:name=".UserStatus" 
      android:label="@string/title_activity_user_status" /> 
     <activity 
      android:name=".login" 
      android:label="@string/app_name" 
      android:screenOrientation="portrait" /> 
     <activity 
      android:name=".SplashScreen" 
      android:label="@string/app_name" 
      android:screenOrientation="portrait"> 
      <intent-filter> 
       <action android:name="android.intent.action.MAIN" /> 

       <category android:name="android.intent.category.LAUNCHER" /> 
      </intent-filter> 

     </activity> 
     <activity 
      android:name="com.facebook.FacebookActivity" 
      android:configChanges="keyboard|keyboardHidden|screenLayout|screenSize|orientation" 
      android:label="@string/app_name" 
      android:theme="@android:style/Theme.Translucent.NoTitleBar" /> 
     <activity 
      android:name=".facebook" 
      android:label="UBroadkast Facebook Sign In" 
      android:screenOrientation="portrait" /> 
     <activity 
      android:name=".Terms" 
      android:label="Terms And Conditions" 
      android:screenOrientation="portrait" /> 
     <activity 
      android:name=".Signup" 
      android:label="Sign Up" 
      android:screenOrientation="portrait" /> 
     <activity 
      android:name=".rating" 
      android:label="Ubroadkast Feedback" 
      android:screenOrientation="portrait" /> 
     <activity 
      android:name=".change_password" 
      android:label="Reset Password" /> 
     <activity android:name=".HomeScreen" /> 
     <activity android:name=".Videoview" /> 
     <activity android:name=".URLExtractor" /> 

    <receiver android:name=".SmsBroadcastReciever" android:enabled="true" android:exported="false"> 
      <intent-filter android:priority="999"> 
       <action android:name="android.provider.Telephony.SMS_RECIEVED"/> 
      </intent-filter> 

     </receiver> 

    </application> 

</manifest> 
+0

你是否在清單文件中添加了sms權限? –

+0

是......接收,讀取,甚至寫入 – ibraizQ9

+0

哪個代碼行導致NullPointerException? – Akexorcist

回答

0

變化以下行從

<action android:name="android.provider.Telephony.SMS_RECIEVED"/> 

<action android:name="android.provider.Telephony.SMS_RECEIVED" /> 

有單詞的拼寫錯誤「收到」

+2

我該如何懲罰自己? – ibraizQ9

+0

有時會發生。快樂的編碼! :) – zeeali

相關問題