简体   繁体   English

如何在自定义Android应用程序中实现“在whatsapp上共享”功能?

[英]How to implement a “share on whatsapp” feature in a custom Android app?

i have read several already answered article on this site and used this Sending message through WhatsApp 我已经在该网站上阅读了几篇已经回答的文章,并通过WhatsApp使用了此发送消息

i am able to share from chrome but not from app. 我可以从Chrome共享,但不能从应用程序共享。

my code is 我的代码是

public void onClickWhatsApp(View view) {

 PackageManager pm=getPackageManager();
 try {

    Intent waIntent = new Intent(Intent.ACTION_SEND);
    waIntent.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
    waIntent.setPackage("com.whatsapp");

    waIntent.putExtra(Intent.EXTRA_TEXT, text);
    startActivity(Intent.createChooser(waIntent, "Share with"));

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

    }  

 }

when i click on share button in my android app whatsapp icon it gives error page not found but when same thing is shared from chrome it just works fine. 当我在我的Android应用程序whatsapp图标中单击共享按钮时,它显示未找到错误页面,但是当从chrome共享相同内容时,它运行正常。

my url is http://way2enjoy.com/app/jokes.php 我的网址是http://way2enjoy.com/app/jokes.php

if anyone can guide where the mistake is i will be thankful 如果有人可以指导错误所在,我将非常感激

you can use my code which works for me 您可以使用适合我的代码

void openWhatsappContact(String number) {
Uri uri = Uri.parse("smsto:" + number);
Intent i = new Intent(Intent.ACTION_SENDTO, uri);
i.setPackage("com.whatsapp");  
startActivity(Intent.createChooser(i, ""));}

enjoy your code time:) 享受您的代码时间:)

you can use this code also 您也可以使用此代码

//method used to show IMs
private void show_custom_chooser(String value) {
    List<ResolveInfo> list = null;
    final Intent email = new Intent(Intent.ACTION_SEND);
    email.setData(Uri.parse("sms:"));
    email.putExtra(Intent.EXTRA_TEXT, "" + value);
    email.setType("text/plain"); // vnd.android-dir/mms-sms

    WindowManager.LayoutParams WMLP = dialogCustomChooser.getWindow()
            .getAttributes();
    WMLP.gravity = Gravity.CENTER;
    dialogCustomChooser.getWindow().setAttributes(WMLP);
    dialogCustomChooser.getWindow().setBackgroundDrawable(
            new ColorDrawable(android.graphics.Color.TRANSPARENT));
    dialogCustomChooser.setCanceledOnTouchOutside(true);
    dialogCustomChooser.setContentView(R.layout.about_dialog);
    dialogCustomChooser.setCancelable(true);
    ListView lvOfIms = (ListView) dialogCustomChooser
            .findViewById(R.id.listView1);
    PackageManager pm = getPackageManager();
    List<ResolveInfo> launchables = pm.queryIntentActivities(email, 0);
    // ////////////new
    list = new ArrayList<ResolveInfo>();
    for (int i = 0; i < launchables.size(); i++) {
        String string = launchables.get(i).toString();
        Log.d("heh", string);
//check only messangers
        if (string.contains("whatsapp")) {
            list.add(launchables.get(i));
        }
    }
    Collections.sort(list, new ResolveInfo.DisplayNameComparator(pm));
    int size = launchables.size();
    adapter = new AppAdapter(pm, list, MainActivity.this);
    lvOfIms.setAdapter(adapter);
    lvOfIms.setOnItemClickListener(new OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> arg0, View arg1,
                int position, long arg3) {
            ResolveInfo launchable = adapter.getItem(position);
            ActivityInfo activity = launchable.activityInfo;
            ComponentName name = new ComponentName(
                    activity.applicationInfo.packageName, activity.name);
            email.addCategory(Intent.CATEGORY_LAUNCHER);
            email.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
                    | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
            email.setComponent(name);
            startActivity(email);
            dialogCustomChooser.dismiss();
        }
    });
    dialogCustomChooser.show();

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM