我使用ContentObserver和服务(在前台运行)创建了一个跟踪后台呼叫记录的应用。 Manifest.permission.CALL_PHONE 这是我正在检查的权限
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)
}
我启动了一个gcmtaskservice,它将call-log上传到服务器
override fun onRunTask(p0: TaskParams?): Int {
val callLogs = CallTrackingHelper.getCallLog(lastSyncedIndex, this)
//
}
在服务中我获取联系
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
}
我仍然得到很多securityException所以我必须添加try和catch
答案 0 :(得分:1)
将其添加到<appication>
标记上方的manifest.xml中:
<uses-permission android:name="android.permission.READ_CALL_LOG"></uses-permission>
<uses-permission android:name="android.permission.WRITE_CALL_LOG"></uses-permission>
您可以在here
了解更多信息答案 1 :(得分:1)
您需要获得6.0中的运行时权限&gt;设备
像这样获得运行时权限:
List<String> al = new ArrayList<>();
答案 2 :(得分:1)
问题是,如果用户已经没有应用程序
<uses-permission android:name="android.permission.READ_CALL_LOG"></uses-permission>
<uses-permission android:name="android.permission.WRITE_CALL_LOG"></uses-permission>
在清单中并给出呼叫权限或电话权限,更新后如果在更新呼叫/电话中添加了这些权限仍然是他们但是呼叫记录权限不是那么你需要再次询问。 全新安装对我来说很好
答案 3 :(得分:0)
您是否已请求运行时权限?
// Here, thisActivity is the current activity
if (ContextCompat.checkSelfPermission(thisActivity,
Manifest.permission.READ_CONTACTS)
!= PackageManager.PERMISSION_GRANTED) {
// Should we show an explanation?
if (ActivityCompat.shouldShowRequestPermissionRationale(thisActivity,
Manifest.permission.READ_CONTACTS)) {
// Show an explanation to the user *asynchronously* -- don't block
// this thread waiting for the user's response! After the user
// sees the explanation, try again to request the permission.
} else {
// No explanation needed, we can request the permission.
ActivityCompat.requestPermissions(thisActivity,
new String[]{Manifest.permission.READ_CONTACTS},
MY_PERMISSIONS_REQUEST_READ_CONTACTS);
// MY_PERMISSIONS_REQUEST_READ_CONTACTS is an
// app-defined int constant. The callback method gets the
// result of the request.
}
}
检查一下:
https://developer.android.com/training/permissions/requesting.html
答案 4 :(得分:0)
我面临着同样的问题。将此代码用于运行时权限:
private static final int PHONE_LOG = 1;
private void checkCallLogPermission() {
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.READ_CALL_LOG) != PackageManager.PERMISSION_GRANTED
&& ActivityCompat.checkSelfPermission(this, Manifest.permission.WRITE_CALL_LOG) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this,
new String[]{Manifest.permission.READ_CALL_LOG, Manifest.permission.WRITE_CALL_LOG},PHONE_LOG);
return;
}
}
答案 5 :(得分:0)
@此代码用于android,您可以访问手机的所有联系人日志
private静态最终int PHONE_LOG_PERMISSION = 1;
private void checkCallLogPermission(){
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.READ_CALL_LOG) != PackageManager.PERMISSION_GRANTED
&& ActivityCompat.checkSelfPermission(this, Manifest.permission.WRITE_CALL_LOG) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this,
new String[]{Manifest.permission.READ_CALL_LOG, Manifest.permission.WRITE_CALL_LOG},PHONE_LOG_PERMISSION);
return;
}
}
@Override 公共无效onRequestPermissionsResult(int requestCode,String []权限,int [] grantResults){ 如果(requestCode == PHONE_LOG_PERMISSION){ 如果(grantResults [0] == PackageManager.PERMISSION_GRANTED){ //授予权限 showContacts(); }其他{ Toast.makeText(this,“直到您授予权限,我们才能显示名称”,Toast.LENGTH_SHORT).show(); } }
}