I have made an app that tracks call log in background using ContentObserver and service(runs in foreground).
Manifest.permission.CALL_PHONE
This is the permission i am checking for
if (CallTrackingHelper.isCallPermissionEnabled(appContext)) {
val task = OneoffTask.Builder()
.setService(CallLogService::class.java)
.setExecutionWindow(0, 6)
.setTag("call-track")
.setRequiredNetwork(Task.NETWORK_STATE_ANY)
.setRequiresCharging(false)
.setUpdateCurrent(true)
.build()
val mGcmNetworkManager = GcmNetworkManager.getInstance(appContext)
mGcmNetworkManager.schedule(task)
}
i start a gcmtaskservice that uploads call-log to server
override fun onRunTask(p0: TaskParams?): Int {
val callLogs = CallTrackingHelper.getCallLog(lastSyncedIndex, this)
//
}
in service i fetch contact
fun getCallLog(id: Long, service: CallLogService): ArrayList<CallLogModel> {
val callLogList = ArrayList<CallLogModel>()
if (!isCallPermissionEnabled(service)) {
return callLogList
}
var mCursor: Cursor? = null
try {
val args = arrayOf(id.toString())
if (id != 0L) {
mCursor = UCApplication
.getInstance()
.applicationContext
.contentResolver
.query(
CallLog.Calls.CONTENT_URI,
null,
"_id > ? ",
args,
null
)
} else {
mCursor = UCApplication
.getInstance()
.applicationContext
.contentResolver
.query(
CallLog.Calls.CONTENT_URI,
null,
null,
null,
CallLog.Calls._ID + " DESC" + " LIMIT 200 "
)
}
} catch (e: SecurityException) {
//
}
//
return callLogList
}
fun isCallPermissionEnabled(context: Context?): Boolean {
if (context == null || ContextCompat.checkSelfPermission(context, PERMISSIONS)
!= PackageManager.PERMISSION_GRANTED) {
return false
}
return true
}
Still i am getting lot of securityException so I have to add try and catch
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…