Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

android: register application to receive sms

Tags:

android

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

like image 223
Buda Gavril Avatar asked Nov 23 '10 14:11

Buda Gavril


2 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>
like image 129
Vinothkumar Arputharaj Avatar answered Nov 13 '22 22:11

Vinothkumar Arputharaj


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" />
  1. 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>
  1. 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

Here's 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();
like image 32
Shardul Avatar answered Nov 13 '22 20:11

Shardul