问题描述
我想从我的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 :
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);
if (cursor != null) {
while (cursor.moveToNext()) {
contactId = cursor.getString(cursor.getColumnIndexOrThrow(ContactsContract.PhoneLookup._ID));
}
cursor.close();
}
return contactId;
}
然后使用此 contactID 和 mimeType 查找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);
}
}
确保添加检查是否已安装“电报”。