Search code examples
androidgpsjava-web-start

How to Send Text Message to Android and Start Application or Application Activity


My question is very simple and I know the function can be preformed. I want to start activity via text message like "WHERE IS MY DROID" how can this be done? Please provide any information. I think SMSReceiver BroadcastReceiver is in the correct direction but I am not sure.


Solution

  • You need to register an Broadcast Receiver in AndroidManifest.xml for Receiving SMS_RECIVERD Broadcast as:

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

    Add permission in AndroidManifest:

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

    in your Broadcast Receiver code start your Application as:

    public class SMSReceiver extends BroadcastReceiver {
        @Override
        public void onReceive(Context context, Intent intent) {
    if (intent.getAction().equals("android.provider.Telephony.SMS_RECEIVED")){      
       //context.startService(new Intent(context, YourService.class));
    
       //Start activity as:
        Intent intent24 = new Intent(Intent.ACTION_MAIN).addCategory(
        Intent.CATEGORY_LAUNCHER).setClassName("YOUR_PACKAGE_NAME",
        "com.YOUR_PACKAGE_NAME..YOURACTIVITY_NAME").addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
        .addFlags(Intent.FLAG_FROM_BACKGROUND).setComponent(new ComponentName("YOUR_PACKAGE_NAME",
        "com.YOUR_PACKAGE_NAME..YOURACTIVITY_NAME"));
        context.startActivity(intent24);
       }
     }
    }
    

    NOTE : For starting your Activity from background you need to set Intent.FLAG_ACTIVITY_NEW_TASK and Intent.FLAG_FROM_BACKGROUND flags in intent for starting activity from background.