0

This is the only thing that is unsolved for our project. Assuming the app is already installed on someone's phone. Then we want to launch the app by sending him/her a message that matched the message's body parameter in our program. I followed some codes but seems like all of those that I tried did not work.Please respect. Thank you for those who are generous to help.

public class SmsReceiver extends BroadcastReceiver{

    public static String trigger = "";

    @Override
    public void onReceive(Context context, Intent intent) {
        Bundle bundle = intent.getExtras();
        SmsMessage[] msgs = null;
        String str="";
        String num="";
        Intent i = new Intent();
        i.setClassName("gavadev.com.autolaunch", "gavadev.com.autolaunch.NewAct");
        i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);



        if(bundle != null){
            Object[] pdus = (Object[]) bundle.get("pdus");
            msgs= new SmsMessage[pdus.length];


            for(int j =0 ;j<pdus.length; j++) {

                trigger = msgs[j].getMessageBody().toString();
                num = msgs[j].getOriginatingAddress();

                str += trigger;



            }
            Toast.makeText(context,"From: "+num+ " Message: " +trigger,Toast.LENGTH_LONG).show();
            if((trigger.equals("themessage"))&&(num.equals("thenumber"))){
                context.startActivity(i);

            }
        }
    }
}

Here is my Manifest.xml

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="gavadev.com.autolaunch">

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

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity
            android:name=".MainActivity"
            android:label="@string/app_name"
            android:theme="@style/AppTheme.NoActionBar">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

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

        <activity
            android:name=".NewAct"
            android:label="@string/title_activity_new"
            android:theme="@style/AppTheme.NoActionBar"></activity>
    </application>

</manifest>

*I did nothing on the MainActivity.class

NOOBIE
  • 1
  • 1
  • 1
    What does "did not work" mean? What were your **specific** symptoms? – CommonsWare Feb 21 '16 at 15:29
  • I installed it first in my phone. programmed "HOOD" as the trigger message, and my groupmate's phone number as the number. I asked him to send me "HOOD" but nothing happens. The app didn't launch. =( – NOOBIE Feb 21 '16 at 15:33
  • When you used your debugger, or `Log` statements, to see what is going on in your code, what did you learn? – CommonsWare Feb 21 '16 at 15:36
  • I'll try to take a look at it and will try to understand.I just build the apk and installed it on my phone because the emulator runs so slow. I'm using Android Studio btw. Thank you so much. – NOOBIE Feb 21 '16 at 15:40

2 Answers2

0

Why don't you debug and see why it's not calling the intent? As far as you declare correctly your SMS broadcast receiver it should work without any problem.

However, let me say why I think all this is a bad idea:

  1. These permissions scare people a lot and are very sensitive to use:

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

    You should just use <uses-permission android:name="android.permission.RECEIVE_SMS"/> permission only for that. Look at this simple example, it wisely suggest to use DDMS to test or mock SMS messages for your purpose. Thus you could debug that method and see how it works.

  2. You will definitely have a lot of problems to use your app with Android Marshmallow and upwards. It require runtime permissions and those are the dangerous type ones which require to ask the user at runtime. Unless you explain why you need those permissions and why the app cannot work without them, they will always deny those permissions.

  3. If you have a simple backend, why don't you just use Push Messages for that purpose?

If you still decide to use this, I would increase your receiver's priority in your manifest:

<receiver android:name=".SmsReceiver" android:exported="true" > 
  <intent-filter android:priority="1000"> 
    <action android:name="android.provider.Telephony.SMS_RECEIVED" />
  </intent-filter> 
</receiver>
Community
  • 1
  • 1
GoRoS
  • 5,183
  • 2
  • 43
  • 66
  • So that means it is okay to just use and only use permission RECEIVE_SMS?I honestly just known basics of Android Programming as we've been taught in school. I've tried a lot of research that are possibly related to my problem. I've been following each step carefully. as of now can't find a solution. (cries) – NOOBIE Feb 21 '16 at 15:52
  • I've updated the point 1 of my answer. All of us were a beginner once, don't give up. Try to read and research as much as possible before asking here, it will help you a lot and you will learn faster! :) – GoRoS Feb 21 '16 at 16:04
  • @GoRoS The Google administrators for the Google Play Store consider the RECEIVE_SMS permission to be dangerous. As a result, an app that contains the permission will be rejected. Then the developer has to submit a form to Google Play administrators for approval. Other developers have mentioned the process is awful with feedback taking weeks, receiving only general feedback and often outright rejections are received with no explanations. Any ideas on how to avoid? – AJW Jul 15 '20 at 15:02
0

Use this code:

     public class SmsListener extends BroadcastReceiver{

        private SharedPreferences preferences;

        @Override
        public void onReceive(Context context, Intent intent) {
            // TODO Auto-generated method stub

            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();
                            String match="String to match from TEXT MESSAGE eg.HOOD";
if(msgBody.toLowerCase().contains(match.toLowerCase()))
{
Intent launchIntent = getPackageManager().getLaunchIntentForPackage("YOUR PACKAGE NAME OF APP TO LAUNCH");
startActivity(launchIntent);
}


                        }
                    }catch(Exception e){
    //                            Log.d("Exception caught",e.getMessage());
                    }
                }
            }
        }
    }

Note: In your manifest file add the BroadcastReceiver-

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

Add this permission:

<uses-permission android:name="android.permission.RECEIVE_SMS" />
  • @ "Intent launchIntent = getPackageManager().getLaunchIntentForPackage("YOUR PACKAGE NAME OF APP TO LAUNCH"); startActivity(launchIntent);" error in getPackageManager and startActivity and variables may seem to unused. Thanks anyways. – NOOBIE Feb 22 '16 at 14:50
  • Have u inserted your package name name in double quotes.? – user5783725 Feb 23 '16 at 03:51