本文章總結了一段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();
獲取聯系人通話時間的長短java代碼
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();