Android获取通话时间实例分析
本文章总结了一段Android获取通话时间程序代码,有需要的朋友可参考一下。
我们知道安卓系统中通话时长应该是归Callog管,所以建议去查查ContactProvider,或者是TelephonyProvider
Service测试
可以的通话开始的时候启动Service 记录当前时间A, 然后stopSelf(); 另外在通话结束的时候再次启动一下Service,再次获得当前时间B, 然后把时间A和B进行比较处理
String time = Long.toString(比较后处理的时间)
Toast.makeText(this, time, Toast.LENGTH_SHORT).show();
使之显示出来 ,再stopSelf();
Cursor cursor = getContentResolver().query(Calls.CONTENT_URI,
new String[] { Calls.DURATION, Calls.TYPE, Calls.DATE },
null,
null,
Calls.DEFAULT_SORT_ORDER);
MainActivity.this.startManagingCursor(cursor);
boolean hasRecord = cursor.moveToFirst();
long incoming = 0L;
long outgoing = 0L;
int count = 0;
while (hasRecord) {
int type = cursor.getInt(cursor.getColumnIndex(Calls.TYPE));
long duration = cursor.getLong(cursor.getColumnIndex(Calls.DURATION));
switch (type) {
case Calls.INCOMING_TYPE:
incoming += duration;
break;
case Calls.OUTGOING_TYPE:
outgoing += duration;
default:
break;
}
count++;
hasRecord = cursor.moveToNext();
}
Toast.makeText(MainActivity.this,
"共计 " + count + "次通话 . 总通话时长 " + (incoming + outgoing) + "秒. 其中接听 " + incoming + " 秒, 拔打 "
+ outgoing + " 秒.",
Toast.LENGTH_LONG).show();