In my application, I am accessing call log and text messages using getContentResolver()
It means I am working with android content-provider
. That getContentResolver()
is running in a background service. Everything is okay, but my main challenge is this: whenever I start the service to run the codes which extract call log and text messages, the content provider blocks my main thread of execution. I tried to put those codes in a different thread of execution, but nothing is changing.
I really need help; how can I avoid that? Because the application is continually giving a ANR (Application Not Responding) message on devices which have small RAM and PROCESSORS.
In case you need me to show codes, I am ready to paste if is really needed.
UPDATE: (codes in a background service)
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;
}