SMS Logger: getContentResolver () method - undefined for type

I am starting to program in android, and I am trying to create an application that writes sms to a file. I have a problem with "The getContentResolver () method is undefined for type SMSObserver" and I don't know why ...

Here is the code:

public class SMSObserver extends ContentObserver { SMSLogger smsLogger; public SMSObserver(SMSLogger smsLogger) { super(new Handler()); this.smsLogger = smsLogger; } @Override public void onChange(boolean selfChange) { super.onChange(selfChange); querySMS(); } protected void querySMS() { 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 out = ""; if (protocol == null) out = "Sending to "+add + ".Time:"+time +" - "+body; else out = "Receive from "+add + ".Time:"+time +" - "+body; /*logging action HERE...*/ } } 

and import:

 import android.database.ContentObserver; import android.os.Handler; import android.content.ContextWrapper; import org.json.JSONException; import org.json.JSONStringer; import android.content.ContentResolver; import android.content.Intent; import android.content.BroadcastReceiver; import android.database.Cursor; import android.net.Uri; import android.content.Context; import android.os.RemoteException; import android.provider.ContactsContract; import android.provider.ContactsContract.CommonDataKinds; import android.provider.ContactsContract.PhoneLookup; 

Please, help.

0
java android sms broadcastreceiver
Apr 15 '12 at 20:05
source share
2 answers

You can call this method only for the Context object. Try the following:

 public class SMSObserver extends ContentObserver { SMSLogger smsLogger; Context context; public SMSObserver(SMSLogger smsLogger, Context c) { super(new Handler()); context = c; this.smsLogger = smsLogger; } protected void querySMS() { Uri uriSMS = Uri.parse("content://sms/"); Cursor cur = context.getContentResolver().query(uriSMS, null, null, null, null); } } 
+4
Apr 15 '12 at 20:26
source share

Extend the application to maintain context and access it statically. Or you can pass a function call to each library function in a ContentResolver. You currently do not have a reference to the Context, so you cannot call getContentResolver () here.

0
Apr 15 '12 at 20:40
source share



All Articles