'getContentResolver() is not working
I have a class name MyPhoneStateListener
. Here, I want to use getContentResolver
, but there is some problem in the class. It's not my main activity.
Cursor cur = getContentResolver()
.query(Calls.CONTENT_URI, projection, null, null, Calls.DATE +" desc");
Solution 1:[1]
To use getContentResolver
inside another class which doesn't extends Activity
/Service
you should have a Context
object.
Inside your MyPhoneStateListener
class, in your constructor ask for a Context
and save it, then when you need the content resolver
use context.getContentResolver()
Example:
public class MyPhoneStateListener {
private Context context;
public MyPhoneStateListener(Context context) {
this.context = context;
}
public void someMethod() {
Cursor cur = context.getContentResolver()
.query(Calls.CONTENT_URI, projection, null, null, Calls.DATE +" desc");
}
}
Solution 2:[2]
Cursor cur = this.getActivity().getContentResolver() .query(Calls.CONTENT_URI, projection, null, null, Calls.DATE +" desc");
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | |
Solution 2 | Andrea Gomez De Pablo |