zoukankan      html  css  js  c++  java
  • 16读取通话记录

    • 所需权限

      <!-- 获取拨打电话的记录权限 -->
          <uses-permission android:name="android.permission.READ_CALL_LOG"/>
    • layout 布局文件
      mainActivity

      <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"
          android:paddingBottom="@dimen/activity_vertical_margin"
          android:paddingLeft="@dimen/activity_horizontal_margin"
          android:paddingRight="@dimen/activity_horizontal_margin"
          android:paddingTop="@dimen/activity_vertical_margin"
          tools:context=".MainActivity" >
      
          <ListView
              android:id="@+id/lv"
              android:layout_width="match_parent"
              android:layout_height="match_parent"
            />
      
          <TextView 
              android:id="@+id/tv"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:text="暂无数据"
              android:layout_centerInParent="true"
              android:textColor="#f00"
              android:textSize="30sp"
              />
      
      </RelativeLayout>
      

    list填充文件

    <?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" >
    
        <TextView 
            android:id="@+id/tv_number"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            />
        <TextView 
            android:id="@+id/tv_date"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            />
    
    
    </LinearLayout>
    
    • 逻辑代码 mainActivity.java

      package com.qf.day16_contentresolver_call_demo2;
      
      import java.text.SimpleDateFormat;
      import java.util.ArrayList;
      import java.util.HashMap;
      import java.util.List;
      import java.util.Map;
      import java.util.logging.SimpleFormatter;
      
      import android.net.Uri;
      import android.os.Bundle;
      import android.provider.CallLog;
      import android.app.Activity;
      import android.content.ContentResolver;
      import android.database.Cursor;
      import android.view.Menu;
      import android.widget.ListView;
      import android.widget.SimpleAdapter;
      import android.widget.TextView;
      
      /**
       * 
       * 通话记录的数据
       * 
       * 1,获取ContentResolver对象 2,ContentResolver对象 通过Uri地址 获取数据 3,展示数据
       * 
       */
      public class MainActivity extends Activity {
      
          private ListView lv;
          private TextView tv;
      
          private ContentResolver contentResolver;
          //content://call_log/calls
          private Uri callUri = CallLog.Calls.CONTENT_URI;
      
          private String [] coumns = new String[]{CallLog.Calls._ID,CallLog.Calls.NUMBER,CallLog.Calls.DATE};
      
      
          //存数据源的集合
          private List<Map<String, Object>> list = new ArrayList<Map<String,Object>>();
      
          SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
      
          @Override
          protected void onCreate(Bundle savedInstanceState) {
              super.onCreate(savedInstanceState);
              setContentView(R.layout.activity_main);
      
              lv = (ListView) findViewById(R.id.lv);
              tv = (TextView) findViewById(R.id.tv);
      
              contentResolver = getContentResolver();
      
              //解析器 通过Uri地址获取指定的字段项
              Cursor cursor = contentResolver.query(callUri, 
                      coumns, 
                      null, 
                      null,
                      null);
      
              while(cursor.moveToNext()){
                  Map<String, Object> map = new HashMap<String, Object>();
                  long id = cursor.getLong(cursor.getColumnIndex(CallLog.Calls._ID));
                  String number = cursor.getString(cursor.getColumnIndex(CallLog.Calls.NUMBER));
                  long date = cursor.getLong(cursor.getColumnIndex(CallLog.Calls.DATE));
      
      
                  map.put("id", id);
                  map.put("number", number);
                  map.put("date", format.format(date));
      
                  list.add(map);
      
              }
      
      
              SimpleAdapter adapter = new SimpleAdapter(MainActivity.this,
                      list, R.layout.item, new String[]{"number","date"}, new int[]{R.id.tv_number,R.id.tv_date});
      
              lv.setAdapter(adapter);
              //如果没有数据  展示VIew
              lv.setEmptyView(tv);
      
          }
      
      }
      
  • 相关阅读:
    工业互联网网络安全渗透测试技术研究
    ios加固,ios代码混淆,ios代码混淆工具, iOS源码混淆使用说明详解
    Java代码加密,Java加密方式,Java加密使用说明
    移动App安全等级保护建议
    Android APP安全问题应对办法的探讨
    工业互联网环境下的工业控制系统安全防护
    保护IoT设备安全的5种方法
    移动App安全等级保护测评防护要点
    Windows下给IDApro 安装yara-python 和findcrypt
    gradle-下载地址
  • 原文地址:https://www.cnblogs.com/muyuge/p/6152243.html
Copyright © 2011-2022 走看看