1

i have read several already answered article on this site and used this Sending message through WhatsApp

i am able to share from chrome but not from app.

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.

my url is http://way2enjoy.com/app/jokes.php

if anyone can guide where the mistake is i will be thankful

Community
  • 1
  • 1
anamika
  • 75
  • 5

2 Answers2

0

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:)

John smith
  • 1,781
  • 17
  • 27
  • replaced my question codes with ur codes.but same error.page not found.its not working – anamika Aug 22 '15 at 08:06
  • @anamika please replace content type to */* insteed of text/plain in my last comment. – John smith Aug 22 '15 at 08:10
  • where is content type in above code 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, ""));} – anamika Aug 22 '15 at 08:17
  • dear last code code start from //method used to show IMs comment – John smith Aug 22 '15 at 08:25
  • plz help i m unable to share – anamika Aug 22 '15 at 13:17
0

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();

}
John smith
  • 1,781
  • 17
  • 27