Search code examples
androidmessage

Perform Text analysis on Incoming messages


I am creating an android application which will detect person mood through text messages, it will search for emo's and keywords that depict emotions, I have done else part, but now I am coming to messages, I have made a condition that my application will check person mood after 10 messages are sent and search keywords and set his/her mood.

import android.content.Context;
import android.database.Cursor;
import android.net.Uri;
import android.telephony.*;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;



public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        Uri uriSMSURI = Uri.parse("content://sms/sent");
        Cursor cur = getContentResolver().query(uriSMSURI, new String[]{"_id", "thread_id", "address", "person", "date", "body"}, null, null, null);
        if (cur.getCount() > 0) {
            while (cur.moveToNext()) {
                String smsBody = cur.getString(5);
                Log.v("body", smsBody);

                if (smsBody.contains(":(")) {
                    Toast.makeText(getApplicationContext(), "found sad smiley in ur texts", Toast.LENGTH_LONG).show();
                }
                else{
                    Toast.makeText(getApplicationContext(),"didnt found what u are looking for",Toast.LENGTH_LONG).show();
                }
            }
        }
        super.onCreate(savedInstanceState);
    }
}

Right now my code is reading messages and also finds a keyword, but I want help in 2 things:

  1. How to get latest messages.
  2. Applying custom dictionary on messages.

Solution

  • Use the following for reading inbox

      Uri message = Uri.parse("content://sms/inbox");
    
    int j = 0;
      Cursor c =this.getContentResoulver().query(message, null, null, null, null);
    
      int totalSMS = c.getCount();
    if(totalSms >10)
    j = 10;
    else
    j= totalSms;
    
     if (c.moveToFirst()) {
        for (int i = 0; i < j;i++) {
        String messageBody = c.getString(c.getColumnIndex   ("body"));
     c.moveToNext();
           }
    }
    

    To split a string use following and search key words

    String[] splitted_message = stringObject.split("");
    

    and this array contains the words you can check with your keywords

    Dont forget to add permission

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

    Hope it Helps!