在某些App的开发者,获取通话记录有时候是必要的(现在都要申请相对于权限哦,App上架得说明为啥获取此权限)。
因为工作风控数据收集经常会用到,在此做个总结。
大致步骤
1.权限声明
2.申请权限
3.获取通话记录
4.生成列表数据上传
一、权限声明
manifest.xml
<uses-permission android:name="android.permission.READ_CALL_LOG" />
二、权限申请
使用EasyPermissions进行权限判断及申请
val CALL_LOG = 4if (EasyPermissions.hasPermissions(requireActivity(), android.Manifest.permission.READ_CALL_LOG)) {LogUtils.e("callInfo", CallUtils().getList(requireActivity() as AppCompatActivity).toString())} else {EasyPermissions.requestPermissions(this@HomeFragment,"需要获取对应权限才能保证功能正常使用",CALL_LOG,android.Manifest.permission.READ_CALL_LOG,)
}
记得你的activity或者fragment一定要继承EasyPermissions.PermissionCallbacks接口重写对应的方法。
override fun onPermissionsGranted(requestCode: Int, perms: MutableList<String>) {LogUtils.e("permission", "onPermissionsGranted $requestCode perms$perms")if (requestCode == AppConstant.PER_CALL) {toCall()}else if (requestCode == AppConstant.CALL_LOG){LogUtils.e("callInfo", CallUtils().getList(requireActivity() as AppCompatActivity).toString())}}override fun onPermissionsDenied(requestCode: Int, perms: MutableList<String>) {LogUtils.e("permission", "onPermissionsDenied $requestCode perms$perms")}override fun onRequestPermissionsResult(requestCode: Int, permissions: Array<out String>, grantResults: IntArray) {super.onRequestPermissionsResult(requestCode, permissions, grantResults)EasyPermissions.onRequestPermissionsResult(requestCode, permissions, grantResults, this)}
三、抓取通话记录
class CallUtils {@SuppressLint("Range")fun getList(mActivity: AppCompatActivity): List<RecordEntity>? {val contentResolver: ContentResolver = mActivity.contentResolvervar cursor: Cursor? = nulltry {cursor = contentResolver.query(CallLog.Calls.CONTENT_URI, null, null, null,CallLog.Calls.DATE + " desc")if (cursor == null) return nullval mRecordList: MutableList<RecordEntity> = ArrayList()while (cursor.moveToNext()) {val record = RecordEntity()record.name = cursor.getString(cursor.getColumnIndex(CallLog.Calls.CACHED_NAME))record.telephoneNo = cursor.getString(cursor.getColumnIndex(CallLog.Calls.NUMBER))record.callType = cursor.getInt(cursor.getColumnIndex(CallLog.Calls.TYPE))record.callDate = cursor.getLong(cursor.getColumnIndex(CallLog.Calls.DATE))record.callDuration = cursor.getLong(cursor.getColumnIndex(CallLog.Calls.DURATION))LogUtils.e("call record", record.toString())mRecordList.add(record)}return mRecordList} finally {cursor?.close()}}}class RecordEntity {var name: String? = nullvar telephoneNo: String? = nullvar callType: Int = 0var callDate: Long = 0var callDuration: Long = 0override fun toString(): String {return "RecordEntity [toString()=$name,$telephoneNo,$callType,$callDate,$callDuration]"}
}
四、数据上传
得到list后,使用retrofit上传数据到服务端
fun uploadCallLog(activity: AppCompatActivity, isUserClick: Boolean) {if (!AppConstant.getIsLogin()) returnval map: MutableMap<String, Any> = HashMap(16)val list = CallUtils().getList(activity)if (list != null) map["records"] = listRetrofitUtil.post(AppApi.UPLOAD_CALL_LOG, map.toMap(), object : RetrofitCallback() {override fun onSuccess(responseBody: BaseResponseBody) {}override fun onError(t: Throwable?, type: Int?) {}})}