2

I'am trying to create android (Froyo 2.2) SMS logging app. Using many tutorials I've made the main part of the app and it works like a charm! Basically it reads last sms (send or received), gets contact name by a phone number, converts milliseconds unixtime to a date and display result on the screen.

Now I want to remove GUI, modify that code so the app will monitor phone for any incoming/outgoing sms activity and if there will be one it will start mentioned code and save result to a file.

I wish whole app to be completely transparent/stealth for user (working in a background). I'm suspecting that I have to use somehow the ContentObserver class but I've got some problem with implementation (even with start).

Could You help?

Current code:

import android.app.Activity;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.provider.ContactsContract.PhoneLookup;
import android.widget.TextView;
import android.net.Uri;
import android.database.Cursor;
import java.util.Date;

public class SMSLog extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        TextView tv = new TextView(this);

        Uri uriSMS = Uri.parse("content://sms/");
        Cursor cur = getContentResolver().query(uriSMS, null, null, null, null);
        cur.moveToNext();

        String body = cur.getString(cur.getColumnIndex("body"));   
        String add = cur.getString(cur.getColumnIndex("address"));
        String time = cur.getString(cur.getColumnIndex("date"));
        String protocol = cur.getString(cur.getColumnIndex("protocol"));       

        String contactName = "";
        Uri personUri = Uri.withAppendedPath( ContactsContract.PhoneLookup.CONTENT_FILTER_URI, Uri.encode(add));  
        Cursor c = getContentResolver().query(personUri, new String[] { PhoneLookup.DISPLAY_NAME }, null, null, null );  
        if( c.moveToFirst() ) {  
            int nameIndex = c.getColumnIndex(PhoneLookup.DISPLAY_NAME);  
            contactName = c.getString(nameIndex); 
        }
        c.close();
        cur.close();

        String out = "";
        Date d = new Date(Long.valueOf(time)); 

        if (protocol == null)
            out = "Sending to: "+ contactName + " <"+add +">\nDate: "+d +"\nBody: "+body+"\n\n";
        else 
            out = "Received from: "+ contactName + " <"+add +">\nDate: "+d +"\nBody: "+body+"\n\n";

        tv.setText(out);
        setContentView(tv);

    }
}
dave.c
  • 10,910
  • 5
  • 39
  • 62
mika
  • 57
  • 1
  • 5
  • http://stackoverflow.com/questions/4257045/android-register-application-to-receive-sms – Ran Apr 17 '12 at 07:54

0 Answers0