上一篇我们简单的介绍了一下RoboGuice的使用(【十一】注入框架RoboGuice使用:(Your
First Injection into a Custom View class)
),今天我们来看下内容提供者(ContentProvider)的注入。

和Robo*Activities一样,RoboContentProviders通过RoboGuice也能自己主动获得注入,为了简便我们能够注入 authority
URI。这时我们须要定义以下自己的module:

public class ContentProvidersModule extends AbstractModule {
@Override
protected void configure() {
} @Provides
@Named("example_authority_uri")
public Uri getExampleAuthorityUri() {
return Uri.parse("content://com.example.data");
}
}

以下就是一个使用RoboGuice注入的Android ContentProvider

public class MyExampleContentProvider extends RoboContentProvider {

    @Inject
@Named("example_authority_uri")
private Uri contentUri; private UriMatcher uriMatcher = new UriMatcher(UriMatcher.NO_MATCH); @Override
public boolean onCreate() {
super.onCreate();
uriMatcher.addURI(contentUri.getAuthority(), "foo/#", 0);
return true;
} @Override
public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) {
switch (uriMatcher.match(uri)) {
case 0:
// Return results of your query
return null;
default:
throw new IllegalStateException("could not resolve URI: " + uri);
} return null;
} @Override
public String getType(Uri uri) {
return null;
} @Override
public Uri insert(Uri uri, ContentValues values) {
return null;
} @Override
public int delete(Uri uri, String selection, String[] selectionArgs) {
return 0;
} @Override
public int update(Uri uri, ContentValues contentValues, String selection, String[] selectionArgs) {
return 0;
}
}

在你的代码中。你能够通过注入的authority URI来回调content provider的方法:

public class ExampleActivity extends RoboFragmentActivity implements LoaderManager.LoaderCallbacks<Cursor> {

    @Inject
@Named("example_authority_uri")
private Uri contentUri; @Override
public Loader<Cursor> onCreateLoader(int id, Bundle args) {
return new CursorLoader(this, Uri.withAppendedPath(contentUri, "foo"), null, null, null, null);
} @Override
public void onLoadFinished(Loader<Cursor> loader, Cursor data) {
getSupportLoaderManager().destroyLoader(1);
// do something cool with the data!
} @Override
public void onLoaderReset(Loader<Cursor> loader) {
} public void start() {
getSupportLoaderManager().initLoader(1, null, this);
}
}

不要忘记你的content provinder在配置文件的注冊

 <provider
android:authorities="com.example.data"
android:name=".MyExampleContentProvider"
android:exported="false" />

【十二】注入框架RoboGuice使用:(Your First Injected ContentProvider)的更多相关文章

  1. 【十】注入框架RoboGuice使用:(Your First Testcase)

    上一篇我们简单的介绍了一下RoboGuice的使用([九]注入框架RoboGuice使用:(Your First Injected Service and BroadcastReceiver)),今天 ...

  2. 【九】注入框架RoboGuice使用:(Your First Injected Service and BroadcastReceiver)

    上一篇我们简单的介绍了一下RoboGuice的使用([八]注入框架RoboGuice使用:(Your First Injected Fragment)),今天我们来看下服务(Service)和广播接受 ...

  3. 【八】注入框架RoboGuice使用:(Your First Injected Fragment)

        上一篇我们简单的介绍了一下RoboGuice的使用([七]注入框架RoboGuice使用:(Your First Custom Binding)),今天我们来看下fragment的注解     ...

  4. 【十三】注入框架RoboGuice采用:(Logging via Ln)

    上一篇我们简单的介绍了一下RoboGuice的使用([十二]注入框架RoboGuice使用:(Your First Injected ContentProvider)),今天我们来看下Log日志使用. ...

  5. 【十一年】注入框架RoboGuice采用:(Your First Injection into a Custom View class)

    上一篇我们简单的介绍了一下RoboGuice的使用([十]注入框架RoboGuice使用:(Your First Testcase)),今天我们来看下自己定义View的注入(Custom View). ...

  6. 【二】注入框架RoboGuice使用:(Your First View Injection)

    上一篇我们简单的介绍了一下RoboGuice的使用([一]注入框架RoboGuice使用:(A brief example of what RoboGuice does)),今天我们我看下View的注 ...

  7. 【七】注入框架RoboGuice使用:(Your First Custom Binding)

    上一篇我们简单的介绍了一下RoboGuice的使用([六]注入框架RoboGuice使用:(Singletons And ContextSingletons)),今天我们来看下自己定义绑定(bindi ...

  8. 【三】注入框架RoboGuice使用:(Your First Resource Injection)

    上一篇我们简单的介绍了一下RoboGuice的使用([二]注入框架RoboGuice使用:(Your First View Injection)),今天我们来看下资源文件的使用注解的方法: 为了在Ac ...

  9. 【六】注入框架RoboGuice使用:(Singletons And ContextSingletons)

    上一篇我们简单的介绍了一下RoboGuice的使用([五]注入框架RoboGuice使用:(Your First POJO Injection)),今天我们来看下单例以及上下文单例(ContextSi ...

随机推荐

  1. java 安全 技术

    韩梦飞沙  韩亚飞  313134555@qq.com  yue31313  han_meng_fei_sha 加密对应的类 是 Cipher ,意思 是加密的意思.这个类 在  javax.cryp ...

  2. 【贪心+Treap】BZOJ1691-[Usaco2007 Dec]挑剔的美食家

    [题目大意] 有n头奶牛m种牧草,每种牧草有它的价格和鲜嫩度.每头奶牛要求它的牧草的鲜嫩度要不低于一个值,价格也不低于一个值.每种牧草只会被一头牛选择.问最少要多少钱? [思路] 显然的贪心,把奶牛和 ...

  3. bzoj 1834: [ZJOI2010]network 网络扩容 -- 最大流+费用流

    1834: [ZJOI2010]network 网络扩容 Time Limit: 3 Sec  Memory Limit: 64 MB Description 给定一张有向图,每条边都有一个容量C和一 ...

  4. Java反射机制涉及的类常见方法使用总结

    import java.lang.reflect.Constructor; import java.lang.reflect.*; /*Class:代表一个字节码文件的对象,每当有类被加载进内存,JV ...

  5. bzoj1393 旅游航道

    Description SGOI旅游局在SG-III星团开设了旅游业务,每天有数以万计的地球人来这里观光,包括联合国秘书长,各国总统和SGOI总局局长等.旅游线路四通八达,每天都有总躲得载客太空飞船在 ...

  6. 2015 UESTC 搜索专题N题 韩爷的梦 hash

    韩爷的梦 Time Limit: 20 Sec  Memory Limit: 256 MB 题目连接 http://acm.uestc.edu.cn/#/contest/show/61 Descrip ...

  7. kali 执行apt-get upgrade后,终端无法打开的解决办法

    今天在kali执行apt-get upgrade命令后,reboot启动,发现进入界面终端无法开启 一波百度,google发现大概应该是语言的配置问题,因为最开始安装kali的时候是选择中文,可能up ...

  8. 原来通过修改dns加快app store下载速度的确有效

    说来惭愧,这几天休假,并没有做什么技术上的修行.小伙伴推荐我一款avg游戏<11eyes 罪与罚与被诅咒的少女>,说是神作.但是app store上卖rmb118元,起初并没有什么兴趣去购 ...

  9. 一、 Log4E插件下载

    下载地址:http://log4e.jayefem.de/content/view/3/2/ 二.安装Log4E插件 将下载下来的压缩包解压缩,如下图所示: 解压缩生成的[de.jayefem.log ...

  10. redis cluster 设置密码做集群时gem下client.rb文件修改

    redis节点有设置密码,然后在创建集群的时候没有设置密码的命令 ./redis-trib.rb create --replicas 1 127.0.0.1:6381 127.0.0.1:6382 1 ...