0

Possible Duplicate:
Android SMS Receiver not working

I am in the beginning stages of a simple app to intercept sms messages from a specific number. At this point, all I'm trying to achieve is a toast when the onReceive method is fired, however I'm not getting anything.

manifest

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

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

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

   <application android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        <receiver android:name=".Receiver">
            <intent-filter>
                <action android:name="android.provider.Telephony.SMS_RECEIVED"/>
            </intent-filter>
        </receiver>
    </application>
</manifest>

And the receiver

public class Receiver extends BroadcastReceiver {

    private static final String SENDER = "****";
    private static final String SMS_RECEIVED = "android.provider.Telephony.SMS_RECEIVED";

     @Override
     public void onReceive(Context context, Intent intent) {    
         Toast.makeText(context, "Received!", Toast.LENGTH_LONG).show();

         if (intent.getAction().equals(SMS_RECEIVED)) {
             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) {
                     Toast.makeText(context, "Received a message!", Toast.LENGTH_LONG).show();
                     //abortBroadcast();
                 }
             }
         }
    }


}

Update 1 After creating an activity and launching the app, the receiver is registered. I need a way to have the receiver registered automatically though. There is no activity for the app, just the onReceive method intercepting the message. Is this possible? Perhaps using the boot complete intent?

UPdate 2 Solved it using a receiver for BOOT_COMPLETED to start a service which registered the sms receiver. Thanks.

Community
  • 1
  • 1
r2DoesInc
  • 3,759
  • 3
  • 29
  • 60
  • why you are checking intent.getAction().equals(SMS_RECEIVED) if you already filtered it manifest ? – savionok Feb 01 '12 at 23:39
  • because i was trying everything i could think of just to get some response. – r2DoesInc Feb 01 '12 at 23:55
  • did you try in manifest not to filter the receiver. and to make a test with catching all phone broadcasted events such as : – savionok Feb 02 '12 at 00:08
  • i solved it. see update two. im unable to answer my own question currently though so i had to include it in the question. – r2DoesInc Feb 02 '12 at 00:19

1 Answers1

1

Your solution is flawed. It will work for Android 3.0 and older. For Android 3.1, you must have an activity that the user launches at least once, to enable your app.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • the BOOT_COMPLETED intent starts it. Im on 4.0.3 and its working as intended with that starting the service and no activity ever being launched – r2DoesInc Feb 02 '12 at 00:18
  • @r2DoesInc: Try fully uninstalling the app, then reinstalling it. I suspect that you will find that your `BOOT_COMPLETED` receiver no longer works. – CommonsWare Feb 02 '12 at 00:25
  • Well thats quite a pain. your correct. Is there a way around this? The app that Im working on is to come preinstalled on devices. There is no activity tied to it, all it does is intercept specific messages and react to them. Theres gotta be a way around it i would imagine? – r2DoesInc Feb 02 '12 at 00:32
  • @r2DoesInc: "The app that Im working on is to come preinstalled on devices" -- the device manufacturer may have a way to address it, though you don't at the SDK level. – CommonsWare Feb 02 '12 at 00:48
  • Hmm. Alright, well thank you for you input, I will contact them as see if there is something that they can do to help me around it. – r2DoesInc Feb 02 '12 at 01:11