最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Android獲取手機(jī)通話記錄的方法

 更新時間:2016年10月09日 17:16:13   作者:NoOneCode  
這篇文章主要為大家詳細(xì)介紹了Android獲取手機(jī)通話記錄的方法,具有一定的參考價值,感興趣的小伙伴們可以參考一下

Android如何獲取手機(jī)通話記錄,本文為大家揭曉。

獲取手機(jī)通話記錄流程:

1、 獲取ContentResolver;

ContentResolver resolver = getContentResolver();

2、resolver.query(*);

需要傳入通話記錄的URI:CallLog.Calls.CONTENT_URI

3、對查詢得到的Cursor進(jìn)行數(shù)據(jù)獲取.

主要代碼如下:

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);
  }

  /**
   * 讀取數(shù)據(jù)
   * 
   * @return 讀取到的數(shù)據(jù)
   */
  private List<Map<String, String>> getDataList() {
    // 1.獲得ContentResolver
    ContentResolver resolver = getContentResolver();
    // 2.利用ContentResolver的query方法查詢通話記錄數(shù)據(jù)庫
    /**
     * @param uri 需要查詢的URI,(這個URI是ContentProvider提供的)
     * @param projection 需要查詢的字段
     * @param selection sql語句where之后的語句
     * @param selectionArgs ?占位符代表的數(shù)據(jù)
     * @param sortOrder 排序方式
     * 
     */
    Cursor cursor = resolver.query(CallLog.Calls.CONTENT_URI, // 查詢通話記錄的URI
        new String[] { CallLog.Calls.CACHED_NAME// 通話記錄的聯(lián)系人
            , CallLog.Calls.NUMBER// 通話記錄的電話號碼
            , CallLog.Calls.DATE// 通話記錄的日期
            , CallLog.Calls.DURATION// 通話時長
            , CallLog.Calls.TYPE }// 通話類型
        , null, null, CallLog.Calls.DEFAULT_SORT_ORDER// 按照時間逆序排列,最近打的最先顯示
    );
    // 3.通過Cursor獲得數(shù)據(jù)
    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 = "打入";
        break;
      case CallLog.Calls.OUTGOING_TYPE:
        typeString = "打出";
        break;
      case CallLog.Calls.MISSED_TYPE:
        typeString = "未接";
        break;
      default:
        break;
      }
      Map<String, String> map = new HashMap<String, String>();
      map.put("name", (name == null) ? "未備注聯(lián)系人" : name);
      map.put("number", number);
      map.put("date", date);
      map.put("duration", (duration / 60) + "分鐘");
      map.put("type", typeString);
      list.add(map);
    }
    return list;
  }
}

主布局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>

讀取通話記錄的權(quán)限:

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

最終效果圖:

注意:

夜神模擬器貌似無打電話的功能,不要使用夜神測試本例
版主使用的是小米4真機(jī)測試,usb調(diào)試過程中會直接崩潰,需要手動在安全中心給應(yīng)用賦予讀取通話記錄的權(quán)限。(視個人機(jī)器情況,部分機(jī)器可能不需要手動設(shè)置)

源碼下載:http://xiazai.jb51.net/201610/yuanma/androidContentDemo(jb51.net).rar

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • Android開發(fā)之緩沖dialog對話框創(chuàng)建、使用與封裝操作

    Android開發(fā)之緩沖dialog對話框創(chuàng)建、使用與封裝操作

    這篇文章主要介紹了Android開發(fā)之緩沖dialog對話框創(chuàng)建、使用與封裝操作,結(jié)合具體實(shí)例形式分析了Android緩沖dialog對話框的創(chuàng)建、設(shè)置、顯示、關(guān)閉等操作實(shí)現(xiàn)方法,需要的朋友可以參考下
    2017-09-09
  • Android下的CMD命令之關(guān)機(jī)重啟及重啟recovery

    Android下的CMD命令之關(guān)機(jī)重啟及重啟recovery

    這篇文章主要介紹了Android下的CMD命令之關(guān)機(jī)重啟及重啟recovery,本文涉及到cmd命令知識點(diǎn),通過了解cmd命令就可以很容易的實(shí)現(xiàn)此功能了,需要的朋友一起看看吧
    2016-08-08
  • Android中打電話的數(shù)據(jù)流程分析

    Android中打電話的數(shù)據(jù)流程分析

    所有流程的起點(diǎn)是從撥號后按下?lián)芴栨I開始,接下來將對打電話的數(shù)據(jù)流程進(jìn)行源碼分析,感興趣的朋友可以了解下
    2013-01-01
  • Android自定義View實(shí)現(xiàn)多邊形統(tǒng)計圖示例代碼

    Android自定義View實(shí)現(xiàn)多邊形統(tǒng)計圖示例代碼

    這篇文章主要給大家介紹了關(guān)于Android自定義View如何實(shí)現(xiàn)多邊形統(tǒng)計圖的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧。
    2018-01-01
  • 適配android7.0獲取文件的Uri的方法

    適配android7.0獲取文件的Uri的方法

    本篇文章主要介紹了適配android7.0獲取文件的Uri的方法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-09-09
  • Android仿新版微信浮窗效果

    Android仿新版微信浮窗效果

    在新版微信中,可以把瀏覽的文章縮小為浮窗.點(diǎn)擊浮窗繼續(xù)閱讀.這篇文章主要介紹了Android仿新版微信浮窗效果,需要的朋友可以參考下
    2018-06-06
  • Android中通過Notification&NotificationManager實(shí)現(xiàn)消息通知

    Android中通過Notification&NotificationManager實(shí)現(xiàn)消息通知

    關(guān)于通知Notification相信大家都不陌生了,平時上QQ的時候有消息來了或者有收到了短信,手機(jī)頂部就會顯示有新消息什么的,就類似這種。今天就稍微記錄下幾種Notification的用法。3.0以前的通知和3.0以后的通知是有些區(qū)別的。
    2015-10-10
  • Android中View跟隨手指移動效果

    Android中View跟隨手指移動效果

    這篇文章主要介紹了Android中View跟隨手指移動效果,代碼簡單易懂,非常不錯,需要的朋友參考下
    2017-01-01
  • Android自定義View事件分發(fā)流程詳解

    Android自定義View事件分發(fā)流程詳解

    這篇文章主要為大家介紹了Android自定義View事件分發(fā)流程詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-02-02
  • flutter實(shí)現(xiàn)頭部tabTop滾動欄

    flutter實(shí)現(xiàn)頭部tabTop滾動欄

    這篇文章主要為大家詳細(xì)介紹了flutter實(shí)現(xiàn)頭部tabTop滾動欄,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-03-03

最新評論

宕昌县| 博乐市| 永春县| 新源县| 乐清市| 惠来县| 陇南市| 子洲县| 余姚市| 崇义县| 华安县| 昌图县| 大石桥市| 辽源市| 庆元县| 周口市| 丘北县| 都安| 商洛市| 宁德市| 辽源市| 盐津县| 汕头市| 大田县| 偏关县| 武威市| 石门县| 浙江省| 察哈| 会东县| 丘北县| 宝山区| 关岭| 正安县| 友谊县| 阿克苏市| 南投县| 阆中市| 丰宁| 永安市| 大埔县|