2016-06-22 178 views
1

我發現了一些通過whatsapp發送消息的代碼。事情是這樣的:
通過whatsapp使用Intent發送消息

Button send=(Button)findViewById(R.id.sendbtn); 
    EditText input=(EditText)findViewById(R.id.inputtxt); 
    final String txt=input.getText().toString(); 

    send.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      Intent sendIntent=new Intent(); 
      sendIntent.setAction(Intent.ACTION_SEND); 
      sendIntent.putExtra(Intent.EXTRA_TEXT,txt); 
      sendIntent.setType("text/plain"); 
      sendIntent.setPackage("com.whatsapp"); 
      startActivity(sendIntent); 



     } 
    });` 

的代碼工作,但在WhatsApp的屏幕顯示了,我選擇一個聯繫人,我WhatsApp的關閉和我的應用程序顯示出來。還有什麼要添加發送消息?

回答

1

你的所作所爲是正確的,根據https://www.whatsapp.com/faq/android/28000012

它打開了WhatsApp,讓你選擇聯繫人,將消息發送給

Intent sendIntent = new Intent(); 
sendIntent.setAction(Intent.ACTION_SEND); 
sendIntent.putExtra(Intent.EXTRA_TEXT, "This is my text to send."); 
sendIntent.setType("text/plain");sendIntent.setPackage("com.whatsapp"); 
startActivity(sendIntent); 

或者,您也可以選擇一個聯繫人,然後whatapps將與選定的聯繫人打開。然後您可以從該窗口中添加自己的文字。

一看便知這個問題:How do I open conversation of a particular contact in Whatsapp

你不能做的是使用的意圖,直接從您的應用程序將消息發送給特定的聯繫人。 (我認爲這是一個關於whatsapp的反垃圾郵件決定)。

1

嘗試更改如下代碼:

Button send=(Button)findViewById(R.id.sendbtn); 
EditText input=(EditText)findViewById(R.id.inputtxt); 


send.setOnClickListener(new View.OnClickListener() { 
    @Override 
    public void onClick(View v) { 

     final String txt = input.getText().toString(); 

     Intent sendIntent=new Intent(); 
     sendIntent.setAction(Intent.ACTION_SEND); 
     sendIntent.putExtra(Intent.EXTRA_TEXT,txt); 
     sendIntent.setType("text/plain"); 
     sendIntent.setPackage("com.whatsapp"); 
     startActivity(sendIntent); 



    } 
});` 
相關問題