Android内容通过Intent无法正常工作,

时间:2016-10-22 14:46:20

标签: java android android-studio

我需要能够将文本发送到whatsapp联系人,堆栈溢出似乎一致同意这是有效的:

path.join()

如果.setType(“text / plain”);被注释掉了,它只是打开了我给它的数字聊天的whatsapp,但如果我不评论它,没有任何事情发生,任何帮助表示赞赏。

2 个答案:

答案 0 :(得分:1)

更改并添加最后一行删除sendIntent.setType的评论

Uri uri = Uri.parse("smsto:" + "<number>"); 
Intent sendIntent = new Intent(Intent.ACTION_SENDTO, uri);
sendIntent.putExtra(Intent.EXTRA_TEXT, "YOOOH");
sendIntent.setType("text/plain");

// this line helps to open the chooser dialog
startActivity(Intent.createChooser(sendIntent, getResources().getString(R.string.share)));

答案 1 :(得分:0)

  

请查看此代码段,如果未安装用户,还会处理该案例   WHATSAPP。

PackageManager pm=getPackageManager();
                try {
                    Intent intent = new Intent(Intent.ACTION_SEND);
                    intent.setType("text/plain");
                    String text = "YOUR TEXT HERE";
                    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
                    intent.setPackage("com.whatsapp");
                    intent.putExtra(Intent.EXTRA_TEXT, text);
                    startActivity(Intent.createChooser(intent, "Share with"));

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