是否可以在没有 Activity 时访问 Contentresolver(例如,当我试图从服务访问它时)?
我尝试过使用“Android.App.Context”和“Android.App.Activity”但没有成功。
这是我试图访问的 Contentprovider(通过 Activity ):
class ReceiveCallState : BroadcastReceiver
public void AccessCallLog()
{
Android.Net.Uri uri = CallLog.ContentUri;
string[] projection = {
CallLog.Calls.CachedName,
CallLog.Calls.Duration
};
var cr = new CursorLoader(activity, uri, projection, null, null, null);
var cursor = (ICursor)cr.LoadInBackground();
}
}
我想做同样的调用,但没有 Activity 对象(或者在没有明确存在时以某种方式检索 Activity )
编辑:代码。添加了正在继承的类 亲切的问候
最佳答案
您可以使用 Android.App.Application.Context
检索 Context
以提供 CursorLoader
构造函数。
例如:
Android.Content.Context context = Android.App.Application.Context;
// ...
var cr = new CursorLoader(context, uri, projection, null, null, null);
关于android - 非 Activity 中的 Xamarin ContentResolver/CursorLoader 访问,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25596295/