Android — intent chooser for whatsapp and sms only

南笙酒味 提交于 2019-12-12 01:44:15

问题


I want to create an IntentChooser who offer to share text only via SMS or WhatsApp.

Here is my code to share via WhatsApp:

Intent localIntent = new Intent(Intent.ACTION_SEND);
localIntent.setType("text/plain");
localIntent.setPackage("com.whatsapp");
if (localIntent != null) {
    localIntent.putExtra(Intent.EXTRA_TEXT, "Hi there! I'm using this app");
    startActivity(Intent.createChooser(localIntent, "Hi there! I'm using this app"); 
}

I need to add to this also sharing with SMS. How can I do it?

Thank on advance.


回答1:


use this for Whatsapp.

            try {
                startActivity(new Intent(Intent.ACTION_SEND).setType("text/plain").setPackage("com.whatsapp").putExtra(Intent.EXTRA_TEXT, Message));
            } catch (android.content.ActivityNotFoundException ex) {
                Toast.makeText(this, "Whatsapp have not been installed.", Toast.LENGTH_SHORT).show();
            }

and this for SMS .

         String number = "12346556";  // The number on which you want to send SMS  
         startActivity(new Intent(Intent.ACTION_VIEW, Uri.fromParts("sms", number, null)));

OR

Possibly Duplicate of Multiple IntentChooser



来源:https://stackoverflow.com/questions/38967086/android-intent-chooser-for-whatsapp-and-sms-only

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!