Sunday, December 7, 2014

android how to known how long phone call time length

1.what

we need implement one function,from user call to hookoff,calculate the duration time in android phone call.

android 2.3+ not support call status finger the calling duration time.maybe android 4.4+ support some status notify.but we need all plateform support.

2.how

first we can known when caller start dial the phone,by the status ‘offhook’

second we can known when the caller stop call by status ‘idle’

Used the idle time subtract the offhook time,but it contain call waiting timer and real calling timer. And we do not known the waiting timer.

 

After some days, my parter said,can used

and t

 

part 1: known when is the call status updating:

// declare the class

public class MyPhoneCallListener extends PhoneStateListener {

public void onCallStateChanged(int state, String incomingNumber) {
switch (state) {
case TelephonyManager.CALL_STATE_IDLE :
// callingstatus = "idle";
if(is_hookoff)
_stopCallStatusCheck(); // it call getLastCallDuration()
is_hookoff = false;
_isCalling = false;
break;
case TelephonyManager.CALL_STATE_OFFHOOK :
// callingstatus = "connecting";
is_hookoff = true;
// _isCalling = true;
break;
case TelephonyManager.CALL_STATE_RINGING :
// callingstatus = "riging";
// _isCalling = true;
break;
default :
break;
}
super.onCallStateChanged(state, incomingNumber);
}
}



// define the variable

MyPhoneCallListener myPhoneCallListener = new MyPhoneCallListener();



// register it



// TelephonyManager _tm;
_tm = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
_tm.listen(myPhoneCallListener,PhoneStateListener.LISTEN_CALL_STATE);




// unregister it



_tm.listen(myPhoneCallListener,PhoneStateListener.LISTEN_NONE);



 



part 2: calcultare the realing calling time:



    public static long getLastCallDuration(Context c) {
//if(true)
// return 0;
long ret = 0;
Cursor cursor = null;
try{
Thread.sleep(1000);

ContentResolver cc = c.getContentResolver();
if(cc == null)
return ret;

cursor = cc.query(Calls.CONTENT_URI,
new String[] { Calls.DURATION, Calls.TYPE }, null,
null, Calls.DEFAULT_SORT_ORDER);

// Activity a = ServiceManager.Instance().getServiceActivity();
// if(a == null)
// a = BaseActivity.s_pkrss;
// if(a == null)
//// return ret;
//
// cursor = c.getContentResolver().query(CallLog.Calls.CONTENT_URI,
// new String[] { CallLog.Calls.DURATION, CallLog.Calls.TYPE},
// null, null, CallLog.Calls.DEFAULT_SORT_ORDER);

if(cursor == null)
return ret;

// a.startManagingCursor(cursor);
boolean hasRecord = cursor.moveToFirst();
while (hasRecord) {
int type = cursor.getInt(cursor.getColumnIndex(Calls.TYPE));
long duration = cursor.getLong(cursor.getColumnIndex(Calls.DURATION));
switch (type) {
case Calls.OUTGOING_TYPE:
ret += duration;
default:
break;
}
hasRecord = cursor.moveToNext();
}
}catch(Exception e){

}finally{
// try{
if(cursor != null){
cursor.close();
cursor = null;
}
// }catch(Exception e){
//
// }
}

return ret;
}



3.last



Thanks

No comments: