我正在尝试使用另一个应用程序在 WhatsApp 中发送短信。
假设我们有一个按钮,在按钮上单击您调用下面的方法。
sendTextMsgOnWhatsApp("+91 9876543210", "你好,这是我的测试信息");
public void sendTextMsgOnWhatsApp(String sContactNo, String sMessage) {
String toNumber = sContactNo; // contains spaces, i.e., example +91 98765 43210
toNumber = toNumber.replace("+", "").replace(" ", "");
/*this method contactIdByPhoneNumber() will get unique id for given contact,
if this return's null then it means that you don't have any contact save with this mobile no.*/
String sContactId = contactIdByPhoneNumber(toNumber);
if (sContactId != null && sContactId.length() > 0) {
/*
* Once We get the contact id, we check whether contact has a registered with WhatsApp or not.
* this hasWhatsApp(hasWhatsApp) method will return null,
* if contact doesn't associate with whatsApp services.
* */
String sWhatsAppNo = hasWhatsApp(sContactId);
if (sWhatsAppNo != null && sWhatsAppNo.length() > 0) {
Intent sendIntent = new Intent("android.intent.action.MAIN");
sendIntent.putExtra("jid", toNumber + "@s.whatsapp.net");
sendIntent.putExtra(Intent.EXTRA_TEXT, sMessage);
sendIntent.setAction(Intent.ACTION_SEND);
sendIntent.setPackage("com.whatsapp");
sendIntent.setType("text/plain");
startActivity(sendIntent);
} else {
// this contact does not exist in any WhatsApp application
Toast.makeText(this, "Contact not found in WhatsApp !!", Toast.LENGTH_SHORT).show();
}
} else {
// this contact does not exist in your contact
Toast.makeText(this, "create contact for " + toNumber, Toast.LENGTH_SHORT).show();
}
}
private String contactIdByPhoneNumber(String phoneNumber) {
String contactId = null;
if (phoneNumber != null && phoneNumber.length() > 0) {
ContentResolver contentResolver = getContentResolver();
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;
}
public String hasWhatsApp(String contactID) {
String rowContactId = null;
boolean hasWhatsApp;
String[] projection = new String[]{ContactsContract.RawContacts._ID};
String selection = ContactsContract.RawContacts.CONTACT_ID + " = ? AND " + ContactsContract.RawContacts.ACCOUNT_TYPE + " = ?";
String[] selectionArgs = new String[]{contactID, "com.whatsapp"};
Cursor cursor = getContentResolver().query(ContactsContract.RawContacts.CONTENT_URI, projection, selection, selectionArgs, null);
if (cursor != null) {
hasWhatsApp = cursor.moveToNext();
if (hasWhatsApp) {
rowContactId = cursor.getString(0);
}
cursor.close();
}
return rowContactId;
}
在AndroidManifest.xml文件中添加以下权限
<uses-permission android:name="android.permission.READ_CONTACTS" />
<uses-permission android:name="android.permission.WRITE_CONTACTS" />
<uses-permission android:name="android.permission.INTERNET" />