【问题标题】:How to get "android.provider.ContactsContract.Contacts" field in Luaj如何在 Luaj 中获取“android.provider.ContactsContract.Contacts”字段
【发布时间】:2019-08-14 19:51:24
【问题描述】:

我使用 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);

【问题讨论】:

    标签: luaj luajava


    【解决方案1】:

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

        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
    

    【讨论】:

    • 您能否为您的答案添加一些解释,使其更具可读性?
    猜你喜欢
    • 2015-01-27
    • 2017-02-07
    • 1970-01-01
    • 1970-01-01
    • 2019-06-28
    • 2012-08-21
    • 2018-08-12
    • 2016-06-12
    • 2020-03-01
    相关资源
    最近更新 更多