21

I'm trying to use android.provider.Telephony.SMS_RECEIVED to catch incoming SMS's.

I built a simple app, which works on 2.x, but when I try it on my 4.0 emulator or device, it doesn't work.

Any ideas?

Manifest:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.giggsey.MyFirstApp" android:versionCode="1"
android:versionName="1.0">
<application android:icon="@drawable/ic_launcher" android:label="@string/app_name">


    <receiver android:name=".MyFirstApp">
        <intent-filter>
            <action android:name="android.provider.Telephony.SMS_RECEIVED"></action>
        </intent-filter>
    </receiver>
</application>
<uses-sdk android:minSdkVersion="9" />


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

MyFirstApp.java

public class MyFirstApp extends BroadcastReceiver {

    private static final String SMS_RECEIVED = "android.provider.Telephony.SMS_RECEIVED";
    private static final String TAG = "MyFirstApp";

    @Override
    public void onReceive(Context context, Intent intent) {
         Log.i(TAG, "Intent recieved: " + intent.getAction());
    }
}
NullUserException
  • 83,810
  • 28
  • 209
  • 234
giggsey
  • 933
  • 2
  • 11
  • 31
  • Are you using a real device or the emulator? – rekire Dec 02 '11 at 08:07
  • @rekire both on 4.0. Also tried the emulator in 2.3, and that works. – giggsey Dec 02 '11 at 11:37
  • I've changed the receiver name to a class that doesn't exist, and it still does nothing (no errors in logcat). So I'm thinking that it's not even getting that far. So either it's not firing that intent (unlikely), or it's not using my App (likely) – giggsey Dec 04 '11 at 12:53
  • Nevermind, I didn't see that Matt says essentially the same thing I did... http://stackoverflow.com/a/8421369/520186 – rf43 Dec 08 '11 at 01:16
  • 2
    Have a look at this answer. Should solve your problem. http://stackoverflow.com/questions/7349173/android-xoom-honeycomb-application-without-launcher-activity-does-not-work/7350165#7350165 – Gaurav Das Dec 09 '11 at 07:20

6 Answers6

4

Make sure that you are actually creating and registering the receiver in an Activity or a Service, otherwise it will not get called (I believe).

A very simple example of this might be:

public class MyActivity extends Activity {

    private BroadcastReceiver receiver;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        IntentFilter filter = new IntentFilter();
        filter.addAction("android.provider.Telephony.SMS_RECEIVED");
        //Extends BroadcastReceiver
        receiver = new MyFirstApp();
        registerReceiver(receiver,filter);
    }


   //Also, to save headaches later
   @Override
   protected void onDestroy() {
        unregisterReceiver(receiver);
   }
}

I can't promise this will work, but I believe it will fix some things. If you have any questions about stuff, just ask in comments. I believe you are correct in saying that it is not even getting called because your receiver is not registered to anything. If you want it to run in the background consider using a service. I really hope this helps and best of luck with your endeavors!

Matt
  • 5,404
  • 3
  • 27
  • 39
  • Thanks Matt, this was it. Sorry you don't get the bounty, didn't check until after it had expired (other commitments) – giggsey Dec 20 '11 at 18:10
  • 2
    All good, Interestingly enough there is more to life than programming :0) – Matt Dec 22 '11 at 01:27
  • A statically registered Receiver doesn't need to be registered dynamically. [This post](http://stackoverflow.com/questions/7349173/android-xoom-honeycomb-application-without-launcher-activity-does-not-work/7350165#7350165) linked by @Gaurav in the comments above describes the actual problem in this case. – Mike M. Jan 19 '16 at 18:37
1

You just need to exported value true for the reciever.

<receiver android:name=".MyFirstApp" exported="true">
        <intent-filter>
            <action android:name="android.provider.Telephony.SMS_RECEIVED"></action>
        </intent-filter>
</receiver>
Sazzad Hissain Khan
  • 37,929
  • 33
  • 189
  • 256
0

in case you try "catch" in backgroung you can see this post.

"android.provider.Telephony.SMS_RECEIVED" work great from Service. otherwise only during activity lifecycle you can "catch" it

Community
  • 1
  • 1
itzhar
  • 12,743
  • 6
  • 56
  • 63
  • Sorry, should've been specific. "otherwise only during activity lifecycle you can "catch" it" - This part is not correct. Dynamically registering the Receiver will work, as long as the registering component is active at the time of receipt. However, the Receiver does not need to be dynamically registered. Statically registering it in the manifest is valid. The problem in this case is described in [this post](http://stackoverflow.com/questions/7349173/android-xoom-honeycomb-application-without-launcher-activity-does-not-work/7350165#7350165) linked by @Gaurav in their comment on the question. – Mike M. Jan 19 '16 at 18:15
  • No. Your link not describe my problem & solution. Static manifest reciever NOT work. – itzhar Jan 19 '16 at 18:25
  • "Static manifest reciever NOT work." - I'm sorry, but that's just not true. Furthermore, no matter how you solved your specific problem, _in this case_, it was the app being in the stopped state which prevented the Receiver from working. The OP needed an Activity to run at least once after installation to enable the statically registered Receiver. – Mike M. Jan 19 '16 at 18:31
  • i edited answer so people with this problem only, use it. thanks for your feedback – itzhar Jan 19 '16 at 18:36
  • Are you saying that statically registered Receivers don't work in Lollipop? That's not true either. Even the platform SMS app registers its Receivers [in the manifest](https://android.googlesource.com/platform/packages/apps/Mms/+/android-5.0.0_r1/AndroidManifest.xml). – Mike M. Jan 19 '16 at 18:51
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/101118/discussion-between-itzhar-and-mike-m). – itzhar Jan 19 '16 at 18:57
0

I think your error is that you use the class name in your package name.

In your manifest you wrote package="com.giggsey.MyFirstApp" and also <receiver android:name=".MyFirstApp"> in your receiver. This would mean that the full name of your receiver is com.giggsey.MyFirstApp.MyFirstApp, but I belive it is just com.giggsey.MyFirstApp.

Exchange com.giggsey.MyFirstApp in your manifest with com.giggsey that sould work if I guess right.

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

And also this:

package com.giggsey;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;

public class MyFirstApp extends BroadcastReceiver {
    private static final String TAG = "MyFirstApp";

    @Override
    public void onReceive(Context context, Intent intent) {
        Log.i(TAG, "Intent recieved: " + intent.getAction());
    }
}
rekire
  • 47,260
  • 30
  • 167
  • 264
  • Tried with both com.giggsey as the package, and the full path (com.giggsey.MyFirstApp.MyFirstApp in the receiver, and neither worked :( – giggsey Dec 04 '11 at 21:19
-1

This may help you..try this.. In brodcast receiver class

 public static final String SMS_BUNDLE = "pdus";


public void onReceive(Context context, Intent intent)
{

     Bundle intentExtras = intent.getExtras();
        if (intentExtras != null) {
            Object[] sms = (Object[]) intentExtras.get(SMS_BUNDLE);
            String smsMessageStr = "";
            for (int i = 0; i < sms.length; ++i) {
                SmsMessage smsMessage = SmsMessage.createFromPdu((byte[]) sms[i]);

                smsBody = smsMessage.getMessageBody().toString();
                address = smsMessage.getOriginatingAddress();

                smsMessageStr += "SMS From: " + address + "\n";
                smsMessageStr += smsBody + "\n";
            }
            Toast.makeText(context, smsMessageStr, Toast.LENGTH_SHORT).show();
    }
Namrata
  • 137
  • 1
  • 2
  • 12
-2

in the On receive please try to add the following line

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

if (intent.getAction() == SMS_RECEIVED) {
    //any action you want here..
    Toast.makeText(MyClass.this, "SMS RECEIVED",Toast.LENGTH_LONG).show();
}
rekire
  • 47,260
  • 30
  • 167
  • 264
Dany's
  • 943
  • 1
  • 7
  • 17