我试过跟随whatsapp分享网站网址。我有错误请再试一次。我引用了来自http://techzog.com/development/android-share-to-whatsapp-code-for-developers/的代码。任何人都知道我该怎么做?
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 :(得分: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();
}
});
希望这对你有用..请试试这个..