2012-03-14 53 views
6

這是我特意打電話短信應用:如何從我的Android應用程序使用WhatsApp?

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?我沒有發現這樣的文檔。

+0

你接受分數實在是低。如果你想要人們回答你的問題,它需要更高。 – 2012-03-14 17:26:21

回答

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

+0

試過這個,它只顯示不顯示直接信息選項的電子郵件,對話框中的其他聊天應用程序,如何顯示它們。 – 2013-10-16 07:23:32

0
Intent i = new Intent(Intent.ACTION_SEND); 
i.setPackage("com.whatsapp"); 
i.putExtra("chat",true); 
i.setType("text/plain"); 
startActivity(i); 
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(); 
    } 

    } 
+0

完美答案.. – Noman 2014-07-10 06:05:13

+1

不檢查是否安裝了WhatsApp。請參閱http://stackoverflow.com/questions/15462874/sending-message-through-whatsapp – 0101100101 2014-07-25 14:29:56

+0

我編輯了我的答案。現在它檢查是否安裝了WhatsApp。 – krishan 2016-01-20 07:55:00

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 ;  
} 
相關問題