如何在安卓应用中使用WhatsApp和WeChat消息功能?
实际需求是使用WhatsApp和WeChat(免费短信)发送短信。
如何在安卓应用中使用WhatsApp和WeChat消息功能?
实际需求是使用WhatsApp和WeChat(免费短信)发送短信。
我有解决方案了。在这里发布答案,希望能帮助其他可能有相同疑问的人。
要通过任何应用程序分享...
public void sendAppMsg(View view) {
Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("text/plain");
String text = " message you want to share..";
// change with required application package
intent.setPackage("PACKAGE NAME OF THE APPLICATION");
if (intent != null) {
intent.putExtra(Intent.EXTRA_TEXT, text);//
startActivity(Intent.createChooser(intent, text));
} else {
Toast.makeText(this, "App not found", Toast.LENGTH_SHORT)
.show();
}
}
注意:根据您的要求更改应用程序的*包名称,例如:
示例:使用
//Whatsapp
intent.setPackage("com.whatsapp");`
//Linkedin
intent.setPackage("com.linkedin.android");
//Twitter
intent.setPackage("com.twitter.android");
//Facebook
intent.setPackage("com.facebook.katana");
//GooglePlus
intent.setPackage("com.google.android.apps.plus");
要向任何WhatsApp用户发送直接消息,请使用以下代码:
private void sendMessageToWhatsAppContact(String number) {
PackageManager packageManager = context.getPackageManager();
Intent i = new Intent(Intent.ACTION_VIEW);
try {
String url = "https://api.whatsapp.com/send?phone=" + number + "&text=" + URLEncoder.encode(CommonStrings.SHARING_APP_MSG, "UTF-8");
i.setPackage("com.whatsapp");
i.setData(Uri.parse(url));
if (i.resolveActivity(packageManager) != null) {
context.startActivity(i);
}
} catch (Exception e) {
e.printStackTrace();
}
}
这应该可以帮助你使用WhatsApp发送消息:
public void sendWhatsAppMsg() {
Intent waIntent = new Intent(Intent.ACTION_SEND);
waIntent.setType("text/plain");
String text = "testing message";
waIntent.setPackage("com.whatsapp");
if (waIntent != null) {
waIntent.putExtra(Intent.EXTRA_TEXT, text);//
startActivity(Intent.createChooser(waIntent, text));
} else {
Toast.makeText(this, "WhatsApp not found", Toast.LENGTH_SHORT)
.show();
}
}
private void openWhatsApp(String countryCode, String mobile) {
String customerPhoneNumber = String.format("%s%s", countryCode, mobile);
Uri uri = Uri.parse("smsto:" + customerPhoneNumber);
Intent sendIntent = new Intent(Intent.ACTION_SENDTO, uri);
sendIntent.setPackage("com.whatsapp");
if(sendIntent.resolveActivity(getPackageManager()) == null){
showDialogMessage("you should install whatsapp.");
return;
}
startActivity(sendIntent);
}
or
private void openWhatsApp(String countryCode, String mobile) {
String customerPhoneNumber = String.format("%s%s", countryCode, mobile);
Intent sendIntent = new Intent(Intent.ACTION_VIEW);
sendIntent.setPackage("com.whatsapp");
// you can remove this part ("&text=" + "your message")
String url = "https://api.whatsapp.com/send?phone=" + customerPhoneNumber + "&text=" + "your message";
sendIntent.setData(Uri.parse(url));
if(sendIntent.resolveActivity(getPackageManager()) == null){
showDialogMessage("you should install whatsapp.");
return;
}
startActivity(sendIntent);
}
这是我得到的最简单的例子
/**
* sends message to the specific application
* @param text The message that user wants to send
* @param view is the activity view of the application
*/
public void sendMessage(String text, View view){
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setType("text/plain");
intent.setPackage("com.example.appname");//For whatsapp you can use com.whatsapp
if (intent !=null){
intent.putExtra(Intent.EXTRA_TEXT, text);
startActivity(intent);
}else{
Snackbar.make(this, "Application not found.", Snackbar.LENGTH_SHORT).show();
}
}