通过联系人号码获取联系人姓名在Android 9.0 Pie中不起作用

时间:2019-02-21 09:56:32

标签: java android list kotlin contacts

我想通过以下方法从联系人列表中获取来电号码的名称。它适用于所有android版本,但在android 9.0 pie中为null。

public String onCreateIncomingConnection (String s) {
    // Get the telephone number from the incoming request URI.
    String phoneNumber = s;

    String displayName = "Unknown caller";
    boolean isCallerInWorkProfile = false;

    // Look up contact details for the caller in the personal and work profiles.
    Uri lookupUri = Uri.withAppendedPath(
            ContactsContract.PhoneLookup.ENTERPRISE_CONTENT_FILTER_URI,
            Uri.encode(phoneNumber));
    Cursor cursor = getContentResolver().query(
            lookupUri,
            new String[]{
                    ContactsContract.PhoneLookup._ID,
                    ContactsContract.PhoneLookup.DISPLAY_NAME,
                    ContactsContract.PhoneLookup.CUSTOM_RINGTONE
            },
            null,
            null,
            null);

    // Use the first contact found and check if they're from the work profile.
    if (cursor != null) {
        try {
            if (cursor.moveToFirst() == true) {
                displayName = cursor.getString(1);
                isCallerInWorkProfile =
                        ContactsContract.Contacts.isEnterpriseContactId(cursor.getLong(0));
            }
        } finally {
            cursor.close();
        }
    }

    // Return a configured connection object for the incoming call.
    // MyConnection connection = new MyConnection();
    // connection.setCallerDisplayName(displayName, TelecomManager.PRESENTATION_ALLOWED);
    //
    // Our app's activity uses this value to decide whether to show a work badge.
   //   connection.setIsCallerInWorkProfile(isCallerInWorkProfile);

    // Configure the connection further ...
    return displayName;
}

我也尝试了一种新方法,但它也无法正常工作,并给了我空值。我从谷歌开发人员文档中获取此代码。我的代码有什么问题,或者请帮助我以哪种方法获取号码的联系人姓名。

$image = $request->file('image');
$filename = time() . $image->getClientOriginalName();
$destination_path = public_path('/image/');
$image->save($destination_path . $filename);
$image->image_path= $destination_path;

0 个答案:

没有答案
相关问题