2016-07-17 7 views
0

Intentがあなたを直接WhatsAppにコントロールさせたいと思っています。ユーザーがボタンをクリックすると、インテントはあなたをWhatsAppに連れて行くことになっています。 これは私がいくつかのガイドラインに従った後に書いたコードですが、それは私がどのようにこれらを使用することができ、テキストを共有するためにここにテキストと画像の両方、 を共有するかを示すのですあなたのAndroid Appでインテントを使用してWhatsAppを開く方法

buttonWhatsapp.setOnClickListener(new View.OnClickListener() { 
     public void onClick(View v) { 
      // Performs action on click 
      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(Intent.createChooser(sendIntent, "")); 
      startActivity(sendIntent); 
      //opens the portfolio details class 
     } 
    }); 

答えて

0
PackageManager pm = getActivity().getPackageManager(); 

    try 
    { 
     // Raise exception if whatsapp doesn't exist 
     PackageInfo info = pm.getPackageInfo("com.whatsapp", PackageManager.GET_META_DATA); 

     Intent waIntent = new Intent(Intent.ACTION_SEND); 
     waIntent.setType("text/plain"); 
     waIntent.setPackage("com.whatsapp"); 
     waIntent.putExtra(Intent.EXTRA_TEXT, "YOUR TEXT"); 
     startActivity(waIntent); 
    } 
    catch (PackageManager.NameNotFoundException e) 
    { 
     Toast.makeText(MainActivity.activity, "Please install whatsapp app", Toast.LENGTH_SHORT) 
       .show(); 
    } 
+0

いや私は今それを試してみます –

+0

@PrateekMahesh私はコードを編集しました。 –

+0

@ AliGürelliそれはシンボル活動を解決することができないと言います –

2

を動作しません。コード、

private void shareTextUrl() { 
    Intent share = new Intent(android.content.Intent.ACTION_SEND); 
    share.setType("text/plain"); 
    share.addFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET); 

    // Add data to the intent, the receiving app will decide 
    // what to do with it. 
    share.putExtra(Intent.EXTRA_SUBJECT, "Title Of The Post"); 
    share.putExtra(Intent.EXTRA_TEXT, "http://www.codeofaninja.com"); 

    startActivity(Intent.createChooser(share, "Share link!")); 
} 

今、あなたがイメージを共有したいならば、あなたはこれらのコードを使用することができ、

private void shareImage() { 
    Intent share = new Intent(Intent.ACTION_SEND); 

    // If you want to share a png image only, you can do: 
    // setType("image/png"); OR for jpeg: setType("image/jpeg"); 
    share.setType("image/*"); 

    // Make sure you put example png image named myImage.png in your 
    // directory 
    String imagePath = Environment.getExternalStorageDirectory() 
      + "/myImage.png"; 

    File imageFileToShare = new File(imagePath); 

    Uri uri = Uri.fromFile(imageFileToShare); 
    share.putExtra(Intent.EXTRA_STREAM, uri); 

    startActivity(Intent.createChooser(share, "Share Image!")); 
} 
+0

Ok、それを試してみましょう –

0

ねえ、このスニペットは、私が知っている最も簡単な方法は、次のメソッドを呼び出すことである公式のWhatsAppサイト

https://www.whatsapp.com/faq/android/28000012

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); 
+0

私はそれを知っています。 –

0

からである(入力に送信したいテキストを文字列変数(メッセージ)を使用しますWhatAapp経由):

private void sendWhatsapp(String message){ 
    Intent sendIntent = new Intent(); 
    sendIntent.setAction(Intent.ACTION_SEND); 
    sendIntent.putExtra(Intent.EXTRA_TEXT, message); 
    sendIntent.setType("text/plain"); 
    sendIntent.setPackage("com.whatsapp"); 
    if (sendIntent.resolveActivity(getPackageManager()) != null) { 
     startActivity(sendIntent); 
    } 
} 

私はこれがあなたを助けてくれることを願っています。

関連する問題