当我运行我的应用程序时,数据不会添加到我的联系人中

时间:2018-12-06 11:54:02

标签: android kotlin

当我运行我的应用程序时,数据未添加到我的联系人中。但 当我调试应用程序时,联系人将被添加到联系人列表中,那么如何解决此问题?

    private var TAG = "PhoneActivityTAG"
    internal var activity: Activity = this@MainActivity
    private var wantPermission = Manifest.permission.WRITE_CONTACTS
    private val PERMISSION_REQUEST_CODE = 1

    val ops = ArrayList<ContentProviderOperation>()
    val rawContactInsertIndex = ops.size

    ops.add(
        ContentProviderOperation.newInsert(RawContacts.CONTENT_URI)
            .withValue(RawContacts.ACCOUNT_TYPE, null)
            .withValue(RawContacts.ACCOUNT_NAME, null).build()
    )

    //Phone Number
    ops.add(
        ContentProviderOperation
            .newInsert(ContactsContract.Data.CONTENT_URI)
            .withValueBackReference(
                ContactsContract.Data.RAW_CONTACT_ID,
                rawContactInsertIndex
            )
            .withValue(ContactsContract.Data.MIMETYPE, Phone.CONTENT_ITEM_TYPE)
            .withValue(Phone.NUMBER, "1234567890")
            .withValue(ContactsContract.Data.MIMETYPE, Phone.CONTENT_ITEM_TYPE)
            .withValue(Phone.TYPE, "1").build()
    )
    //Display name/Contact name
    ops.add(
        ContentProviderOperation
            .newInsert(ContactsContract.Data.CONTENT_URI)
            .withValueBackReference(
                ContactsContract.Data.RAW_CONTACT_ID,
                rawContactInsertIndex
            )
            .withValue(ContactsContract.Data.MIMETYPE, StructuredName.CONTENT_ITEM_TYPE)
            .withValue(StructuredName.DISPLAY_NAME, "xyz").build()
    )

    try {
        contentResolver.applyBatch(ContactsContract.AUTHORITY, ops)
        Log.d("test", "4444")
    } catch (e: Exception) {
        Log.d("test", "5555")
        // Display warning
        val ctx = applicationContext
        val txt = "ContactCreationFailure"
        val duration = Toast.LENGTH_SHORT
        val toast = Toast.makeText(ctx, txt, duration)
        toast.show()

        // Log exception
       Log.e("test", "Exception encountered while inserting contact: $e")
    }

    if (!checkPermission(wantPermission)) {
        requestPermission(wantPermission)
    } else {
        Log.d(TAG, "Phone number: $ops")
    }

}

private fun requestPermission(permission: String) {
    if (ActivityCompat.shouldShowRequestPermissionRationale(activity, permission)) {
        Toast.makeText(activity,"Phone state permission allows us to get phone number. Please allow it for additional functionality.", Toast.LENGTH_LONG).show()
    }
    ActivityCompat.requestPermissions(activity, arrayOf(permission), PERMISSION_REQUEST_CODE)
}

override fun onRequestPermissionsResult(requestCode: Int, permissions: Array<String>, grantResults: IntArray) {

    if (grantResults.isNotEmpty() && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
        Toast.makeText(activity, "Permission Granted", Toast.LENGTH_LONG).show()
        //Log.d(TAG, "Phone number: ")
    } else {
        Toast.makeText(activity, "Permission Denied. We can't get phone number.", Toast.LENGTH_LONG).show()
    }
}

private fun checkPermission(permission: String): Boolean {
    return if (Build.VERSION.SDK_INT >= 23) {
        val result = ContextCompat.checkSelfPermission(activity, permission)
        result == PackageManager.PERMISSION_GRANTED
    } else {
        true
    }

0 个答案:

没有答案