以编程方式使用 "user_id" 和 android 进行电报调用

Make telegram call with "user_id" with android programmatically

我想在不打开电报的情况下从我的 android 应用发起电报呼叫。试过了:

public void callToTelegramContact(String user_id) {
    final String appName = "org.telegram.messenger";
    Intent i = new Intent(Intent.ACTION_VIEW);
    i.setData(Uri.parse("tg://openmessage?" + user_id)); // open chan with user
    i.setPackage(appName);
    i.setType("vnd.android.cursor.item/vnd.org.telegram.messenger.android.call"); //mimeType
    i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    startActivity(i);
}

有人知道怎么做吗?我知道这是可能的,因为它是在应用程序“MacroDroid”

中实现的

https://i.stack.imgur.com/c3Ylt.png

我知道怎么做了。 如果您需要实现此功能,请将 URI contactID 与 mimeType 一起使用,即“vnd.android.cursor.item/vnd.org.telegram.messenger.android.call.video”。首先按号码 contactID phone:

private String getContactIdByPhoneNumber(String phoneNumber) {
    ContentResolver contentResolver = getContentResolver();
    String contactId = null;
    Uri uri = Uri.withAppendedPath(ContactsContract.PhoneLookup.CONTENT_FILTER_URI, Uri.encode(phoneNumber));
    String[] projection = new String[]{ContactsContract.PhoneLookup._ID};
    Cursor cursor = contentResolver.query(uri, projection, null, null, null);

    if (cursor != null) {
        while (cursor.moveToNext()) {
            contactId = cursor.getString(cursor.getColumnIndexOrThrow(ContactsContract.PhoneLookup._ID));
        }
        cursor.close();
    }
    return contactId;
}

然后使用此 contactIDmimeType 来查找 URI

private Uri getUriFromPhoneNumber(String phoneNumber) {
    Uri uri = null;
    String contactId = getContactIdByPhoneNumber(phoneNumber);
    String mimeTypeTelegram = "vnd.android.cursor.item/vnd.org.telegram.messenger.android.call";
    Cursor cursorTelegram = getContentResolver().query(
            ContactsContract.Data.CONTENT_URI,
            new String[]{ContactsContract.Data._ID},
            ContactsContract.Data.CONTACT_ID + "=? AND " + ContactsContract.Data.MIMETYPE + "=?",
            new String[]{contactId, mimeTypeTelegram}, null);
    if (cursorTelegram != null) {
        while (cursorTelegram.moveToNext()) {
            String id = cursorTelegram.getString(cursorTelegram.getColumnIndexOrThrow(ContactsContract.Data._ID));
            if (!TextUtils.isEmpty(id)) {
                uri = Uri.parse(ContactsContract.Data.CONTENT_URI + "/" + id);
                break;
            }
        }
        cursorTelegram.close();
    }
    return uri;
}

然后使用 Intent.ACTION_VIEW

public void сallToTelegramContact(String phoneNumber) {
    Uri uri = getUriFromPhoneNumber(phoneNumber);
    Log.d(TAG, uri + "");
    if (uri != null) {
        Intent intent = new Intent(Intent.ACTION_VIEW);
        intent.setData(uri);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        startActivity(intent);
    }
}

一定要检查是否安装了 'Telegram'。