【十二】注入框架RoboGuice使用:(Your First Injected ContentProvider)
上一篇我们简单的介绍了一下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)的更多相关文章
- 【十】注入框架RoboGuice使用:(Your First Testcase)
上一篇我们简单的介绍了一下RoboGuice的使用([九]注入框架RoboGuice使用:(Your First Injected Service and BroadcastReceiver)),今天 ...
- 【九】注入框架RoboGuice使用:(Your First Injected Service and BroadcastReceiver)
上一篇我们简单的介绍了一下RoboGuice的使用([八]注入框架RoboGuice使用:(Your First Injected Fragment)),今天我们来看下服务(Service)和广播接受 ...
- 【八】注入框架RoboGuice使用:(Your First Injected Fragment)
上一篇我们简单的介绍了一下RoboGuice的使用([七]注入框架RoboGuice使用:(Your First Custom Binding)),今天我们来看下fragment的注解 ...
- 【十三】注入框架RoboGuice采用:(Logging via Ln)
上一篇我们简单的介绍了一下RoboGuice的使用([十二]注入框架RoboGuice使用:(Your First Injected ContentProvider)),今天我们来看下Log日志使用. ...
- 【十一年】注入框架RoboGuice采用:(Your First Injection into a Custom View class)
上一篇我们简单的介绍了一下RoboGuice的使用([十]注入框架RoboGuice使用:(Your First Testcase)),今天我们来看下自己定义View的注入(Custom View). ...
- 【二】注入框架RoboGuice使用:(Your First View Injection)
上一篇我们简单的介绍了一下RoboGuice的使用([一]注入框架RoboGuice使用:(A brief example of what RoboGuice does)),今天我们我看下View的注 ...
- 【七】注入框架RoboGuice使用:(Your First Custom Binding)
上一篇我们简单的介绍了一下RoboGuice的使用([六]注入框架RoboGuice使用:(Singletons And ContextSingletons)),今天我们来看下自己定义绑定(bindi ...
- 【三】注入框架RoboGuice使用:(Your First Resource Injection)
上一篇我们简单的介绍了一下RoboGuice的使用([二]注入框架RoboGuice使用:(Your First View Injection)),今天我们来看下资源文件的使用注解的方法: 为了在Ac ...
- 【六】注入框架RoboGuice使用:(Singletons And ContextSingletons)
上一篇我们简单的介绍了一下RoboGuice的使用([五]注入框架RoboGuice使用:(Your First POJO Injection)),今天我们来看下单例以及上下文单例(ContextSi ...
随机推荐
- hiho1393二分图多重匹配
题目链接:[http://hihocoder.com/problemset/problem/1393] 题意:中文题意. 题解:二分图的多重匹配.主要是建图然后跑一个最带流,再判断一下就可以了. 建图 ...
- 【BZOJ 1221】 1221: [HNOI2001] 软件开发 (最小费用流)
1221: [HNOI2001] 软件开发 Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 1581 Solved: 891 Description ...
- HDU 5909 Tree Cutting(FWT+树形DP)
[题目链接] http://acm.hdu.edu.cn/showproblem.php?pid=5909 [题目大意] 给出一棵树,其每棵连通子树的价值为其点权的xor和, 问有多少连通子树的价值为 ...
- 【MPI】矩阵向量乘法
输入作乘法的次数K 然后输入矩阵和向量的维度n 然后输入一个n维向量 然后输入K个n阶方阵 程序会给出该向量连续与此K个方阵做乘法后的结果 主要用了MPI_Gather, MPI_Allgather, ...
- iOS开发系列——内购、GameCenter、iCloud、Passbook系统服务开发汇总
社交 Social 现在很多应用都内置“社交分享”功能,可以将看到的新闻.博客.广告等内容分享到微博.微信.QQ.空间等,其实从iOS6.0开始苹果官方就内置了Social.framework专门来实 ...
- SSM+Maven(教程一):学习SSM框架的前提条件。
准备工作 环境准备 1.配置jdk:http://jingyan.baidu.com/article/6dad5075d1dc40a123e36ea3.html Intelij中配置JDK:File- ...
- 函数中的 arguments 对象
JavaScript函数具有像数组一样的对象,这些对象称为arguments,与传递给函数的参数相对应.传递给JavaScript函数的所有参数都可以使用arguments对象来引用. 现在我们开始学 ...
- DOM节点中属性nodeName、nodeType和nodeValue的区别 < Delphi >
http://msdn.microsoft.com/zh-cn/library/vstudio/hf9hbf87.aspx <?xml version="1.0"?> ...
- Android PopupWindow做的分享界面
package com.tq.mbaexam.view; import java.util.ArrayList; import java.util.LinkedHashMap; import java ...
- linux下关于压缩、解压相关的操作
本文转自: http://alex09.iteye.com/blog/647128 很不错的linux下关于压缩.解压相关的操作,适合于linux初学者. .tar 解包:tar xvf Fil ...