0

For my project, I need to use an BroadcastReceiver to receive the SMS. My code work with my emulator but no with my phone. My phone is a HTC one X (android 4.0.3). In the emulation, I can see the toast when I receive an SMS and in my phone I don't see that...

My code:

SMSReceiver.java:

package com.tuto.smsreceiver;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.telephony.SmsMessage;
import android.widget.Toast;

public class SMSReceiver extends BroadcastReceiver
{
     private final String   ACTION_RECEIVE_SMS  = "android.provider.Telephony.SMS_RECEIVED";

     @Override
     public void onReceive(Context context, Intent intent)
     {
         if (intent.getAction().equals(ACTION_RECEIVE_SMS))
         {
             Bundle bundle = intent.getExtras();
             if (bundle != null)
             {
                 Object[] pdus = (Object[]) bundle.get("pdus");

                 final SmsMessage[] messages = new SmsMessage[pdus.length];
                 for (int i = 0; i < pdus.length; i++)  {  messages[i] = SmsMessage.createFromPdu((byte[]) pdus[i]);  }  if (messages.length > -1)
                 {
                     final String messageBody = messages[0].getMessageBody();
                     final String phoneNumber = messages[0].getDisplayOriginatingAddress();

                     Toast.makeText(context, "Exp : " + phoneNumber, Toast.LENGTH_LONG).show();
                     Toast.makeText(context, "MSG recu : " + messageBody, Toast.LENGTH_LONG).show();

                 }
             }
        }
    }
}

AndroidManifest.xml:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.tuto.smsreceiver"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="7"
        android:targetSdkVersion="8" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >



    <receiver class="com.tuto.smsreceiver.SMSReceiver"
        android:name="com.tuto.smsreceiver.SMSReceiver">
        <intent-filter android:priority="999">
            <action android:name="android.provider.Telephony.SMS_RECEIVED" />
        </intent-filter>
    </receiver>

    </application>

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

</manifest>
Ionică Bizău
  • 109,027
  • 88
  • 289
  • 474
Shuty
  • 87
  • 1
  • 4
  • 9
  • 2
    Welcome to StackOverflow. Have you tried to use the debugger to see if the receiver is even called? Have you tried to put a toast or a log in every else case? Have you checked log cat if you find any issues there? – WarrenFaith Jan 12 '13 at 19:39
  • What version of Android is on the Emulator? – t0mm13b Jan 12 '13 at 19:43
  • Agreed with @WarrenFaith. Adding a toast or Logcat debug message in the onReceive function itself (or in an else block of the if statement) will at least let you see if the android.provider.Telephony.SMS_RECEIVED intent is being caught by your filter. Try outputting intent.getAction(). If it shows you something, that's the intent to register for. If you don't see anything, I wouldn't be surprised if the intent is something slightly different on HTC phones (which would explain why it works in the emulator but not on the phone). I've had something similar happen on HTC devices in the past. – Aaron Jan 12 '13 at 20:01

1 Answers1

0

When SMS message is received, broadcast which you are trying to catch is an ordered broadcast. It can be cancelled by any recipient (in this case it will not be received by other potential recipients, including your BroadcastReceiver). I cannot be 100% sure, but probably something on your device receives this broadcast and cancels it (I would blame software from HTC stack, I heard that's well known issue with it).

For details, check out BroadcastReceiver documentation (part of it which explains ordered broadcasts).

Andrii Chernenko
  • 9,873
  • 7
  • 71
  • 89
  • 1
    Good call. It looks like some GoSMS app (possibly bundled with HTC?) has been a [common culprit](http://code.google.com/p/gtalksms/issues/detail?id=308) for some similar cases? [This answer](http://stackoverflow.com/a/8037914/1098302) might help diagnose if something else is cancelling the intent. – Aaron Jan 12 '13 at 20:11