【问题标题】:CursorLoader in a Non Activity非活动中的 CursorLoader
【发布时间】:2012-11-18 01:45:06
【问题描述】:

我有一个小项目,我从内容提供商那里读取系统指标,例如通话记录、短信记录等。

我创建了 (Call/SMS)Logger 类来读取内容提供者并将信息保存在 (Call/SMS)Metrics 类的对象中。

MainActivity 使用 (Call/SMS)Metrics 类的对象中的信息,并使用 databaseOpenHelper 类将数据保存在我自己的数据库中。

现在我打算使用 CursorLoader 从 contentproviders 加载数据。

我看到的例子表明 MainActivity 实现了 LoaderManager.LoaderCallbacks

当对非活动类进行实际查询时,如何在我的项目中使用它?

我可以在 Activity 中创建 I 1 loaderManger 并用于每个非 Activity 吗?

这里是一些示例代码sn-ps:

从 Main Activity 我调用数据集合,我将上下文传递给 clssess,以便他们可以在管理器光标中使用它

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 的方法

public SMSMetrics CollectSMSMetrics() {
    SMSLogger smsLogger = new SMSLogger(_context);
    smsLogger.ReadSMSDataFromPhone();
    return smsLogger.GetSMSMetrics();
}

SMSLogger 类中的变量。

Uri smsUri = Uri.parse("content://sms");
String[] selectColumns = null;
String where = null;
String whereArgs[] = null;
String sortBy = null;

SMSLogger中使用游标读取数据的方法

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);
}

【问题讨论】:

    标签: android android-activity android-contentprovider android-cursorloader


    【解决方案1】:

    当对非活动类进行实际查询时,如何在我的项目中使用它?

    要么让活动成为实际使用Loaders 的活动,要么不使用Loaders。欢迎您在 AsyncTaskThread 中使用 ContentResolver

    【讨论】:

    • 您能否在我的应用程序场景中提供一个 AsyncTask 示例。我已经在我的代码中使用了内容解析器,但是在所有数据库操作完成之前,GUI(活动)对函数调用没有响应。
    • @KhurramMajeed:将您现有的ReadSMSDataFromPhone() 逻辑放在doInBackground()AsyncTask 中。从 AsyncTaskonPostExecute() 更新 UI。
    【解决方案2】:

    我不能 100% 确定您的用例是什么,但是...

    实现 LoaderCallbacks 的活动的重点只是易于使用。你可以在任何你想要的地方声明你的 LoaderCallbacks,然后把它交给 loaderManager:

    LoaderCallbacks blah = new LoaderCallbacks() {
    //Override methods here
    }
    
    getLoaderManager().initLoader(LOADER_ID, null, blah);    
    

    请注意,getLoaderManager() 通常与 Fragment 或 Activity 相关联。

    【讨论】:

    • 我想在我的非 Activity 类中功能性地使用 CursorLoader,例如 SMSLogger 类
    猜你喜欢
    • 1970-01-01
    • 2013-03-09
    • 1970-01-01
    • 1970-01-01
    • 2016-07-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多