: The method of Android programming to operate the mobile phone call record

The process of obtaining mobile phone call records:

1. Get ContentResolver; 

ContentResolver resolver = getContentResolver(); 

2、resolver.query(*); 

URI for incoming call logs: CallLog.Calls.CONTENT_URI ("content://call_log/calls")

3. Obtain data from the Cursor obtained by the query.

The source code of the content provider responsible for storing call records is under the ContactsProvider project:

Source path: com/ Android /providers/contacts/ CallLogProvider.Java

The database used is: /data/data/com.android.providers.contacts/databases/contacts2.db

Table name: calls

View phone records:  

CallLog.Calls.CONTENT_URI : Equivalent to: Uri.parse("content://call_log/calls");

Contact name for CallLog.Calls.CACHED_NAME query

CallLog.Calls.NUMBER phone number

CallLog.Calls.TYPE Phone Type

 There are three types of call records: (Incoming calls: 1, Outgoing calls: 2, Missing calls: 3)

       Incoming calls: CallLog.Calls.INCOMING_TYPE (constant value: 1)

       Dialed: CallLog.Calls.OUTGOING_TYPE (constant value: 2)

       Missed: CallLog.Calls.MISSED_TYPE (constant value: 3)

 

Date of the call (the millisecond value to be processed when the CallLog.Calls.DATE date is returned)

      SimpleDateFormat simple = new SimpleDateFormat("yyyy-MM-dd "+"hh:mm:ss");

      Date dates = new Date(Long.parseLong(date));

      String time=simple.format(dates);

Query all records

       ContentResolver resolver = getContentResolver();

       resolver.query(CallLog.Calls.CONTENT_URI, null, null, null, null);

Query all records of a contact (by phone number)

       resolver.query(CallLog.Calls.CONTENT_URI, null, "number=?", new String[]{"15101689022"}, null);

Query all missed call records of a contact (by phone number)

       resolver.query(CallLog.Calls.CONTENT_URI, null, "number=? and type=3", new String[]{"15101689022"}, null);

 

The main code is as follows:

MainActivity.java

package com.noonecode.contentresolvercalllogdemo;
 
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
 
import android.app.Activity;
import android.content.ContentResolver;
import android.database.Cursor;
import android.os.Bundle;
import android.provider.CallLog;
import android.widget.ListView;
import android.widget.SimpleAdapter;
 
public class MainActivity extends Activity {
  private ListView mLvShow;
  private List<Map<String, String>> dataList;
  private SimpleAdapter adapter;
 
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate (savedInstanceState);
    setContentView(R.layout.activity_main);
    mLvShow = (ListView) findViewById(R.id.lv_show);
    dataList = getDataList();
    adapter = new SimpleAdapter(this, dataList, R.layout.simple_calllog_item//
        , new String[] { "name", "number", "date", "duration", "type" }//
        , new int[] { R.id.tv_name, R.id.tv_number, R.id.tv_date, R.id.tv_duration, R.id.tv_type });
    mLvShow.setAdapter(adapter);
  }
 
  /**
   * read data
   *
   * @return read data
   */
  private List<Map<String, String>> getDataList() {
    // 1. Get ContentResolver
    ContentResolver resolver = getContentResolver();
    // 2. Use the query method of ContentResolver to query the call record database
    /**
     * @param uri The URI that needs to be queried, (this URI is provided by ContentProvider)
     * @param projection The field to be queried
     * The statement after @param selection sql statement where
     * @param selectionArgs ? The data represented by the placeholder
     * @param sortOrder sort order
     *
     */
    Cursor cursor = resolver.query(CallLog.Calls.CONTENT_URI, // Query call log URI
        new String[] { CallLog.Calls.CACHED_NAME// Contact for call log
            , CallLog.Calls.NUMBER// The phone number of the call log
            , CallLog.Calls.DATE// The date of the call log
            , CallLog.Calls.DURATION// call duration
            , CallLog.Calls.TYPE }// call type
        , null, null, CallLog.Calls.DEFAULT_SORT_ORDER// Arrange in reverse time order, the most recent call is displayed first
    );
    // 3. Get data through Cursor
    List<Map<String, String>> list = new ArrayList<Map<String, String>>();
    while (cursor.moveToNext()) {
      String name = cursor.getString(cursor.getColumnIndex(CallLog.Calls.CACHED_NAME));
      String number = cursor.getString(cursor.getColumnIndex(CallLog.Calls.NUMBER));
      long dateLong = cursor.getLong(cursor.getColumnIndex(CallLog.Calls.DATE));
      String date = new SimpleDateFormat("yyyy-MM-dd HH-mm-ss").format(new Date(dateLong));
      int duration = cursor.getInt(cursor.getColumnIndex(CallLog.Calls.DURATION));
      int type = cursor.getInt(cursor.getColumnIndex(CallLog.Calls.TYPE));
      String typeString = "";
      switch (type) {
      case CallLog.Calls.INCOMING_TYPE:
        typeString = "Enter";
        break;
      case CallLog.Calls.OUTGOING_TYPE:
        typeString = "Type out";
        break;
      case CallLog.Calls.MISSED_TYPE:
        typeString = "未接";
        break;
      default:
        break;
      }
      Map<String, String> map = new HashMap<String, String>();
      map.put("name", (name == null) ? "Unremarked contact" : name);
      map.put("number", number);
      map.put("date", date);
      map.put("duration", (duration / 60) + "分钟");
      map.put("type", typeString);
      list.add(map);
    }
    return list;
  }
}

 Main layout activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:tools="http://schemas.android.com/tools"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  tools:context="com.noonecode.contentresolvercalllogdemo.MainActivity" >
 
  <ListView
    android:id="@+id/lv_show"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />
 
</RelativeLayout>

 simple_calllog_item.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:orientation="vertical"
  android:padding="10dp" >
 
  <TextView
    android:id="@+id/tv_name"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="name"
    android:textSize="20sp" />
 
  <LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal" >
 
    <TextView
      android:id="@+id/tv_number"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_margin="4dp"
      android:text="number"
      />
 
    <TextView
      android:id="@+id/tv_date"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_margin="4dp"
      android:text="date"
      />
 
    <TextView
      android:id="@+id/tv_duration"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_margin="4dp"
      android:text="duration"
      />
 
    <TextView
      android:id="@+id/tv_type"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_margin="4dp"
      android:text="type"
      />
  </LinearLayout>
 
</LinearLayout>

 Permission to read call records:

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

Final renderings: 

Notice:

The night god simulator does not seem to have the function of making calls. Do not use the night god test. In this example, the
moderator uses the Xiaomi Mi 4 real machine test. During the usb debugging process, it will crash directly. You need to manually assign the app to read the call record in the security center. permissions. (Depending on your personal machine, some machines may not require manual settings)

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326323105&siteId=291194637