android > CallLog 通话记录

.java

 

package test.mft;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

import android.app.Activity;
import android.app.TabActivity;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.provider.CallLog;
import android.util.Log;
import android.widget.TabHost;


public class TestActivity extends Activity {
    /** Called when the activity is first created. */
	
	
	ArrayList<String> outgoingList = new ArrayList<String>();
	ArrayList<String> incomingList = new ArrayList<String>();
	ArrayList<String> missedcallList = new ArrayList<String>();
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		readCallLogs();
	}
	private void readCallLogs() {
		missedcallList.clear();
		incomingList.clear();
		outgoingList.clear();

		/*Query Call Log Content Provider*/
		Cursor callLogCursor = getContentResolver().query(android.provider.CallLog.Calls.CONTENT_URI, null, null, null, android.provider.CallLog.Calls.DEFAULT_SORT_ORDER);

		/*Check if cursor is not null*/
		if (callLogCursor != null) {

			/*Loop through the cursor*/
			while (callLogCursor.moveToNext()) {
	
				/*Get ID of call*/
				String id = callLogCursor.getString(callLogCursor.getColumnIndex(CallLog.Calls._ID));
				
				/*Get Contact Name*/
				String name = callLogCursor.getString(callLogCursor.getColumnIndex(CallLog.Calls.CACHED_NAME));
				
				/*Get Contact Cache Number*/
				String cacheNumber = callLogCursor.getString(callLogCursor.getColumnIndex(CallLog.Calls.CACHED_NUMBER_LABEL));
				
				/*Get Contact Number*/
				String number = callLogCursor.getString(callLogCursor.getColumnIndex(CallLog.Calls.NUMBER));
				
				/*Get Date and time information*/
				long dateTimeMillis = callLogCursor.getLong(callLogCursor.getColumnIndex(CallLog.Calls.DATE));
				long durationMillis = callLogCursor.getLong(callLogCursor.getColumnIndex(CallLog.Calls.DURATION));
				
				/*Get Call Type*/
				int callType = callLogCursor.getInt(callLogCursor.getColumnIndex(CallLog.Calls.TYPE));
				
				String duration = durationMillis+""; //通话时间长度(秒)
				
				//时间戳转换
				SimpleDateFormat format =   new SimpleDateFormat( "yyyy-MM-dd HH:mm:ss" );
				String dateString = format.format(dateTimeMillis);// dateTimeMillis = 1365514019004;  通话时间(什么时候通话的)
				
				if (cacheNumber == null)
						cacheNumber = number; 
				
				if (name == null)
					name = "No Name"; 
	
				/*Create Model Object*/
				//CallLogModel callLogModel = new CallLogModel(name, cacheNumber, duration, dateString);
				String callLogString = "{name='"+name+"',cacheNumber='"+cacheNumber+"',duration='"+duration+"',dateString='"+dateString+"'}";
	
				/*Add it into respective ArrayList*/
				if (callType == CallLog.Calls.OUTGOING_TYPE) {
					outgoingList.add(callLogString);
				} else if (callType == CallLog.Calls.INCOMING_TYPE) {
					incomingList.add(callLogString);
				} else if (callType == CallLog.Calls.MISSED_TYPE) {
					missedcallList.add(callLogString);
				}
			}
			/*Close the cursor*/
			callLogCursor.close();
		}
		Log.d("log1", "outgoingList:"+outgoingList);
		Log.d("log1", "incomingList:"+incomingList);
		Log.d("log1", "missedcallList:"+missedcallList);
	}

	
	
	
}

 

 

  加权限

<uses-permission android:name="android.permission.READ_CONTACTS" > </uses-permission>

 

 

<uses-permission android:name="android.permission.READ_CALL_LOG"></uses-permission>

<uses-permission android:name="android.permission.WRITE_CALL_LOG"></uses-permission>

 

 

你可能感兴趣的:(android)