2016-04-26 50 views
0

我已經嘗試以下共享網站上的whatsapp網址。我有錯誤請再試一次。我引用http://techzog.com/development/android-share-to-whatsapp-code-for-developers/的代碼劑量誰知道我該怎麼做?分享網站鏈接什麼應用程序使用android工作室

public class MainActivity extends Activity { 

    EditText message; 
    Button btn; 
    ImageView img; 
    Uri uri; 
    String imgurl="http://www.google.com"; 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     uri = Uri.parse(""+imgurl); 
     //Caption for the image! 
     message = (EditText) findViewById(R.id.caption); 
     btn=(Button)findViewById(R.id.button1); 
     img=(ImageView)findViewById(R.id.imageToBeShared); 
     btn.setOnClickListener(new View.OnClickListener() { 
            @Override 
            public void onClick(View v) { 
             PackageManager pm = getPackageManager(); 
             try { 

              Intent waIntent = new Intent(Intent.ACTION_SEND); 
              waIntent.setType("text/plain"); 
              String text = "Want to share this"; 

              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(waIntent.ACTION_VIEW, uri); 
              waIntent.putExtra(Intent.EXTRA_TEXT, text); 
              startActivity(Intent.createChooser(waIntent, "Share with")); 

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

            } 
           } 
     ); 
    } 
} 
+0

如果你的代碼,這是什麼問題? –

+0

問題是我無法分享在uri中傳遞的參數。鏈接沒有反映在whatsapp –

+0

'鏈接沒有反映whatsapp'你沒有任何'鏈接'在你的意圖 –

回答

3

試試這個在whatsapp中分享你的網址。

爲了更好的使用定義一個函數共享在下面的whatsapp像。

void shareinWhatsapp(String shareURL) { 
     Intent waIntent = new Intent(Intent.ACTION_SEND); 
     waIntent.setType("text/plain"); 
     waIntent.setPackage("com.whatsapp"); 
     if (waIntent != null) { 
      waIntent.putExtra(
        Intent.EXTRA_TEXT, 
        shareURL); 
      startActivity(Intent.createChooser(waIntent, "Share with")); 
     } else 
      Toast.makeText(this, "WhatsApp not Installed", Toast.LENGTH_SHORT) 
        .show(); 
    } 

然後,只要單擊whatsapp按鈕上的共享,就調用該函數。不要像下面..

whatsapp_share.setOnClickListener(new View.OnClickListener() { 

      @Override 
      public void onClick(View arg0) { 
       // TODO Auto-generated method stub 
       shareinWhatsapp(); 
      } 
     }); 

希望這會爲你工作..請試試這個..

相關問題