Android开发:getContentResolver的使用
2017-02-16 17:14
393 查看
getContentResolver的使用 分两种情况:
一、在有Activity和Service的情况下
getContext().getContentResolver().insert(...);
1.getContext()是获得一个上下文对象(Context),一般在四大组件中都会获取上下文对象。
2.在Activity和Service中,就没必要获取Context了,因为他本身就是,所以可以直接调用getContentResolver()。
3.在ContentProvider中,就需要先调用getContext()获取到Context,然后调用getContentResolver()
获得ContentResolver对 象,也就是,getContext().getContentResolver().
另外:
(1)getContext().getContentResolver()返回的是ContentResolver
对象,ContentResolver负责获取ContentProvider提供的数据。
(2) MainActivity.this.getContentResolver()+数据库操作
等同于
getContext().getContentResolver()+数据操作。
二、在没有Activity的情况下
例如:
public class companyInfo{
public void AAA() throws Exception {
Uri scanUri = getContentResolver().insert(MediaStore.getMediaScannerUri(), values);
getContentResolver().delete(scanUri, null, null);
}
}
报错提示getContentResolver()不存在,需要通过activity或者service来实现。
这个类的AAA()方法肯定是有activity或者service调用的,所以需要写一个带有Context参数的构造方法就可以实现:
public class companyInfo{
private Context context;
public companyInfo(Context context){
this.context = context;
}
public
void AAA() throws Exception {
Uri scanUri = getContentResolver().insert(MediaStore.getMediaScannerUri(), values);
context.getContentResolver().delete(scanUri, null, null);
}
}
一、在有Activity和Service的情况下
getContext().getContentResolver().insert(...);
1.getContext()是获得一个上下文对象(Context),一般在四大组件中都会获取上下文对象。
2.在Activity和Service中,就没必要获取Context了,因为他本身就是,所以可以直接调用getContentResolver()。
3.在ContentProvider中,就需要先调用getContext()获取到Context,然后调用getContentResolver()
获得ContentResolver对 象,也就是,getContext().getContentResolver().
另外:
(1)getContext().getContentResolver()返回的是ContentResolver
对象,ContentResolver负责获取ContentProvider提供的数据。
(2) MainActivity.this.getContentResolver()+数据库操作
等同于
getContext().getContentResolver()+数据操作。
二、在没有Activity的情况下
例如:
public class companyInfo{
public void AAA() throws Exception {
Uri scanUri = getContentResolver().insert(MediaStore.getMediaScannerUri(), values);
getContentResolver().delete(scanUri, null, null);
}
}
报错提示getContentResolver()不存在,需要通过activity或者service来实现。
这个类的AAA()方法肯定是有activity或者service调用的,所以需要写一个带有Context参数的构造方法就可以实现:
public class companyInfo{
private Context context;
public companyInfo(Context context){
this.context = context;
}
public
void AAA() throws Exception {
Uri scanUri = getContentResolver().insert(MediaStore.getMediaScannerUri(), values);
context.getContentResolver().delete(scanUri, null, null);
}
}
相关文章推荐
- Android开发如何在getContentResolver().query()方法中实现结果去重
- Android开发笔记——Itent.ACTION_PICK Intent.ACTION_GET_CONTENT妙用
- Android ContentProvider和getContentResolver
- Android 4.4以上获取图片URI格式不同导致getContentResolver返回null
- 在没有activity的时候使用getContentResolver方法
- String android.content.Context.getString(int resId, Object... formatArgs)的使用
- AndroidContentProvider ContentResolver和ContentObserver的使用
- Android应用:ContentResolver内容观察者的使用
- android开发中使用setContentView和LayoutInflater的点点滴滴
- Android ContentProvider和getContentResolver
- Android开发--详解ContentProvider/Cursor的使用
- Android开发中使用Volley框架发送请求的get和post方法
- android开发 使用HttpClient访问网络,post、get方式。注册、登陆、上传文件。
- Android 设置铃声,getContentResolver().insert returns null
- phonegap 开发入门 PhoneGap官方网站上有详细的入门示例教程,这里,我针对使用PhoneGap进行Android移动应用的开发对其官网的Get Started进行一些介绍、补充。 Ste
- Android基础 : Android ContentProvider和getContentResolver
- Android开发使用GET方式向服务器请求和发送数据
- Android开发——通过使用getResource.getIdentifier() 获取layout中组件id
- Android开发之使用Get方法向服务器发送请求
- getContentResolver().query()方法selection参数使用详解(转)