如何在Luaj中获取“ android.provider.ContactsContract.Contacts”字段

时间:2019-03-24 11:00:29

标签: luaj luajava

我使用Lua Interpreter获取有关iGO导航器的信息,我需要从android.provider.ContactsContract.Contacts中获取一些字段

我成功

cntr = luajava.bindClass("android.provider.ContactsContract")

然后我尝试获取联系人

cntct = cntr:Contacts()

cntct = luajava.bindClass("android.provider.ContactsContract.Contacts")

没有成功

基本上我需要在那里得到结果

Intent pickIntent = new Intent(Intent.ACTION_PICK, 
android.provider.ContactsContract.Contacts.CONTENT_URI);
startActivityForResult(pickIntent, PICK_RESULT);

1 个答案:

答案 0 :(得分:0)

这是对自己的回答。当然,您需要在清单中获得许可。

    cr = activity:getContentResolver()
    contacts = luajava.bindClass("android.provider.ContactsContract")
    build = luajava.bindClass("android.os.Build")

    D_Name = build.VERSION.SDK_INT >= build.VERSION_CODES.HONEYCOMB and contacts.Contacts.DISPLAY_NAME_PRIMARY or contacts.Contacts.DISPLAY_NAME
    --str = luajava.bindClass("java.lang.String")

    cur = cr:query(contacts.Contacts.CONTENT_URI)
    local count = cur and cur:getCount() or -1
    print(count)
    if cur and count > 0 then
        --local name, id, pCur, phoneNo
        while cur:moveToNext() do
            id = cur:getString(cur:getColumnIndex(contacts.Contacts._ID))
            name = cur:getString(cur:getColumnIndex(D_Name))
            --print("id: " .. id, "name: " .. name)

            if cur:getInt(cur:getColumnIndex(contacts.Contacts.HAS_PHONE_NUMBER)) > 0 then
                --str_str = luajava.newInstance( "java.lang.String", id )
             -- str_array = luajava.newInstance( "java.lang.reflect.Array", ({str_str}) )
                pCur = cr:query(contacts.CommonDataKinds.Phone.CONTENT_URI, nil, contacts.CommonDataKinds.Phone.CONTACT_ID .. " = ?", ({id}), nil)
       print(pCur:getCount())
                while pCur:moveToNext() do
                    phoneNo = pCur:getString(pCur:getColumnIndex(contacts.CommonDataKinds.Phone.NUMBER))
                    print("Name: " .. name, "Phone Number: " .. phoneNo)
                end
                pCur:close()
            end

            --if cur:getInt(cur:getColumnIndex(contacts.Contacts.HAS_EMAIL_DATA)) > 0 then
                --str_str = luajava.newInstance( "java.lang.String", id )
             -- str_array = luajava.newInstance( "java.lang.reflect.Array", ({str_str}) )
            pCur = cr:query(contacts.CommonDataKinds.Email.CONTENT_URI, nil, contacts.CommonDataKinds.Email.CONTACT_ID .. " = ?", ({id}), nil)
       print(pCur:getCount())
            if pCur then
                while pCur:moveToNext() do
                    Email_data = pCur:getString(pCur:getColumnIndex(contacts.CommonDataKinds.Email.DATA))
                    print("Name: " .. name, "Email_data: " .. Email_data)
                end
            end
            pCur:close()

            pCur = cr:query(contacts.Data.CONTENT_URI, nil, contacts.Data.CONTACT_ID .. " = " .. id)
       print(pCur:getCount())
            if pCur then
                while pCur:moveToNext() do
                    skype_type = pCur:getInt(pCur:getColumnIndex(contacts.CommonDataKinds.Im.PROTOCOL))
        print(skype_type, contacts.CommonDataKinds.Im.PROTOCOL_SKYPE)
                    if contacts.CommonDataKinds.Im.PROTOCOL_SKYPE == skype_type then
                        imName = pCur:getString(pCur:getColumnIndex(contacts.CommonDataKinds.Im.DATA))
                        print("Name: " .. name, "skype_type: " .. skype_type .. "imName: " .. imName)
                    end
                end
            end
            pCur:close()
            --end

        end
    end
    if cur then
        cur:close()
    end