上一篇我们简单的介绍了一下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. BZOJ 1497 JZYZOJ 1344 [NOI2006]最大获利 网络流 最大权闭合图

    http://www.lydsy.com/JudgeOnline/problem.php?id=1497 http://172.20.6.3/Problem_Show.asp?id=1344   思路 ...

  2. 腾讯通消息webSDK踩坑

    1.腾讯通提供一个通过http协议的接口,可用于发送消息,公告等功能,要使用其功能首先要开启RTX_HTTPServer服务. 2.阅读文档http://rtx.tencent.com/sdk/,为了 ...

  3. LOJ2758 年轮蛋糕

    JOI 君马上要和妹妹 JOI 子和 JOI 美一起吃小吃.今天的小吃是他们三个人都很喜欢的年轮蛋糕. 年轮蛋糕是像下图一样呈圆筒形的蛋糕.为了把蛋糕分给三个人,JOI 君必须沿着半径方向切 3 刀, ...

  4. Nginx服务器的启动控制

    写在前面的话:读书破万卷,编码如有神--------------------------------------------------------------------主要内容包括: nginx服 ...

  5. python开发_pickle

    pickle模块使用的数据格式是python专用的,并且不同版本不向后兼容,同时也不能被其他语言说识别.要和其他语言交互,可以使用内置的json包使用pickle模块你可以把Python对象直接保存到 ...

  6. CROC 2016 - Elimination Round (Rated Unofficial Edition) E. Intellectual Inquiry 贪心 构造 dp

    E. Intellectual Inquiry 题目连接: http://www.codeforces.com/contest/655/problem/E Description After gett ...

  7. webstrom内置压缩工具YUI-compressor详解

    压缩工具层次不穷,各有优点,选择适合的压缩工具为将来做项目开发使用是一件很重要的事情!!在这介绍YUI-compressor 英文官网:http://yui.github.io/yuicompress ...

  8. Papilio Pro Boards

    http://papilio.cc/index.php?n=Papilio.PapilioOne The Papilio is an Open Source FPGA development boar ...

  9. gitignore / Delphi.gitignore

    https://github.com/github/gitignore/blob/master/Delphi.gitignore *.dcu *.~*~ *.local *.identcache __ ...

  10. linux下使用free命令查看实际内存占用(可用内存)

    转:http://blog.is36.com/linux_free_command_for_memory/ linux下在终端环境下可以使用free命令看到系统实际使用内存的情况,一般用free -m ...