Search code examples
androidbroadcastreceiverandroid-4.4-kitkat

Cannot read SMS in KitKat, very confused


trying to get a very simple SMS broadcastreceiver working on my KitKat phone, but nothing is working. Driving me a little crazy. I don't need to do anything but read the SMS, so I don't think the big SMS changes in KitKat will affect me. I've read many posts, tried everything.

From what I've read I should only need SMS_DELIVER_ACTION to do a simple sniff of the text. I've put READ_SMS and RECEIVE_SMS in the manifest permissions. I have had it with JUST READ_SMS at first. I assume receive gives it to the app first, probably is short-circuited by KitKat "default" SMS behavior. BTW, nothing I've done will allow my app to show in the default sms system setting.

I have an incredibly simple app that has 1 activity, 1 broadcastreceiver class, and it's registered in the manifest. In onReceive I'm trying to write to LogCat and show a toast when a text comes in. Nothing ever hits that code.

Here's my manifest:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.smoftware.smsreceivertest.app" >
    <uses-permission android:name="android.permission.READ_SMS"/>
    <uses-permission android:name="android.permission.RECEIVE_SMS"/>

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

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

        <receiver
            android:name=".SMSReceiver"
            android:permission="android.permission.BROADCAST_SMS">
            <intent-filter>
                <action android:name="android.provider.Telephony.SMS_DELIVER_ACTION"/>
            </intent-filter>
        </receiver>
    </application>
</manifest>

and my broadcastreceiver...

public class SMSReceiver extends BroadcastReceiver {
    public SMSReceiver() {
    }

    @Override
    public void onReceive(Context context, Intent intent) {
        Toast.makeText(context,"in onReceive of SMSReceiver",Toast.LENGTH_LONG).show();
        Log.d("SMSReceiver", "In body of onReceive method");
    }
}

I have a main activity but it's just a helloworld template. I made sure to have this because I've read about the 3.1 change that keeps a broadcastreceiver from running if not launched yet.

There shouldn't be any issue with this running in debug, should it? Even if so, I've stopped the app and relaunched so that it has been manually opened.

Thanks!


Solution

  • Set receiver's IntentFilter as :

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

    Following permissions :

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