Search code examples
androidservicesmssmsmanager

service does not work for receiving incoming messages


Decided to make a simple service which displays the received SMS , found on this website example , sold it, but I can not understand why it is not working.

Even after giving manifest priority . Can you tell what the problem is , that's my main class , and this class is responsible for the mapping of the SMS.

MainClass:

public class BroadcastNewSms extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
}
 }

Service incoming class:

public class IncomingSms extends BroadcastReceiver {

private final String TAG = this.getClass().getSimpleName();

@Override
public void onReceive(Context context, Intent intent)
{
    if(intent.getAction().equals("android.provider.Telephony.SMS_RECEIVED")){
        Bundle bundle = intent.getExtras();           //---get the SMS message passed in---
        SmsMessage[] msgs = null;
        String msg_from;
        if (bundle != null){
            //---retrieve the SMS message received---
            try{
                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]);
                    msg_from = msgs[i].getOriginatingAddress();
                    String msgBody = msgs[i].getMessageBody();
                    Toast.makeText(context,msgBody,Toast.LENGTH_LONG).show();
                }
            }catch(Exception e){
            //                            Log.d("Exception caught",e.getMessage());
            }
        }
    }

    //---get the SMS message passed in---
    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();
    }

}

}

Manifest:

<?xml version="1.0" encoding="utf-8"?>
       <manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.example.SmsService"
      android:versionCode="1"
      android:versionName="1.0">
     <uses-sdk android:minSdkVersion="14"/>
     <application android:label="@string/app_name" android:icon="@drawable/ic_launcher">
    <activity android:name="BroadcastNewSms"
              android:label="@string/app_name">
        <intent-filter>
            <action android:name="android.intent.action.MAIN"/>
            <category android:name="android.intent.category.LAUNCHER"/>
        </intent-filter>
        <receiver android:name="com.example.SmsService.IncomingSms">
            <intent-filter android:priority="2147483647">
                <action android:name="android.provider.Telephony.SMS_RECEIVED" />
            </intent-filter>
        </receiver>
    </activity>
</application>
<uses-permission android:name="android.permission.RECEIVE_SMS"></uses-permission>
<uses-permission android:name="android.permission.READ_SMS" />
<uses-permission android:name="android.permission.SEND_SMS"></uses-permission>

Sorry for my english.


Solution

  • You have the <receiver> tag inside the <activity> tag. It needs to be outside the <activity> tag.