2

I am using contentobserver to monitor SMS. It all works fine. When I try to save these SMS to a database, it shows an error error near "t" syntax error for a particular SMS. When I delete this particular SMS there is no problem. After installing, it shows all the messages correctly in order. But the error is sent to the end of my arraylist. Also the SMS sent from my phone after this are updated in between the list, not on the last position. Please help.

adapter = new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_list_item_1,list);
setListAdapter(adapter);
data = Incoming_outgoing_smsActivity.this.openOrCreateDatabase("Messages", MODE_PRIVATE, null);
data.execSQL("CREATE TABLE IF NOT EXISTS recor(text varchar(300));");
Cursor cur = data.rawQuery("SELECT * FROM recor", null);
while(cur.moveToNext()) {
  String content = cur.getString(cur.getColumnIndex("text"));
  backward_list.add(content);
  list.add(content);
}
adapter.notifyDataSetChanged();
Cursor cursor = getContentResolver().query(Uri.parse("content://sms"), null, null, null, null);
while(cursor.moveToNext()) {
  String number = cursor.getString(cursor.getColumnIndex("address"));
  String[] projection = new String[] {ContactsContract.PhoneLookup.DISPLAY_NAME};
  Uri contactUri = Uri.withAppendedPath(ContactsContract.PhoneLookup.CONTENT_FILTER_URI, Uri.encode(number));
  Cursor cursor_name = getContentResolver().query(contactUri, projection, null, null, null);
  String body = cursor.getString(cursor.getColumnIndex("body"));
  String type = cursor.getString(cursor.getColumnIndex("type"));
  long date1= cursor.getLong(cursor.getColumnIndex("date"));
  SimpleDateFormat formatter = new SimpleDateFormat("dd-MM-yyyy hh:mm:ss.SSS");
  Calendar calendar = Calendar.getInstance();
  calendar.setTimeInMillis(date1);
  try {
    int n = cursor.getInt(cursor.getColumnIndex("type"));
    switch (n) {
      case 1:
        String message = "FROM "+number+"\n"+formatter.format(calendar.getTime())+"\n"+"Message:-"+body;
        if(backward_list.contains(message)) {
          continue;
        } else {
          list.add(message);
          backward_list.add(message);
          data.execSQL("INSERT INTO recor VALUES('"+message+"')");
        }
        break;
      case 2:
        String messag = "TO "+number+"\n"+formatter.format(calendar.getTime())+"\n"+"Message:-"+body;
        if(backward_list.contains(messag)) {
          continue;
        } else {
          list.add(messag);
          backward_list.add(messag);
          data.execSQL("INSERT INTO recor VALUES('"+messag+"')");
        }
        break;
      default:
        break;
    }
  }
  catch (Exception e) {
    // TODO: handle exception
    Toast.makeText(getApplicationContext(), e.getMessage(), Toast.LENGTH_LONG).show();
    continue;
  }
}

The above code saves the current SMS in your inbox to the database. The code below is used to update your inbox when a new SMS arrives. It does toast the arrived messages but doesn't insert them into the database.

data = Incoming_outgoing_smsActivity.this.openOrCreateDatabase("Messages", MODE_PRIVATE, null);
data.execSQL("CREATE TABLE IF NOT EXISTS recor(text varchar(300));");
super.onChange(selfChange);
Cursor cursor = getContentResolver().query(Uri.parse("content://sms"), null, null, null, null);
while(cursor.moveToNext()) {
  String number = cursor.getString(cursor.getColumnIndex("address"));
  String[] projection = new String[] {
    ContactsContract.PhoneLookup.DISPLAY_NAME};
    Uri contactUri = Uri.withAppendedPath(ContactsContract.PhoneLookup.CONTENT_FILTER_URI, Uri.encode(number));
    Cursor cursor_name = getContentResolver().query(contactUri, projection, null, null, null);
    if(cursor_name.moveToFirst()) {
      name = cursor_name.getString(cursor_name.getColumnIndex(ContactsContract.PhoneLookup.DISPLAY_NAME));
    }
    String body = cursor.getString(cursor.getColumnIndex("body"));
    String type = cursor.getString(cursor.getColumnIndex("type"));
    long date1= cursor.getLong(cursor.getColumnIndex("date"));
    SimpleDateFormat formatter = new SimpleDateFormat("dd-MM-yyyy hh:mm:ss.SSS");
    Calendar calendar = Calendar.getInstance();
    calendar.setTimeInMillis(date1);
    int n = cursor.getInt(cursor.getColumnIndex("type"));
    switch (n) {
      case 1:
        String message = "FROM "+number+"\n"+formatter.format(calendar.getTime())+"\n"+"Message:-"+body;
        if(backward_list.contains(message)) {
          continue;
        } else {
          list.add(message);
          backward_list.add(message);
          data.execSQL("INSERT INTO recor VALUES('"+message+"')");
        }
        break;
      case 2:
        String messag = "TO "+number+"\n"+formatter.format(calendar.getTime())+"\n"+"Message:-"+body;
        if(backward_list.contains(messag)) {
          continue;
        } else {
          list.add(messag);
          backward_list.add(messag);
          data.execSQL("INSERT INTO recor VALUES('"+messag+"')");
        }
        break;
      default:
        break;
    }
dda
  • 6,030
  • 2
  • 25
  • 34
Akhil
  • 303
  • 2
  • 7
  • 19

1 Answers1

1

At a guess there was some sort of restricted character/word in the one SMS.

You should use prepared statements to take care of the issue.

See this SO Answer for an example.

For your second issue about the order of display, change/use an ORDER BY in your query to set the proper order.

Community
  • 1
  • 1
Barak
  • 16,318
  • 9
  • 52
  • 84