2

I am building an android app. My app needs to send the customer's(who is giving purchase order) phone number to a specific mobile number via either whatsapp or sms. But I also want to restrict all other apps from my share via list other than these two.

I know how to use sharing Intent but don't know how to restrict all other apps from that drop down list.

Karup
  • 2,024
  • 3
  • 22
  • 48
Joy Jak
  • 21
  • 1

3 Answers3

0

To restric all the other app you need to provide the package name of the app you wanted to open e.g.

Intent i = new Intent(Intent.ACTION_SEND);
                 i.setPackage("com.whatsapp");

this is the package name for whats app.

you can easily get the packege name from setting->apps-> your App

For more information click here

Community
  • 1
  • 1
Baqir
  • 717
  • 1
  • 7
  • 20
0

Try this for whatsapp

public void shareViaWhatsapp() {
    Intent sendIntent = new Intent();
    sendIntent.setAction(Intent.ACTION_SEND);
    sendIntent.setPackage("com.whatsapp");
    sendIntent.putExtra(Intent.EXTRA_TEXT, "This is my text to send.");
    sendIntent.setType("text/plain");
    startActivity(sendIntent);
}

For SMS

public void sendSMS(){
     if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) // At least KitKat
      {
         String defaultSmsPackageName = Telephony.Sms.getDefaultSmsPackage(getActivity()); // Need to change the build to API 19
         Intent sendIntent = new Intent(Intent.ACTION_SEND);
         sendIntent.setType("text/plain");
         sendIntent.putExtra(Intent.EXTRA_TEXT, "This is my text to send.");

         if (defaultSmsPackageName != null)// Can be null in case that there is no default, then the user would be able to choose
         // any app that support this intent.
         {
            sendIntent.setPackage(defaultSmsPackageName);
         }
         startActivity(sendIntent);
      }
      else // For early versions, do what worked for you before.
      {
         Intent smsIntent = new Intent(android.content.Intent.ACTION_VIEW);
         smsIntent.setType("vnd.android-dir/mms-sms");

         smsIntent.putExtra("sms_body","This is my text to send.");
         startActivity(smsIntent);
      }
}
Narendra Motwani
  • 1,085
  • 10
  • 20
0

Above answer is sufficient for your question.But for other coders this may help :)

For whatsapp :

              try {
                    Intent waIntent = new Intent(Intent.ACTION_SEND);
                    waIntent.setType("text/plain");
                    waIntent.setPackage("com.whatsapp");
                    if (waIntent != null) {
                        waIntent.putExtra(Intent.EXTRA_TEXT, textToSend);//
                        startActivity(Intent.createChooser(waIntent, "Share with"));
                    } else {
                        Toast.makeText(this, "WhatsApp not Installed", Toast.LENGTH_SHORT)
                                .show();
                    }
                } catch (Exception e) {
                    Toast.makeText(ShareActivity.this,"Could not launch WhatsApp.",Toast.LENGTH_SHORT).show();
                }

For Message :

                try {
                    Intent sendIntentmsg = new Intent(Intent.ACTION_VIEW);
                    sendIntentmsg.setData(Uri.parse("sms:"));
                    sendIntentmsg.putExtra("sms_body", textToSend);
                    startActivity(sendIntentmsg);
                } catch (Exception e) {
                    Toast.makeText(ShareActivity.this,"Could not launch Messaging App now.",Toast.LENGTH_SHORT).show();
                }
Tushar Sheth
  • 391
  • 4
  • 18