非活动中的CursorLoader [英] CursorLoader in a Non Activity
问题描述
我有一个小项目,我从内容提供商那里读取诸如呼叫日志,短信日志等系统指标.
I have small project where I read system metrics like Calls Logs, SMS Logs etc from content providers.
我创建了(Call/SMS)Logger 类,以从内容提供者中读取并将信息保存在(Call/SMS)Metrics 类的对象中.
I have created (Call/SMS)Logger classes to read from content providers and save info in object of (Call/SMS)Metrics clases.
MainActivity 使用(Call/SMS)Metrics 类的对象中的信息,并使用databaseOpenHelper类将数据保存在我自己的数据库中.
The MainActivity uses the info in the objects of (Call/SMS)Metrics classes and saves the data in my own database using a databaseOpenHelper class.
现在,我打算使用CursorLoader从contentproviders中加载数据.
Now I intend to use CursorLoader to load datafrom contentproviders.
我看到的示例表明MainActivity实现了LoaderManager.LoaderCallbacks
The examples I have seen suggest that MainActivity implements LoaderManager.LoaderCallbacks
在非活动类上完成实际查询后,如何在我的项目中使用它?
我可以在Activity中创建1个loaderManger并将其用于每个非Activity吗?
以下是一些示例代码段:
Here is some sample code snippets:
在Main Activity中,我将数据称为集合,然后将上下文传递给clsss,以便他们可以在管理器游标中使用它
From Main Activity I call the collection of data, I pass the context to the clssess so that they can use it in manager cursor
private void CollectSystemMetrics() {
//passing the context in constructor so that it can be passed to
//the non activity classes which need it for quering
SystemMetricsCollector collector = new SystemMetricsCollector(this);
_callMetrics = collector.CollectCallMetrics();
_smsMetrics = collector.CollectSMSMetrics();
Toast toast = Toast.makeText(
MyActivity.this,
"Calls and SMS Data Collected",
Toast.LENGTH_SHORT);
toast.show();
}
SystemMetricsCollector中的方法来提升SMSData
Method in SystemMetricsCollector to raed SMSData
public SMSMetrics CollectSMSMetrics() {
SMSLogger smsLogger = new SMSLogger(_context);
smsLogger.ReadSMSDataFromPhone();
return smsLogger.GetSMSMetrics();
}
SMSLogger类中的变量.
Variables in SMSLogger class.
Uri smsUri = Uri.parse("content://sms");
String[] selectColumns = null;
String where = null;
String whereArgs[] = null;
String sortBy = null;
SMSLogger中使用光标读取数据的方法
Methods in SMSLogger to read data using cursor
public void ReadSMSDataFromPhone() {
int inCount = 0, outCountContacts = 0, outCountUnknown = 0;
Cursor managedCursor;
managedCursor = _context.getContentResolver().query(
smsUri,selectColumns,where,whereArgs,sortBy);
try {
if (managedCursor.moveToFirst()) {
int idxAddress = managedCursor.getColumnIndexOrThrow("address");
int idxType = managedCursor.getColumnIndex("type");
do {
int valType = managedCursor.getInt(idxType);
switch (valType) {
case 2://outgoing
String valAddress =
managedCursor.getString(idxAddress);
if (isContact(valAddress)) outCountContacts++;
else outCountUnknown++;
break;
default://incoming
inCount++;
break;
}
} while (managedCursor.moveToNext());
}
} finally {
managedCursor.close();
}//end finally
_smsMetrics.set_receivedSMS(inCount);
_smsMetrics.set_sentSMSContacts(outCountContacts);
_smsMetrics.set_sentSMSUnknown(outCountUnknown);
}
推荐答案
当对非活动类进行实际查询时,如何在我的项目中使用它?
How can I use this in my project when actual query stuff is done on non activity classes?
使活动成为实际使用Loaders
的活动,或者不使用Loaders
.欢迎您在AsyncTask
或Thread
中使用ContentResolver
.
Either have the activity be the one to actually use the Loaders
, or do not use Loaders
. You are welcome to use a ContentResolver
in an AsyncTask
or Thread
instead.
这篇关于非活动中的CursorLoader的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!