简体   繁体   English

android黑屏时获得电话联系

[英]android black screen when get phone contacts

I'm using this code to get all contacts from phone but when I use it that cause of black screen. 我正在使用此代码从电话中获取所有联系人,但是当我使用它时,会导致黑屏。

Thread : 线程:

        Thread getAllContacts = new Thread () {
            @Override
            public void run () {
                runOnUiThread ( new Runnable () {
                    @Override
                    public void run () {
                        getContacts ( getContentResolver () );
                    }
                } );
            }
        };
        getAllContacts.start ();

And this below code is my getContacts method, I can not update it to have fast way to get contacts: 下面的代码是我的getContacts方法,我无法对其进行更新以获取联系的快速方法:

public void getContacts(ContentResolver cr){
    Cursor phones = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,null,null, null);

    while (phones.moveToNext())
    {
        String id          = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone._ID));
        String name        = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
        String phoneNumber = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
        Bitmap photo       = G.getContactPhoto ( phoneNumber, getContentResolver () );

        ContactListStructure item = new ContactListStructure();
        String phone_number = phoneNumber.replaceAll("\\s+","").trim ().replace("+98", "0");
        int check_mobile = phone_number.indexOf ( "09" );
        if( check_mobile != -1){
            item.id = Long.parseLong ( id );
            item.name = name;
            item.mobile = phone_number;
            item.photo = photo;
            item.checked = false;
            G.contact_item.add ( item );
            //personlst.add( new Person (item.name, item.mobile) );
        }
    }
    phones.close ();
}

Ok I see your problem. 好的,我知道您的问题。 You are running it on the UI thread. 您正在UI线程上运行它。 Create an AsyncTask and run it there. 创建一个AsyncTask并在那里运行它。 When the tash is complete use the runOnUIThread method to display the contacts. 完成任务后,使用runOnUIThread方法显示联系人。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM