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>
相关推荐
沐怡旸14 分钟前
【底层机制】垃圾回收(GC)底层原理深度解析
android·面试
whatever who cares42 分钟前
android/java中gson的用法
android·java·开发语言
用户0273851840261 小时前
【Android】活动的正/异常生命周期和启动模式、标志位详解
android
nono牛2 小时前
MTK平台详解`adb devices`输出的序列号组成
android·linux·adb·智能手机
zhangphil2 小时前
Android通过SQL查询trace分析进程启动线程总数量
android
下位子3 小时前
『OpenGL学习滤镜相机』- Day3: 着色器基础 - GLSL 语言
android·opengl
bqliang3 小时前
Jetpack Navigation 3:领航未来
android·android studio·android jetpack
云存储小天使3 小时前
安卓蛙、苹果蛙为什么难互通?
android
陈大头铃儿响叮当5 小时前
Android Studio升级后,Flutter运行android设备报错
android·flutter·android studio
勤劳打代码5 小时前
isar_flutter_libs 引发 Namespace not specified
android·flutter·groovy