2017-05-31 70 views
0

我正在構建一個SMS應用程序,並且有one activitybroadcast receiver。問題是broadcast receiveronReceive()方法沒有被調用。下面給出了receiver類和manifest文件的codeSMS廣播接收器onReceive()方法不被調用

廣播接收機

public class SmsBroadcastReceiver extends BroadcastReceiver { 
    public AudioManager audioManager; 
    SharedPreferences sharedPreferences; 
    boolean b=false; 
    String smsBody; 
    final int REQUEST_CODE_ASK_PERMISSIONS = 123; 

    public static final String SMS_BUNDLE = "pdus"; 

    SmsMessage smsMessage; 
    public void onReceive(Context context, Intent intent) { 
     Toast.makeText(context.getApplicationContext(),"Toast Long",Toast.LENGTH_LONG).show(); 


     sharedPreferences = context.getApplicationContext().getSharedPreferences("Pritom", context.MODE_PRIVATE); 
     Bundle intentExtras = intent.getExtras(); 
     String format = intentExtras.getString("format"); 
     if (intentExtras != null) { 
      Object[] sms = (Object[]) intentExtras.get(SMS_BUNDLE); 
      String smsMessageStr = ""; 
      String pass3 = sharedPreferences.getString("password", null); 
      for (int i = 0; i < sms.length; ++i) { 

       if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { 

        smsMessage = SmsMessage.createFromPdu((byte[]) sms[i], format); 
       } else { 
        smsMessage = SmsMessage.createFromPdu((byte[]) sms[i]); 
       } 
       smsBody = smsMessage.getMessageBody().toString(); 
       String address = smsMessage.getOriginatingAddress(); 

       smsMessageStr += "SMS From: " + address + "\n"; 
       smsMessageStr += smsBody + "\n"; 
      } 
      /*if (smsBody.equals("@general" + pass3)) { 
       AudioManager audioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE); 
       audioManager.setRingerMode(audioManager.RINGER_MODE_NORMAL); 
      }*/ 
      Toast.makeText(context, "SMS : " + smsBody + pass3, Toast.LENGTH_SHORT).show(); 
      Log.d("Message:", smsBody + pass3); 

     } 
    } 
} 

的這裏的問題是,沒有被顯示的正下方的onReceive()消息Toast這表明該方法onReceive()不被調用。

AndroidManifest.xml中

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
    package="com.mytrendin.inappmessaging"> 
    <uses-permission android:name="android.permission.READ_SMS" /> 
    <uses-permission android:name="android.permission.RECEIVE_SMS" /> 
    <uses-permission android:name="android.permission.READ_PHONE_STATE"/> 
    <application 
     android:allowBackup="true" 
     android:icon="@mipmap/ic_launcher" 
     android:label="@string/app_name" 
     android:supportsRtl="true" 
     android:theme="@style/AppTheme"> 
     <activity android:name=".MainActivity"> 
      <intent-filter> 
       <action android:name="android.intent.action.MAIN" /> 

       <category android:name="android.intent.category.LAUNCHER" /> 
      </intent-filter> 
     </activity> 
     <receiver android:name=".SmsBroadcastReceiver" android:exported="true" > 
      <intent-filter android:priority="999" > 
       <action android:name="android.provider.Telephony.SMS_RECEIVED" /> 
      </intent-filter> 
     </receiver> 
    </application> 

</manifest> 

我已經使用Android 5.0.1測試我的應用程序。 謝謝您的幫助:)

+0

嘗試設置優先級的android:在接收器優先=「2147483647」的意圖過濾 – FAT

+0

@DanielNugent,我有嘗試過,但它沒有工作:( –

+0

在哪個活動/片段你調用這個接收器? – Ibrahim

回答

0

嘗試在接收器intent-filter設置更高的優先級android:priority="2147483647"

<receiver android:name=".SmsBroadcastReceiver" android:exported="true" > 
    <intent-filter android:priority="2147483647" > 
     <action android:name="android.provider.Telephony.SMS_RECEIVED" /> 
    </intent-filter> 
</receiver> 
+0

我已經這樣做了,但它沒有工作 –

+0

我想有一些問題'android 5.0' –

+0

我認爲你的應用程序需要運行時間接收短信的權限。 – FAT

相關問題