Android源码开发之通话计时功能

来源:互联网 发布:淘宝如何联系卖家客服 编辑:程序博客网 时间:2024/04/29 16:43

一开始拿到这个问题,搜了搜资料,网上有一大堆,不过,貌似都没有什么用,都是讲的理论知识,并没有实践。最开始的思路是,监听通话状态的广播,通话开始计时,挂断结束,但这样,又不好区分是主叫还是接听,拨打112等电话可能也被计时,果断放弃了这个。接着,想从calllog入手,通过查询calllog来获取通话时长,不过,如果把通话记录删掉,也是无济于事,况且boss还要求通话计时,即使恢复出厂设置,数据也不被擦除,这样显然不行。后来看看源码,发现了倪端,每次挂断电话,都会把通话日志插入数据库,我们就可以去这里入手。

frameworks/base/core/java/android/provider/CallLog.java

-----------------------

        public static Uri addCall(CallerInfo ci, Context context, String number,
                int presentation, int callType, int features, PhoneAccountHandle accountHandle,
                long start, int duration, Long dataUsage, boolean addForAllUsers,
                long conferenceCallId) {

-----------------------

Intent intent = new Intent("com.android.action.CALL_TOTAL_CALCULATE");//携带数据,发送广播,数据累加不在这里做运算
Bundle bundle = new Bundle();
bundle.putLong("duration",Long.valueOf(duration));
bundle.putInt("type", Integer.valueOf(callType));//通话类型,主叫还是接听
intent.putExtras(bundle);
context.sendBroadcast(intent); 
    Log.i("dial","duration = "+duration);
    Log.i("dial","type = "+callType);

接下来,想必很简单了吧

//接收广播,做运算

 private BroadcastReceiver mIntentReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
   if(action.equals(CALL_TOTAL)){
Bundle bundle = new Bundle();
bundle = intent.getExtras();
Long duration = bundle.getLong("duration",0l);
int callType = bundle.getInt("type",CallLog.Calls.MISSED_TYPE);
if(callType == CallLog.Calls.OUTGOING_TYPE){//call out
callOutTimeTemp = Settings.System.getLong(getContentResolver(), "call_calculate_out", 0l);
callOutTime = callOutTimeTemp + duration;
}else if(callType == CallLog.Calls.INCOMING_TYPE){//call in
callInTimeTemp = Settings.System.getLong(getContentResolver(), "call_calculate_in", 0l);
callInTime = callInTimeTemp + duration;
}else{
callOutTime = Settings.System.getLong(getContentResolver(), "call_calculate_out", 0l);
callInTime = Settings.System.getLong(getContentResolver(), "call_calculate_in", 0l);
}
Settings.System.putLong(getContentResolver(), "call_calculate_out", callOutTime);
Settings.System.putLong(getContentResolver(), "call_calculate_in", callInTime);
callValue = longToByte(callOutTime,callInTime);
Log.i("dial", "onReceive.callOutTime1 = "+callOutTime+"\n"+"callInTime ="+callInTime);
}else if(action.equals(CALL_DELETE)){
int flag = Settings.System.getInt(getContentResolver(), "call_calculate_flag", 0);
if(flag == 1){
callOutTime = 0l;
callInTime = 0l;
Settings.System.putLong(getContentResolver(), "call_calculate_out", callOutTime);
Settings.System.putLong(getContentResolver(), "call_calculate_in", callInTime);
callValue = longToByte(callOutTime,callInTime);
Settings.System.putInt(getContentResolver(), "call_calculate_flag", 0);
}
}
Log.i("dial", "onReceive.callOutTime3 = "+callOutTime+"\n"+"callInTime1 ="+callInTime);
 }
};

接下来就是显示问题

TextView out = (TextView) findViewById(R.id.out);
TextView in = (TextView) findViewById(R.id.in);
TextView sum = (TextView) findViewById(R.id.sum);

callOutTime = Settings.System.getLong(getContentResolver(), "call_calculate_out", 0l);
callInTime = Settings.System.getLong(getContentResolver(), "call_calculate_in", 0l);
callTotal = callOutTime + callInTime;
       Log.i("dial","CallTotalCalculate.callOutTime = "+callOutTime);
       Log.i("dial","CallTotalCalculate.callInTime = "+callInTime);
out.setText(getCalculateTime(callOutTime));
in.setText(getCalculateTime(callInTime));
sum.setText(getCalculateTime(callTotal));

关键代码已经给出,谢谢支持,不明白的可以指出,我一定会在第一时间回复

0 0
原创粉丝点击