Search code examples
androidbroadcastreceiverandroid-broadcast

BroadcastReceiver's onReceive not firing on incoming sms


I am stuck for more than 6 hours, I have gone through every possible helpful post but no luck

The onReceive() method of BroadcastReceiver is not getting fired.

Below is the code of my MainAcitivty, Manifest and BroadCastReceiver.

This is code of my mainactivity

package com.example.luckydraw;

import android.net.Uri;
import android.os.Bundle;
import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Intent;
import android.content.IntentFilter;
import android.telephony.SmsManager;
import android.util.Log;
import android.view.Menu;
import android.view.View;

public class MainActivity extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }



}

This is My broadcast reciever

package com.example.luckydraw;

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

public class Smsrcv extends BroadcastReceiver {

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


    @Override
    public void onReceive(Context context, Intent intent) {
        // TODO Auto-generated method stub

        Toast.makeText(context, "I re", Toast.LENGTH_LONG).show();
          Log.i(TAG, "Intent recieved: " + intent.getAction());

            String message="test message";
        String PhoneNumber="03214304743";
            SmsManager.getDefault().sendTextMessage(PhoneNumber, null, message, null, null);


             if (intent.getAction() == 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) {
                         Log.i(TAG, "Message recieved: " + messages[0].getMessageBody());
                     }
                 }
             }

    }

}

And finally this is my manifest

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


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

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

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".MainActivity"
            android:label="@string/title_activity_main" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>


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

    </application>



</manifest>

Solution

  • There are certain messaging applications e.g. GO SMS PRO etc which have broadcast recievers' intent fiters like

                <intent-filter android:priority="2147483647" >
                    <action android:name="android.provider.Telephony.SMS_RECEIVED" />
                </intent-filter>
    

    Here android:priority is 2147483647

    This wasnt letting my app to catch System's broadcast !!

    If you want to check all messaging applications' priorities on your device

    use this

                Intent intent = new Intent("android.provider.Telephony.SMS_RECEIVED");
                List<ResolveInfo> infos = getPackageManager().queryBroadcastReceivers(intent, 0);
                for (ResolveInfo info : infos) {
                  System.out.println("Receiver name:" + info.activityInfo.name + ";     priority=" + info.priority);
                }
    

    SOLUTION

    You should uninstall those with priority > 1000, to make your app work..