5

What I've done


Hello Guys, I'm creating at the moment a SMS Broadcast Receiver, i just builded one up with this tutorial: Broadcasttutorial. After I did the code, I updated my Manifest. After that I sent sms from my other Phone to my Phone, but It didn't work. I didn't get any output.

Question


What do I need to change, that I can receive those SMS. Please gimme a detailed anwser that I can learn it, a good tutorial would also be great!

Code


SMSBroadcastReceiver (is in package .services)

package de.retowaelchli.filterit.services;

import de.retowaelchli.filterit.R;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.telephony.SmsMessage;
import android.util.Log;
import android.widget.Toast;


public class SmileySmsReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) 
    {
        //---get the SMS message passed in---
        Log.d("SmileySmsReceiver", "Yes it calls the onReceive");
        Bundle bundle = intent.getExtras();        
        SmsMessage[] msgs = null;
        String str = "";            
        if (bundle != null)
        {
            //---retrieve the SMS message received---
            Object[] pdus = (Object[]) bundle.get("pdus");
            msgs = new SmsMessage[pdus.length];            
            for (int i=0; i<msgs.length; i++){
                msgs[i] = SmsMessage.createFromPdu((byte[])pdus[i]);                
                str += "SMS from " + msgs[i].getOriginatingAddress();                     
                str += " :";
                str += msgs[i].getMessageBody().toString();
                str += "\n";        
            }
            //---display the new SMS message---
            Toast.makeText(context, str, Toast.LENGTH_SHORT).show();
        }                         
    }
}

This is my AndroidManifest.xml:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="de.retowaelchli.filterit"
      android:versionCode="1"
      android:versionName="1.0">
    <uses-sdk android:minSdkVersion="10" />

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

    <application android:icon="@drawable/icon"
                 android:label="@string/app_name"
                 android:debuggable="true"
                 android:screenOrientation="sensor"
                 android:theme="@style/FilterIt.Theme"> 

        <activity android:name=".SplashScreenActivity"
                  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 -->
        <receiver android:name="de.retowaelchli.filterit.services.SmileySmsReceiver" android:enabled="true"> 
            <intent-filter> 
                <action android:name="android.provider.Telephony.SMS_RECEIVED" /> 
            </intent-filter> 
        </receiver>     



        <!--  Startseite -->
        <activity android:name=".StartseiteActivity"></activity>

        <!-- Von Startseite ausgehende Activitys -->   
        <activity android:name=".SmileyActivity"></activity>
        <activity android:name=".ADeleteActivity"></activity>
        <activity android:name=".StatsActivity"></activity>
        <activity android:name=".HelpMenuActivity"></activity>


        <!-- Von Stats ausgehende Activitys -->
        <activity android:name=".stats.ADFilterStats"></activity>
        <activity android:name=".stats.SFilterStats"></activity>
        <activity android:name=".stats.CreatedADFilters"></activity>
        <activity android:name=".stats.CreatedSFilters"></activity>

        <!-- Von ADeleteActivity ausgehende Activitys -->
        <activity android:name=".ADFilterConfigActivity"></activity>

        <!--  Von SmileyActivity ausgehende Activitys -->
        <activity android:name=".SFilterConfigActivity"></activity>

    </application>
</manifest>
Volo
  • 28,673
  • 12
  • 97
  • 125
safari
  • 7,565
  • 18
  • 56
  • 82

4 Answers4

6

Put <uses-permission android:name="android.permission.RECEIVE_SMS" /> outside of the <application> tag:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="de.retowaelchli.filterit"
      android:versionCode="1"
      android:versionName="1.0">
    <uses-sdk android:minSdkVersion="10" />

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

    <application android:icon="@drawable/icon"
                 android:label="@string/app_name"
                 android:debuggable="true"
                 android:screenOrientation="sensor"
                 android:theme="@style/FilterIt.Theme"> 

    <!-- Receiver -->
        <receiver android:name="de.retowaelchli.filterit.services.SmileySMSBroadcastReceiver"> 
            <intent-filter android:priority="999"> 
                <action android:name="android.provider.Telephony.SMS_RECEIVED" /> 
            </intent-filter> 
        </receiver>
       …
       …
    </application>
</manifest>

UPDATE

Turned out that @safari uses "Handcent SMS" application on his phone which intercepts incoming SMS (this is possible because SMS_RECEIVED is an ordered broadcast and can be canceled by high priority broadcast receivers, refer to this thread for details).
To bypass this issue one would need to install broadcast receiver with higher priority than "Handcent SMS". @safari used the highest priority allowed for applications in Android: 999, and it worked for him.
To specify priority of broadcast receiver add android:priority attribute to corresponding <intent-filter> item:

<receiver android:name="YourSmsBroadcastReceiver">
    <intent-filter android:priority="999"> 
        <action android:name="android.provider.Telephony.SMS_RECEIVED" /> 
    </intent-filter>
</receiver>
Community
  • 1
  • 1
Volo
  • 28,673
  • 12
  • 97
  • 125
  • i did that, but it didn't work i still don't get the toastmessage with the message in it, whats wrong :(? – safari Oct 25 '11 at 11:08
  • in logcat I don't have any output,i also made some Log.v, into the code but I also don't see those. – safari Oct 25 '11 at 13:31
  • @safari use the full class name of the broadcast receiver: `` (see my updated answer) – Volo Oct 25 '11 at 13:44
  • i tried this, i still don't see anything on my logcat about my receiver – safari Oct 25 '11 at 13:50
  • @safari Is your broadcast `onReceive` method being called? Have you tried to put `Log.d` at the very beginning of `onReceive` method? Also did you try [sample project](http://mobiforge.com/sites/mobiforge.com/files/SMSMessaging.zip) provided in the tutorial you refer to? – Volo Oct 25 '11 at 14:13
  • yes I did, but I don't get log.d output but i see nothing in logcat – safari Oct 25 '11 at 14:21
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/4506/discussion-between-idolon-and-safari) – Volo Oct 25 '11 at 14:47
  • 1
    Apps like Hancent sms and go sms hacked a way to be at the top of the priority list. – gauglerb Jan 01 '14 at 21:41
0

gauglerb pointed me in the right direction here with his comment for the accepted answer and I think I should share my findings.

Handcent has indeed been a bad boy and are not letting any other apps receive messages when it is installed.

Fortunately there is an easy solution if you don't want to uninstall Handcent:
In the Application settings of Handcent there is an option to make Handcent the default Messaging application. If this is disabled, messages can come through to other receivers.

EmKay
  • 1,089
  • 1
  • 13
  • 28
0

Give permission in MainActivity in onCreate method. It will work.

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.READ_CONTACTS, Manifest.permission.READ_SMS, Manifest.permission.SEND_SMS, Manifest.permission.RECEIVE_SMS}, 10);

}
jrswgtr
  • 2,287
  • 8
  • 23
  • 49
0
for (int i=0; i<pdus.length; i++)

instead of msgs.length, pdus contains the real sms messages.

Carnal
  • 21,744
  • 6
  • 60
  • 75
  • That's how I implemented my SMS Receiver, if you want I can post my implementation tomorrow. – Carnal Oct 25 '11 at 18:16