Reputation: 21
I have tried following for sharing website url on whatsapp. I got error of please try again. I refers the code from http://techzog.com/development/android-share-to-whatsapp-code-for-developers/ Dose anyone know how do i do that?
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();
}
}
}
);
}
}
Upvotes: 0
Views: 3053
Reputation: 4781
Try this to share your url in whatsapp.
For better usage define a function for sharing in whatsapp like below.
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();
}
Then call the function whenever the share on whatsapp button is clicked. Do as like below..
whatsapp_share.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
shareinWhatsapp();
}
});
Hope this will work for you.. Kindly try this..
Upvotes: 4