使用承诺中的LoaderManager.LoaderCallbacks加载Android联系人

时间:2016-08-26 13:35:10

标签: android typescript nativescript android-loadermanager angular2-nativescript

我正在为我的 NativeScript 应用程序编写一个角度2服务,用于检索所有用户的联系人。我当前的实现使用了nativescript-contacts中使用的同步getContentResolver方法。 cursor.getCount报告超过7k游标并冻结应用程序总共约3秒。不好。

我正在遵循本指南using-loaders-in-android,到目前为止没有运气。

这是我到目前为止所拥有的:

declare let android: any
declare let java: any

import * as application from "application"
import {Injectable} from "@angular/core"

@Injectable()
export class ContactsService
    extends android.support.v4.app.LoaderManager
    implements android.support.v4.app.LoaderManager.LoaderCallbacks {

    private contactsLoader: any = new android.support.v4.app.LoaderManager.LoaderCallbacks(
        class extends android.support.v4.app.LoaderManager.LoaderCallbacks {
            onCreateLoader(id, args) {
                let projection: Array<string> = [
                    android.provider.ContactsContract.RawContactsColumns.CONTACT_ID,
                    android.provider.ContactsContract.ContactsColumns.DISPLAY_NAME,
                    android.provider.ContactsContract.ContactsColumns.HAS_PHONE_NUMBER,
                    android.provider.ContactsContract.ContactsColumns.LOOKUP_KEY,
                    android.provider.ContactsContract.ContactsColumns.PHOTO_URI,
                    android.provider.ContactsContract.CommonDataKinds.Phone.NUMBER,
                    android.provider.ContactsContract.CommonDataKinds.Phone.NORMALIZED_NUMBER,
                    android.provider.ContactsContract.CommonDataKinds.Phone.TYPE,
                    android.provider.ContactsContract.DataColumns.MIMETYPE,
                ]
                return new android.support.v4.content.CursorLoader(
                    application.android.foregroundActivity,
                    android.provider.ContactsContract.Data.CONTENT_URI,
                    'data2 IS 2',
                    null,
                    null
                ).loadInBackground()

            }

            onLoadFinished(param0: android.support.v4.content.Loader, param1: java.lang.Object): void {
                global.tnsconsole.dump('onLoadFinished > param0', param0)
                global.tnsconsole.dump('onLoadFinished > param1', param1)
            }

            onLoaderReset(param0: android.support.v4.content.Loader): void {
                global.tnsconsole.dump('onLoaderReset > param0', param0)
            }
        }
    )

    getContactsAsync(): Promise<any> {
        // since the initLoader method is part of android.support.v4.app.LoaderManager
        this.initLoader(1, null, this)
        // but i get this error :(
        // EXCEPTION: Error: Cannot convert object to Landroid/support/v4/app/LoaderManager$LoaderCallbacks; at index 2
        return Promise.resolve([])
    }

}
  

有谁知道如何正确实现LoaderManager.LoaderCallbacks所以我可以在后台线程中运行游标?

谢谢!

0 个答案:

没有答案