Android - SMS Broadcast receiver

前端 未结 10 1711
遥遥无期
遥遥无期 2020-11-22 13:04

I have been trying to get this program to work but so far having no luck. I cannot find where I am doing wrong. I\'m not sure if there\'s something wrong with the code, or d

相关标签:
10条回答
  • 2020-11-22 13:51

    Also note that the Hangouts application will currently block my BroadcastReceiver from receiving SMS messages. I had to disable SMS functionality in the Hangouts application (Settings->SMS->Turn on SMS), before my SMS BroadcastReceived started getting fired.

    Edit: It appears as though some applications will abortBroadcast() on the intent which will prevent other applications from receiving the intent. The solution is to increase the android:priority attribute in the intent-filter tag:

    <receiver android:name="com.company.application.SMSBroadcastReceiver" >
      <intent-filter android:priority="500">
        <action android:name="android.provider.Telephony.SMS_RECEIVED" />
      </intent-filter>
    </receiver>
    

    See more details here: Enabling SMS support in Hangouts 2.0 breaks the BroadcastReceiver of SMS_RECEIVED in my app

    0 讨论(0)
  • 2020-11-22 14:00

    intent.getAction().equals(SMS_RECEIVED)

    I have tried it out successfully.

    0 讨论(0)
  • 2020-11-22 14:01

    Your broadcast receiver must specify android:exported="true" to receive broadcasts created outside your own application. My broadcast receiver is defined in the manifest as follows:

    <receiver
        android:name=".IncomingSmsBroadcastReceiver"
        android:enabled="true"
        android:exported="true" >
        <intent-filter>
            <action android:name="android.provider.Telephony.SMS_RECEIVED" />
        </intent-filter>
    </receiver>
    

    As noted below, exported="true" is the default, so you can omit this line. I've left it in so that the discussion comments make sense.

    0 讨论(0)
  • 2020-11-22 14:04

    Stumbled across this today. For anyone coding an SMS receiver nowadays, use this code instead of the deprecated in OP:

    SmsMessage[] msgs = Telephony.Sms.Intents.getMessagesFromIntent(intent);
    SmsMessage smsMessage = msgs[0];
    
    0 讨论(0)
提交回复
热议问题