短信记录仪:法getContentResolver()是未定义的类型 [英] SMS Logger: method getContentResolver() is undefined for the type

查看:427
本文介绍了短信记录仪:法getContentResolver()是未定义的类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'am begginer in android programming and I'am trying to create app that is logging sms's to a file. Iam having a problem with "The method getContentResolver() is undefined for the type SMSObserver" and i dont know why...

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

和进口:

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;

请帮忙。

推荐答案

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

You can call that method only on Context object. Try this:

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

这篇关于短信记录仪:法getContentResolver()是未定义的类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆