SMS记录仪:方法getContentResolver()是未定义的类型(SMS Logger: m

2019-10-16 14:53发布

在机器人的编程和我'我'初学者试图创建的应用程序正在记录SMS的一个文件。 有荫有问题“的方法getContentResolver()是未定义的类型SMSObserver”,我不知道为什么...

下面是代码:

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...*/
}
}

和进口:

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;

请帮忙。

Answer 1:

您可以致电只能在上下文对象的方法。 尝试这个:

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);
}
}


Answer 2:

扩展应用保持上下文的保持和静态访问它,或者你可以通过函数调用来在ContentResolver的每个库函数。 目前你没有上下文的引用,所以你不能调用getContentResolver()在这里。



文章来源: SMS Logger: method getContentResolver() is undefined for the type