应用程序冻结了几秒钟,同时从电话中获取和存储联系人到SQLite表

时间:2019-07-14 11:43:09

标签: android listview contacts

我正试图获取电话联系并将其存储到SQLite表中,但一切正常,但应用程序冻结了几秒钟。请帮助我解决此问题。谢谢。

我尝试了Async任务,但是它显示了错误消息,即只有UI线程触摸其视图

public class ContactsActivity extends AppCompatActivity {
    private DataBaseHelper db;
    private ListView listView;
    private ProgressBar progressBar;
    private List<String> namelist=null;
    //private Button btnLoad;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_contacts);
        listView = findViewById(R.id.listView);
        progressBar = findViewById(R.id.progressBar);
        //btnLoad = findViewById(R.id.btnLoad);
        db=new DataBaseHelper(ContactsActivity.this);
        namelist = db.getData();

        if(namelist.size()>0)
        {
            ArrayAdapter listadapter = new ArrayAdapter<>(getApplication(),android.R.layout.simple_list_item_1,namelist);
            listView.setAdapter(listadapter);
            listadapter.notifyDataSetChanged();
        }
        else{
//            AsyncTask.execute(this::getcontacts);
            getcontacts();
        }

    }
    public void getcontacts()
    {
            progressBar.setVisibility(View.VISIBLE);
            db=new DataBaseHelper(ContactsActivity.this);
            ContentResolver cr = getContentResolver();
            Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI,
                    null, null, null, null);
            assert cur != null;
            if (cur.getCount() > 0) {
                while (cur.moveToNext()) {
                    String id = cur.getString(cur.getColumnIndex(ContactsContract.Contacts._ID));
                    if (Integer.parseInt(cur.getString(
                            cur.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0) {
                        Cursor pCur = cr.query(
                                ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
                                null,
                                ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = ?",
                                new String[]{id}, null);
                        assert pCur != null;
                        while (pCur.moveToNext()) {
                            String phoneNo = pCur.getString(pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
                            String Name = pCur.getString(pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
                            db.insert(Name,phoneNo);

                        }
                        pCur.close();
                    }
                }
                progressBar.setVisibility(View.GONE);

                        namelist = db.getData();
                        ArrayAdapter listadapter = new ArrayAdapter<>(getApplication(),android.R.layout.simple_list_item_1,namelist);
                        listView.setAdapter(listadapter);
                        listadapter.notifyDataSetChanged();

                cur.close();
            }
    }

}

我只希望该应用平稳运行而不会阻塞主线程。

3 个答案:

答案 0 :(得分:0)

您应该在辅助线程中调用getContacts()方法,并将结果发送到主线程。

答案 1 :(得分:0)

使用AsyncTask

private class GetContacts extends AsyncTask<String, Void, String> {

@Override
protected String doInBackground(String... params) {
    // Do your background task here [get contacts]
    return "";
}      

@Override
protected void onPostExecute(String result) {               
    //Task to be done after your background task is done like updating UI etc
 }
}

请查看开发者链接以获取详细信息:Async Task

答案 2 :(得分:0)

    new AsyncTask<Void, Void, List<String>>() {
        @Override
        protected void onPreExecute() {
            progressBar.setVisibility(View.VISIBLE);
        }

        @Override
        protected void onPostExecute(List<String> strings) {
            // Ui Thread operations
            progressBar.setVisibility(View.GONE);
            ArrayAdapter listadapter = new ArrayAdapter<>(getApplication(),android.R.layout.simple_list_item_1,strings);
            listView.setAdapter(listadapter);
            listadapter.notifyDataSetChanged();
        }

        @Override
        protected List<String> doInBackground(Void... voids) {

            db=new DataBaseHelper(ContactsActivity.this);
            ContentResolver cr = getContentResolver();
            Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI,
                    null, null, null, null);
            assert cur != null;
            if (cur.getCount() > 0) {
                while (cur.moveToNext()) {
                    String id = cur.getString(cur.getColumnIndex(ContactsContract.Contacts._ID));
                    if (Integer.parseInt(cur.getString(
                            cur.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0) {
                        Cursor pCur = cr.query(
                                ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
                                null,
                                ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = ?",
                                new String[]{id}, null);
                        assert pCur != null;
                        while (pCur.moveToNext()) {
                            String phoneNo = pCur.getString(pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
                            String Name = pCur.getString(pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
                            db.insert(Name,phoneNo);

                        }
                        pCur.close();
                    }
                }
                namelist = db.getData();
                cur.close();
            }

            return namelist;
        }
    };