2

The title is not as specific as I would like, so here are more details. I am trying to make an app that auto-replies a message to any incoming texts while driving. This app is 100% working thanks to this community already, but I would like to expand this. My goal is to get the number of the sender (I already have this) but then save it separately somehow so that it does auto-reply to the same person twice (or more) in a row.

public class Receiver extends BroadcastReceiver {
    public static String number = "";
    public static String sms = "";

    @Override
    public void onReceive(Context context, Intent intent) {
        Bundle bundle = intent.getExtras();
        SmsMessage[] msgs;

        if (bundle != null) {
            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]);
                number = msgs[i].getOriginatingAddress();
                sms = msgs[i].getMessageBody();
            }

            if (Main.serviceBroadcast) {
                String sent = "SMS_SENT";
                PendingIntent pi = PendingIntent.getBroadcast(context, 0, new Intent(sent), 0);
                SmsManager sm = SmsManager.getDefault();

                if (Main.serviceReply && !number.equals("")) {
                    sm.sendTextMessage(number, null, Main.reply, pi, null);
                    Toast.makeText(context, "replied to " + number, Toast.LENGTH_SHORT).show();
                }
            }
        }
    }
}

If you would like the full source to this program please visit http://mellowdev.net

sociallymellow
  • 155
  • 1
  • 2
  • 14
  • Can you currently get the number to a string? – NightSkyCode Jul 19 '13 at 05:09
  • Well you do have the `number`. You can just use `sm.sendTextMessage(number, null, Main.reply, pi, null);` as many times as you want. If i understood wrong please elaborate on question. – Ankit Aggarwal Jul 19 '13 at 05:30
  • @AnkitAggarwal I am wondering how to save only the previous and current numbers (seperate). For example I want the following, if Joe sends me a text, it will reply "I am driving now". If he sends me another text it will NOT reply again. If Sarah sends me a text, however, it will reply to her, "I am driving now". Hope this makes more sense. – sociallymellow Jul 19 '13 at 06:03
  • @NightSky I do have the number to a string right now. – sociallymellow Jul 19 '13 at 06:04
  • Why not save it to an array list? – NightSkyCode Jul 19 '13 at 06:32
  • @NightSky How would I go about making an array? It would be best if I could keep like the last 10 numbers or something. And then another array for keeping lists such as important numbers or something else. – sociallymellow Jul 19 '13 at 07:52

2 Answers2

0

I know this might not be the best answer. But what I did in my application was that I created a SQLite database which stored these numbers, as I actually required them later too. So whenever a new message was received I just checked through the database, and executed the SMS code if the number was not in the database, and added it to the database.

Also since the phone knows that the person is driving, you must have initially put some toggle. So when the driving mode is removed you can delete the data in the table.

Ankit Aggarwal
  • 2,367
  • 24
  • 30
  • This method seems very good, although not as simple as I would like, I would love to learn how to use SQLite. I could also you this database for things like a list of people who the user would want to be alerted if they messaged them etc. I just feel like this has the most potential. Would you happen to have any links for help on creating a database? Thanks! – sociallymellow Jul 19 '13 at 07:49
  • Also, I do have a toggle that turns the service on and off. If you would like to download the app (in beta now) here you go: https://dl.dropboxusercontent.com/u/57594406/downloads/drive%20mode/dm_beta.apk – sociallymellow Jul 19 '13 at 07:55
  • `http://www.androidhive.info/2011/11/android-sqlite-database-tutorial/` this has exactly what you need :) Experiment with it. – Ankit Aggarwal Jul 19 '13 at 09:50
  • Don't do a database unless you want this to be a web service. Lwayy to complicated for what you want. Create a person class and save it to an object array list. When Iget home I'll write u upba sample – NightSkyCode Jul 19 '13 at 18:51
0
String number = "";
String person = "";
//Create empty list this way
ArrayList<String> phoneNumbers = new ArrayList<String>();
ArrayList<String> personNames = new ArrayList<String>();

// add them together and the number array and person array will always be in sync 
// when you call them

//Add to your list this way
number.add(phoneNumbers);
personNames.add(person);

//get from your list this way for example or you can add them to a list view, etc..
// you use get to get the string stored in the list at that position.
number = phoneNumbers.get(2);
person = personNames.get(2);

//or you can do a for loop to check them all to see if they match something
for( int i = 0; i < phoneNumbers.length - 1; i++)
{
person = personNames.get(i);

   if (person.matches("Nicolas")) {
// do something with it here
 }

 }

Now you can easily save and get these array list looking at a question I asked a year ago. Save an ArrayList to File on android

There you go!

Community
  • 1
  • 1
NightSkyCode
  • 1,141
  • 2
  • 16
  • 33
  • Thank you for this answer, I got it working perfectly in my app but was wondering how to get the contact name? I have the number already via number = msgs[i].getOriginatingAddress(); but am not sure how to see if that number is in the person contacts etc. I will try to search for this next. Thanks again! – sociallymellow Aug 11 '13 at 19:49