如何避免内容提供程序阻止主(UI)执行线程

时间:2018-06-14 10:39:19

标签: android performance android-contentprovider calllog

在我的应用程序中,我使用getContentResolver()访问通话记录和短信。这意味着我正在使用android content-providergetContentResolver()正在后台服务中运行。一切都还可以,但我的主要挑战是:每当我启动服务运行提取呼叫日志和文本消息的代码时,内容提供程序就会阻止我的主要执行线程。我试图将这些代码放在不同的执行线程中,但没有任何改变。

我真的需要帮助;我怎么能避免这种情况?因为应用程序在具有较小RAM和PROCESSORS的设备上不断提供ANR(应用程序无响应)消息。

如果您需要我显示代码,我准备粘贴,如果真的需要。

更新:(后台服务中的代码)

public class Getting_Call_log_Service extends Service {

//variables of getting call logs
private String ph_number;
private String temp_name;
private String real_name;
private String call_type;
private String dir;
private String call_date;
private String call_duration;
private Date call_day_time;
private SQLite_database_helper_class myDb;

public  class callsThread implements Runnable{
    callsThread(){}

    @Override
    public void run() {

            myDb=new SQLite_database_helper_class(getApplicationContext());
            //codes of extracting call logs
            Cursor cursor = getContentResolver().query(CallLog.Calls.CONTENT_URI,
                    null,null,null,null);

            int number=cursor.getColumnIndex(CallLog.Calls.NUMBER);//to get the index of phoneNumber column
            int name = cursor.getColumnIndex(CallLog.Calls.CACHED_NAME);//to get the index of contact name column
            int type = cursor.getColumnIndex(CallLog.Calls.TYPE);//to get the call type index
            int callDate = cursor.getColumnIndex(CallLog.Calls.DATE);//to get the date of calling
            int callDuration = cursor.getColumnIndex(CallLog.Calls.DURATION);//to get the duration of calling


            cursor.moveToFirst();

                do {

                    //extracting values from phone history
                    ph_number=cursor.getString(number);
                    temp_name=cursor.getString(name);
                    if (temp_name==null){
                        real_name="Annonymous caller!";
                    }else {
                        real_name=temp_name;
                    }
                    call_type=cursor.getString(type);
                    int dir_code=Integer.parseInt(call_type);
                    switch (dir_code) {
                        case CallLog.Calls.INCOMING_TYPE:
                            dir = "INCOMING CALL";
                            break;
                        case CallLog.Calls.OUTGOING_TYPE:
                            dir = "OUTGOING CALL";
                            break;

                        case CallLog.Calls.MISSED_TYPE:
                            dir = "MISSED CALL";
                            break;
                        case CallLog.Calls.REJECTED_TYPE:
                            dir = "REJECTED CALL";
                            break;
                        case CallLog.Calls.ANSWERED_EXTERNALLY_TYPE:
                            dir = "ANSWERED EXTERNALLY";
                            break;
                        case CallLog.Calls.BLOCKED_TYPE:
                            dir = "BLOCKED NUMBER";
                            break;
                        case CallLog.Calls.VOICEMAIL_TYPE:
                            dir = "VOICEMAIL";
                            break;
                    }
                    call_date=cursor.getString(callDate);
                    call_day_time=new Date(Long.valueOf(call_date));

                    call_duration=cursor.getString(callDuration);
                    //end of extraction

                            myDb.saving_call_logs(ph_number,real_name,dir,
                                    call_day_time.toString(), call_duration);
                }while (cursor.moveToNext());
                cursor.close();

        }
    }

public void calls_fromPhone(){

    Thread callThread=new Thread(new callsThread());
    callThread.start();

}


private void call_retriever(){

        TelephonyManager telephonyManager=(TelephonyManager)getSystemService(
                Context.TELEPHONY_SERVICE);

        PhoneStateListener phoneStateListener=new PhoneStateListener(){

            @Override
            public void onCallStateChanged(int state,String inComingNumber){
                if (state==TelephonyManager.CALL_STATE_IDLE){
                    //do stuff here

                    calls_fromPhone();
}

};
  telephonyManager.listen(phoneStateListener,
PhoneStateListener.LISTEN_CALL_STATE);
    }
@Override
public int onStartCommand(Intent intent,int flags, int startId){

    call_retriever();
return START_STICKY;
}

0 个答案:

没有答案