Home » Android » android: register application to receive sms

android: register application to receive sms

Posted by: admin June 15, 2020 Leave a comment

Questions:

how can I register my application so that when I receive a sms my app appears in dialog Complete action using. I have put in an intent code

<intent-filter>
            <action android:name="android.intent.action.VIEW"/>
            <action android:name="android.intent.action.SENDTO"/>
            <category android:name="android.intent.category.DEFAULT"/>
            <category android:name="android.intent.category.BROWSABLE"/>
            <data android:scheme="sms"/>
</intent-filter>

but it’s not working… should I use receiver? Note that the activity in which I’ve inserted this code is not main activity. Thanks

How to&Answers:

Use the following code.

<activity android:name=".SMSNewActivity" >
            <intent-filter>
                <action android:name="android.intent.action.VIEW" />
                <category android:name="android.intent.category.DEFAULT" />
                <data android:mimeType="vnd.android-dir/mms-sms" />
            </intent-filter>
            <intent-filter>
                <action android:name="android.intent.action.VIEW" />
                <action android:name="android.intent.action.SENDTO" />
                <category android:name="android.intent.category.DEFAULT" />
                <category android:name="android.intent.category.BROWSABLE" />
                <data android:scheme="sms" />
                <data android:scheme="smsto" />
            </intent-filter>
            <intent-filter>
                <action android:name="android.intent.action.SEND" />
                <category android:name="android.intent.category.DEFAULT" />
                <data android:mimeType="text/plain" />
            </intent-filter>
        </activity>
        <activity android:name=".SMSMainListActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
             <intent-filter>
                <action android:name="android.intent.action.SEARCH" />
                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>

Answer:

Its not well documented in doc.

You can find info on
AndDev

Here are some extracts:

  1. You must use following permission, include it in your AndroidManifest

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

2 You must not declare an intent filter in your activity for the same but filter it in a receiver, include following your manifest

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

3 Create a class that extends android.content.IntentReceiver and override the onReceiveIntent method of the class listen for action android.provider.Telephony.SMS_RECEIVED not part of SDK

heres some more code extract:

    // @Override
public void onReceiveIntent(Context context, Intent intent) {
    if (intent.getAction().equals(ACTION)) {
        // if(message starts with SMStretcher recognize BYTE)
        StringBuilder sb = new StringBuilder();

        /* The SMS-Messages are 'hiding' within the extras of the Intent. */
        Bundle bundle = intent.getExtras();
        if (bundle != null) {
            /* Get all messages contained in the Intent*/
            SmsMessage[] messages = 
                Telephony.Sms.Intents.getMessagesFromIntent(intent);

            /* Feed the StringBuilder with all Messages found. */
            for (SmsMessage currentMessage : messages){
                sb.append("Received compressed SMSnFrom: ");
                /* Sender-Number */
                sb.append(currentMessage.getDisplayOriginatingAddress());
                sb.append("n----Message----n");
                /* Actual Message-Content */
                sb.append(currentMessage.getDisplayMessageBody());
            }
        }
        /* Logger Debug-Output */
        Log.i(LOG_TAG, "[SMSApp] onReceiveIntent: " + sb);

        /* Show the Notification containing the Message. */ 
        Toast.makeText(context, sb.toString(), Toast.LENGTH_LONG).show();

Answer:

See this free chapter from the book Beginning Android Application Development, which show in details how to receive (and send) SMS’s:

http://p2p.wrox.com/content/articles/creating-android-apps-send-and-receive-sms-messages-and-send-email

Answer:

You will need this

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

in the AndroidManifest.xml