Android读取拨号记录功能

Android读取拨号记录功能

Android读取拨号记录功能

首先会检测应用是否有读取拨号记录的权限

MainActivity.java

java 复制代码
public class MainActivity extends AppCompatActivity {

    private ListView listCalls;
    private List<Map<String, Object>> mapList;
    private static final int REQUEST_CODE = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        onShowCallLog();
    }

    public void initView() {
        listCalls = (ListView) super.findViewById(R.id.call_list);
        SimpleAdapter simpleAdapter = new SimpleAdapter(
                this,
                mapList,
                R.layout.call_item,
                new String[]{CallLog.Calls.NUMBER, CallLog.Calls.DATE},
                new int[]{R.id.call_mobile, R.id.call_date});
        listCalls.setAdapter(simpleAdapter);
    }

    private void initDate() {
        ContentResolver contentResolver = getContentResolver();
        Cursor cursor = contentResolver.query(CallLog.Calls.CONTENT_URI,
                new String[]{CallLog.Calls.NUMBER, CallLog.Calls.DATE},
                null, null, null);
        mapList = new ArrayList<>();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        while (cursor.moveToNext()) {
            Map<String, Object> stringObjectMap = new HashMap<>();
            stringObjectMap.put(CallLog.Calls.NUMBER, cursor.getString(0));
            stringObjectMap.put(CallLog.Calls.DATE, simpleDateFormat.format(new Date(cursor.getLong(1))));
            mapList.add(stringObjectMap);
        }
        cursor.close();
    }

    private void onShowCallLog() {
        int checkCALL_LOGPermission = ContextCompat.checkSelfPermission(this, Manifest.permission.READ_CALL_LOG);
        if (checkCALL_LOGPermission != PackageManager.PERMISSION_GRANTED) {
            ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.READ_CALL_LOG}, REQUEST_CODE);
        } else {
            initDate();
            initView();
        }
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        if (requestCode == REQUEST_CODE) {
            if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                Toast.makeText(this, "获取权限成功", Toast.LENGTH_SHORT).show();
                initDate();
                initView();
            } else {
                Toast.makeText(this, "获取权限失败", Toast.LENGTH_SHORT).show();
                this.finish();
            }
        } else {
            super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        }
    }
}

activity_main.xml

xml 复制代码
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity"
    android:orientation="vertical">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:orientation="horizontal">

        <TextView
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:gravity="center"
            android:text="号码"
            android:textSize="26sp" />

        <TextView
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:gravity="center"
            android:text="时间"
            android:textSize="26sp" />
    </LinearLayout>


    <ListView
        android:id="@+id/call_list"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

    </ListView>

</LinearLayout>

call_item.xml

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="50dp"
    android:orientation="horizontal">

    <TextView
        android:id="@+id/call_mobile"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:gravity="center"
        android:textSize="24sp" />

    <TextView
        android:id="@+id/call_date"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:gravity="center"
        android:textSize="24sp" />

</LinearLayout>

AndroidManifest.xml

xml 复制代码
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android">


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

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/Theme.Learn">
        <activity
            android:name=".MainActivity"
            android:exported="true">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>
相关推荐
EQ-雪梨蛋花汤1 小时前
【Part 2安卓原生360°VR播放器开发实战】第四节|安卓VR播放器性能优化与设备适配
android·性能优化·vr
每次的天空1 小时前
Android学习总结之kotlin篇(二)
android·学习·kotlin
刘洋浪子1 小时前
Android Studio中Gradle中Task列表显示不全解决方案
android·ide·android studio
橙子199110161 小时前
Kotlin 中 infix 关键字的原理和使用场景
android·开发语言·kotlin
后端码匠8 小时前
MySQL 8.0安装(压缩包方式)
android·mysql·adb
梓仁沐白9 小时前
Android清单文件
android
董可伦11 小时前
Dinky 安装部署并配置提交 Flink Yarn 任务
android·adb·flink
每次的天空12 小时前
Android学习总结之Glide自定义三级缓存(面试篇)
android·学习·glide
恋猫de小郭12 小时前
如何查看项目是否支持最新 Android 16K Page Size 一文汇总
android·开发语言·javascript·kotlin
flying robot14 小时前
小结:Android系统架构
android·系统架构