Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
367 views
in Technique[技术] by (71.8m points)

Crash : requires android.permission.READ_CALL_LOG or android.permission.WRITE_CALL_LOG On some devices

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You need to get run time permission in 6.0 > devices

get runtime permission like this:

    final String[] NECESSARY_PERMISSIONS = new String[] {Manifest.permission.GET_ACCOUNTS };

    if (ContextCompat.checkSelfPermission(DialerHomeActivity.this,
                    Manifest.permission.GET_ACCOUNTS) == PackageManager.PERMISSION_GRANTED) {

        //Permission is granted

    } else {

        //ask for permission

        ActivityCompat.requestPermissions(
                DialerHomeActivity.this,
                NECESSARY_PERMISSIONS, 123);
    }

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...