没有activity如何使用getContentResolver

class如下:

public class sendInformation{

  public void test() throws Exception {
    Uri uri = SuspiciousActivityTable.CONTENT_URI;
    getContentResolver().update(uri, values2, where,new String[]{"Null"});
    }
  }
}

但是系统说getContentResolver()不存在,我知道需要context或者activity来实现。但是不知道怎么获得正确的context?

你这个类的test()方法肯定是有activity或者service调用的,所以你可以写一个有参构造函数

public class sendInformation{
      private Context context;
      public sendInformation(Context context){
            this.context = context;
      }
      public void test() throws Exception {
            Uri uri = SuspiciousActivityTable.CONTENT_URI;
            context.getContentResolver().update(uri, values2, where,new String[]{"Null"});
      }
}

你再试试,看可以么?

想办法当参数传进来吧,好像没别的方法

楼上已经将代码贴上来了,分析的很合理,构造中传递或者普通方法中传递都是可以的

getContentResolver方法是属于Context类的。并不需要非要用Activity对象。只要是Context的之类的实例都可以,例如Service。如果没有Context对象,需要通过方法传入,这也是为什么在android中很多方法(主要是构造方法)都需要传入Context类型的参数的原因。