如何从我的Android应用程序使用whatsapp?

时间:2012-03-14 17:08:45

标签: android

这就是我调用短信应用的方式:

Intent sendIntent = new Intent(Intent.ACTION_VIEW);
            sendIntent.putExtra("sms_body", "The SMS text"); 
            sendIntent.setType("vnd.android-dir/mms-sms");

            startActivity(sendIntent);   

如何通过twitter / Whatsapp / Facebook发送消息呢?我该怎么写代替mms-sms?我没有发现这方面的文件。

4 个答案:

答案 0 :(得分:8)

我也找不到直接调用Facebook / Twitter的方法,但你可以随时拨打android.content.Intent.ACTION_SEND并让用户选择应用程序。

Android ACTION_SEND intent

Intent i = new Intent(android.content.Intent.ACTION_SEND);

i.setType("text/plain");
i.putExtra(Intent.EXTRA_SUBJECT, "Subject");
i.putExtra(Intent.EXTRA_TEXT, "Message body");

startActivity(Intent.createChooser(i, "Share dialog title"));

但是,使用此功能通过Facebook分享时可能存在错误。有关详细信息,请参阅:Android Facebook Intent

答案 1 :(得分:2)

    public void onClickWhatsApp(View view) {
     try {
        Intent waIntent = new Intent(Intent.ACTION_SEND);
        waIntent.setType("text/plain");
                String text = "YOUR TEXT HERE";
        waIntent.setPackage("com.whatsapp");
        if (waIntent != null) {
            waIntent.putExtra(Intent.EXTRA_TEXT, text);//
            startActivity(Intent.createChooser(waIntent, "Share with"));
        } else {
            Toast.makeText(this, "WhatsApp not Installed", Toast.LENGTH_SHORT)
                    .show();
        }} catch (NameNotFoundException e) {
        Toast.makeText(this, "WhatsApp not Installed", Toast.LENGTH_SHORT)
                .show();
   }  

    }

答案 2 :(得分:0)

Intent i = new Intent(Intent.ACTION_SEND);
i.setPackage("com.whatsapp");
i.putExtra("chat",true);
i.setType("text/plain");
startActivity(i);

答案 3 :(得分:0)

您可以使用以下代码段: 对于WhatsApp:

public void onClickWhatsApp(View view) {

    PackageManager pm=getPackageManager();
    try {

        Intent waIntent = new Intent(Intent.ACTION_SEND);
        waIntent.setType("text/plain");
        String text = "YOUR TEXT HERE";

        PackageInfo info=pm.getPackageInfo("com.whatsapp", PackageManager.GET_META_DATA);
        //Check if package exists or not. If not then code 
        //in catch block will be called
        waIntent.setPackage("com.whatsapp");

        waIntent.putExtra(Intent.EXTRA_TEXT, text);
        startActivity(Intent.createChooser(waIntent, "Share with"));

   } catch (NameNotFoundException e) {
        Toast.makeText(this, "WhatsApp not Installed", Toast.LENGTH_SHORT)
                .show();
   }  
}

对于Twitter:

void shareOnTwitter()
{
    PackageManager pm=getPackageManager();
    try {
        Intent waIntent = new Intent(Intent.ACTION_SEND);
        waIntent.setType("text/plain");
        String text = "Insert Tweet Here";

        @SuppressWarnings("unused")
        PackageInfo info=pm.getPackageInfo("com.twitter.android", PackageManager.GET_META_DATA);
        //Check if package exists or not. If not then code 
        //in catch block will be called
        waIntent.setPackage("com.twitter.android");

        waIntent.putExtra(Intent.EXTRA_TEXT, text);
        startActivity(Intent.createChooser(waIntent, "Share with"));

    } catch (NameNotFoundException e) {
        Toast.makeText(this, "Twitter not Installed", Toast.LENGTH_SHORT)
        .show();
        return ;
    }  
    return ;    
}