1. Are you ready for the Galaxy S20? Here is everything we know so far!

Please Help! (SMS receiver related)

Discussion in 'Android Development' started by mike_suopys, Jun 6, 2019.

  1. mike_suopys

    mike_suopys Newbie
    Thread Starter

    Hello everyone,

    Let me give you a brief explanation of my situation.

    I'm a mediocre programer. I only had taken two formal classes in computer programing (in the JAVA language) in my life. And am very new to android (phone) app development. About 4 months ago I decided to develop an app for encrypting sms messages. Now the app is nearly 90% complete, I still have one nagging problem I'm trying to solve:

    How can I make the app receive SMS messages in the background when the app is no longer in the foreground?

    e.g. Suppose someone has google Crome open in the foreground and is receiving a sms message. I simply want my app to say (perhaps implemented with the "Toast.maketext(...).show()" method), "You have received and encrypted messages from +15555215554".

    My app already is able to do this when running in the foreground. But I want to do it in the background. I just don't know what this is called. So I'm uncertain what to google search. So far I've googled "android studio sms receiver" and "android studio sms service" and "android studio receive SMS background", but neither results have helped me. Here are my manifest and broadcast recover classes if that helps:

    Here's my manifest code:
    Code (Text):
    1.  
    2. <?xml version="1.0" encoding="utf-8"?>
    3. <manifest xmlns:android="http://schemas.android.com/apk/res/android"
    4.     package="com.example.sendreadsmsexample">
    5.  
    6.     <uses-permission android:name="android.permission.SEND_SMS" />
    7.     <uses-permission android:name="android.permission.READ_SMS" />
    8.     <uses-permission android:name="android.permission.RECEIVE_SMS" />
    9.     <uses-permission android:name="android.permission.READ_PHONE_STATE" />
    10.  
    11.     <application
    12.         android:allowBackup="true"
    13.         android:icon="@mipmap/ic_launcher"
    14.         android:label="@string/app_name"
    15.         android:supportsRtl="true"
    16.         android:theme="@style/AppTheme">
    17.         <activity android:name=".ManMess"
    18.             android:label="Manage Messages"
    19.             android:parentActivityName=".MainActivity"></activity>
    20.         <activity android:name=".ManKeys"
    21.             android:label="Manage Keys"
    22.             android:parentActivityName=".MainActivity"/>
    23.         <activity android:name=".About"
    24.             android:label="About"
    25.             android:parentActivityName=".MainActivity"/>
    26.         <activity android:name=".MainActivity" android:windowSoftInputMode="adjustPan" >
    27.             <intent-filter>
    28.                 <action android:name="android.intent.action.MAIN" />
    29.  
    30.                 <category android:name="android.intent.category.LAUNCHER" />
    31.             </intent-filter>
    32.         </activity>
    33.  
    34.         <receiver android:name=".SmsBroadcastReceiver">
    35.             <intent-filter>
    36.                 <action android:name="android.provider.Telephony.SMS_RECEIVED" />
    37.             </intent-filter>
    38.         </receiver>
    39.  
    40.         <service android:name=".MessageService"
    41.             android:enabled="true"/>
    42.  
    43.     </application>
    44.  
    45. </manifest>
    46.  
    47.  
    Here's my SmsBroadcastReceiver:
    Code (Text):
    1.  
    2. package com.example.sendreadsmsexample;
    3.  
    4. import android.content.BroadcastReceiver;
    5. import android.content.Context;
    6. import android.content.Intent;
    7. import android.os.Bundle;
    8. import android.telephony.SmsMessage;
    9. import android.text.method.ScrollingMovementMethod;
    10. import android.widget.TextView;
    11. import android.widget.Toast;
    12.  
    13. import java.security.InvalidKeyException;
    14. import java.security.NoSuchAlgorithmException;
    15.  
    16. import javax.crypto.BadPaddingException;
    17. import javax.crypto.IllegalBlockSizeException;
    18. import javax.crypto.NoSuchPaddingException;
    19.  
    20.  
    21. public class SmsBroadcastReceiver extends BroadcastReceiver
    22. {
    23.     @Override
    24.     public void onReceive(Context context, Intent intent)
    25.     {
    26.         MainActivity mainActivity = new MainActivity();
    27.         StringManager stringManager = new StringManager();
    28.         MessageLogger messageLogger = new MessageLogger();
    29.         KeyManager keyManager = new KeyManager();
    30.  
    31.         if(intent.getAction().equals("android.provider.Telephony.SMS_RECEIVED"))
    32.         {
    33.             Bundle bundle = intent.getExtras();
    34.             SmsMessage[] msgs = null;
    35.             String msg_from = "";
    36.             String msgBody = "";
    37.  
    38.             if (bundle != null)
    39.             {
    40.  
    41.                 try
    42.                 {
    43.                     Object[] pdus = (Object[]) bundle.get("pdus");
    44.  
    45.                     msgs = new SmsMessage[pdus.length];
    46.  
    47.                     for(int i=0; i<msgs.length; i++)
    48.                     {
    49.                         msgs[i] = SmsMessage.createFromPdu((byte[])pdus[i]);
    50.                         msg_from = msgs[i].getOriginatingAddress();
    51.                         msgBody = msgs[i].getMessageBody();
    52.  
    53.                         if (MainActivity.mThis != null )
    54.                         {
    55.                             if( stringManager.isKey(msgBody) && keyManager.hasKey(msg_from, context) == false )
    56.                             {
    57.                                 Toast.makeText(context,"Recieved key piece: " + stringManager.getKeyNumber(msgBody) + " from " + msg_from + ".", Toast.LENGTH_LONG).show();
    58.                                 keyManager.addKeyEntry(msg_from, msgBody, context);
    59.                                 if( keyManager.hasKey(msg_from, context) )
    60.                                 {
    61.                                     Toast.makeText(context,"A secure link has been established! You can now send encrypted messages to " + msg_from + ".", Toast.LENGTH_LONG).show();
    62.                                 }
    63.                             }
    64.                             else
    65.                             {
    66.                                 ((TextView) MainActivity.mThis.findViewById(R.id.txtFrom)).setText("Sender: " + msg_from + " | Recieved Message Packet # " + stringManager.getNumber(msgBody));
    67.                                 messageLogger.addEntry(msgBody, msg_from, context);
    68.  
    69.                                 if(messageLogger.gotMessageByID(msg_from,stringManager.getNumberID(msgBody),context))
    70.                                 {
    71.                                     FileManager fileManager = new FileManager();
    72.                                     MessageHistory messageHistory = new MessageHistory(msg_from, context);
    73.                                     int ID = stringManager.getNumberID(msgBody);
    74.                                     RSAUtil rsa = new RSAUtil();
    75.  
    76.                                     String text = "";
    77.  
    78.                                     text = messageLogger.getMessageByID(msg_from, ID, context);
    79.                                     messageHistory.addEntry(text);
    80.                                     String[] entries = messageHistory.getEntries();
    81.  
    82.                                     for(int j = 0; j < entries.length; j++)
    83.                                     {
    84.                                         try
    85.                                         {
    86.                                             entries[j] = "> " + rsa.decrypt(entries[j], fileManager.getContent("myPrivKey.txt", context)) + "\n";
    87.                                         }
    88.                                         catch (IllegalBlockSizeException e)
    89.                                         {
    90.                                             e.printStackTrace();
    91.                                         }
    92.                                         catch (InvalidKeyException e)
    93.                                         {
    94.                                             e.printStackTrace();
    95.                                         }
    96.                                         catch (BadPaddingException e)
    97.                                         {
    98.                                             e.printStackTrace();
    99.                                         }
    100.                                         catch (NoSuchAlgorithmException e)
    101.                                         {
    102.                                             e.printStackTrace();
    103.                                         }
    104.                                         catch (NoSuchPaddingException e)
    105.                                         {
    106.                                             e.printStackTrace();
    107.                                         }
    108.                                     }
    109.                                     ((TextView) MainActivity.mThis.findViewById(R.id.txtFull)).setMovementMethod(new ScrollingMovementMethod());
    110.                                     ((TextView) MainActivity.mThis.findViewById(R.id.txtFull)).setText(stringManager.unite(entries));
    111.                                 }
    112.                             }
    113.                         }
    114.                     }
    115.                 }
    116.                 catch(Exception e)
    117.                 {
    118.  
    119.                 }
    120.             }
    121.         }
    122.     }
    123. }
    124.  
     


  2. mike_suopys

    mike_suopys Newbie
    Thread Starter

    Alright everyone....feel like I'm talking to myself here, but I know there are readers. I Finally found the solution to my problem!

    If you want to log and receive notifications of sms messages while your app in the background, you're program would have to read the sms inbox, which every android phone has.

    Here's an example of some code which implements the sms inbox.
    https://www.androidauthority.com/how-to-create-an-sms-app-721438/
    Code (Text):
    1.  
    2. public class MainActivity extends AppCompatActivity {
    3.  
    4.    ArrayList<String> smsMessagesList = new ArrayList<>();
    5.    ListView messages;
    6.    ArrayAdapter arrayAdapter;
    7.    private static final int READ_SMS_PERMISSIONS_REQUEST = 1;
    8.  
    9.    @Override
    10.    protected void onCreate(Bundle savedInstanceState) {
    11.        super.onCreate(savedInstanceState);
    12.        setContentView(R.layout.activity_main);
    13.        messages = (ListView) findViewById(R.id.messages);
    14.        input = (EditText) findViewById(R.id.input);
    15.        arrayAdapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, smsMessagesList);
    16.        messages.setAdapter(arrayAdapter);
    17.        if (ContextCompat.checkSelfPermission(this, Manifest.permission.READ_SMS)
    18.                != PackageManager.PERMISSION_GRANTED) {
    19.           getPermissionToReadSMS();
    20.        } else {
    21.           refreshSmsInbox();
    22.    }
    23. }
    24.  
    and...
    Code (Text):
    1.  
    2. public void refreshSmsInbox() {
    3.    ContentResolver contentResolver = getContentResolver();
    4.    Cursor smsInboxCursor = contentResolver.query(Uri.parse("content://sms/inbox"), null, null, null, null);
    5.    int indexBody = smsInboxCursor.getColumnIndex("body");
    6.    int indexAddress = smsInboxCursor.getColumnIndex("address");
    7.    if (indexBody < 0 || !smsInboxCursor.moveToFirst()) return;
    8.    arrayAdapter.clear();
    9.    do {
    10.        String str = "SMS From: " + smsInboxCursor.getString(indexAddress) +
    11.                   "\n" + smsInboxCursor.getString(indexBody) + "\n";
    12.        arrayAdapter.add(str);
    13.    } while (smsInboxCursor.moveToNext());
    14. }
    15.  
    16.  
    That's what the problem was. I didn't know that an sms inbox existed.
     
    23tony likes this.
  3. 23tony

    23tony Well-Known Member

    There are definitely readers - but this one had nothing to offer. Thank you for sharing the solution you found!
     
Loading...

Share This Page

Loading...