Home » Android » android – Is it possible to open my app through inbox SMS?

android – Is it possible to open my app through inbox SMS?

Posted by: admin June 16, 2020 Leave a comment

Questions:

I have a doubt in opening my app through inbox SMS (came from a particular number), is that possible? Please guide me or give some tips to fix tje problem.

How to&Answers:

You can open your app from a link contained in the SMS.

Create an intent-filter for your domain like this :

<activity
    android:name="ActivityFromSMS" >
    <intent-filter>
        <action android:name="android.intent.action.VIEW" />
        <category android:name="android.intent.category.DEFAULT" />
        <data
            android:host="mydomain.com"
            android:scheme="http" />
    </intent-filter>
</activity>

Then, clicking on a link to http://mydomain.com/... (that can be in an SMS) will prompt the user to open it with your app.

Answer:

generally you can listen for incomming sms and then just look for number, it came from or it’s content, then you can start your desired activity using intent.

Code should look like here. You will need to change the onReceive method

Answer:

First you need to create a broadcast listener for incoming sms. In its onReceive method you can use the following code :

try
{
    Log.d("In try", "In Try");

    new Handler().postDelayed(new Runnable()
    {
        public void run()
        {

            Toast.makeText(mycontext, "Number"+incommingNumber, Toast.LENGTH_SHORT).show();
                        cursor1= db.rawQuery("select phno from List where phno=?" , new String[] {""+incommingNumber} );
                        if(cursor1.moveToFirst())
                        {
                            do{
                                String pno = cursor1.getString(0);
                                Toast.makeText(mycontext, "phno: "+pno, Toast.LENGTH_SHORT).show();
                                Log.d("DB CHEK", "phno: "+pno);

                                dbHelper_sms = new SQLDatabase(mycontext);
                                db_sms = dbHelper_sms.getWritableDatabase();
                                val_sms=new ContentValues();

                                //Get SMS body
                                val_sms.put("body",cursor.getString(cursor.getColumnIndex("Body")) );
                                val_sms.put("phno",incommingNumber);
                                db_sms.insert("SmsList",null,val_sms);



                                //Delete SMS from inbox 
                                mycontext.getContentResolver().delete(Uri.parse("content://sms/conversations/"+cursor.getString(cursor.getColumnIndex("thread_id"))), null, null);


                                //Log.d("Waiting", "Do Nothing");
                                Toast.makeText(mycontext, "WHITELIST SMS DELETED : "+cursor.getString(cursor.getColumnIndex("Body")), Toast.LENGTH_SHORT).show();
                            }while(cursor1.moveToNext());
                        }

                    }
                    cursor.close();
                    cursor1.close();
                    db.close();
                    dbHelper.close();
         }
    }, 2000);

 }
 catch(Exception e)
 {
     e.printStackTrace();
 }

now using above code you get the body of an incoming sms and you can also delete it from the inbox to erase its trace. After you get the body , its upto to you how you want to use it. You want to open an app programatically? I suggest check this : How do I programmatically launch a specific application in Android?

Hope this was useful.