1

I have following code which i am using for opening send SMS window:

Intent sendIntent = new Intent(Intent.ACTION_VIEW);
sendIntent.putExtra("sms_body", msgString);
sendIntent.setType("vnd.android-dir/mms-sms");
activity.startActivity( sendIntent );

Above code avoids "Complete action using" dialog that Android displays when there are two many apps are registered for sending SMS.

This code works on some device bug not on all, so is there any generic solution which will work on all the devices, to open a Send SMS screen without displaying "Complete action using" dialog.

User7723337
  • 11,857
  • 27
  • 101
  • 182
  • Do you know for sure which application you want to use for sending SMS, and are you certain it will be installed, and what would be your strategy if it were not? (please answer all 3 questions) – njzk2 Jan 16 '14 at 12:33
  • 1
    possible duplicate of [Custom filtering of intent chooser based on installed Android package name](http://stackoverflow.com/questions/5734678/custom-filtering-of-intent-chooser-based-on-installed-android-package-name) – Pankaj Kumar Jan 16 '14 at 12:36
  • 1
    And also read http://stackoverflow.com/questions/9730243/android-how-to-filter-specific-apps-for-action-send-intent – Pankaj Kumar Jan 16 '14 at 12:36
  • to answer @njzk2, 1. I want to use default SMS application whihc comes with the device not any third party app. 2. Default app we don't need to install it comes pr-installed on device. 3. So no question of app is not present. – User7723337 Jan 16 '14 at 12:50
  • @PankajKumar please let me know how can i put it to work for sending SMS using default APP. – User7723337 Jan 16 '14 at 12:52
  • the problem is that the preinstalled application is not priviledged in any way. If the user decided to install another application, and to not make any of both installed application the default one, it really is up to the user. You should not force them to use one or the other application. – njzk2 Jan 16 '14 at 12:52
  • I'll post an answer explaining a possible workaround, even though I would strongly advise against doing so – njzk2 Jan 16 '14 at 12:53
  • @njzk2 but using my app user will be allowed to send SMS using default app only that's the restriction that i am putting on my users. – User7723337 Jan 16 '14 at 12:56
  • @A_user See my added answer and let me know if confusion – Pankaj Kumar Jan 16 '14 at 13:06

3 Answers3

1

please try this code

public void sendSMS()
{
    SmsManager sm = SmsManager.getDefault();
    String number = "123456789";
    String msg = "Hello";
    sm.sendTextMessage(number, null, msg, null, null);
}
R9J
  • 6,605
  • 4
  • 19
  • 25
Srikanth Pai
  • 926
  • 3
  • 17
  • 30
1

Use below code

private void send() {
    Intent shareIntent = new Intent(Intent.ACTION_VIEW);
    shareIntent.setType("vnd.android-dir/mms-sms");
    shareIntent.putExtra("sms_body", "hello this is test");

    List<ResolveInfo> resInfo = getPackageManager().queryIntentActivities(shareIntent, 0);
    if (!resInfo.isEmpty()) {
        for (ResolveInfo resolveInfo : resInfo) {
            String packageName = resolveInfo.activityInfo.packageName;
            if (packageName.equals("com.android.mms")) {
                shareIntent.setPackage(packageName);
                            break;
            } else {
                // Else show chooser
            }
        }
        startActivity(shareIntent);
    }
}

Update for a perticular Activity.

There are two Activities of com.android.mms application, .ui.ConversationComposer and ui.ComposeMessageMms. You can use only two of them. As my above answer this was using .ui.ConversationComposer. So to use second activity use below code.

try {
    Intent shareIntent = new Intent(Intent.ACTION_VIEW);
    shareIntent.setType("vnd.android-dir/mms-sms");
    shareIntent.putExtra("sms_body", "hello this is test"); 
    shareIntent.setComponent(new ComponentName("com.android.mms","com.android.mms.ui.ComposeMessageMms"));
    startActivity(shareIntent);
} catch (ActivityNotFoundException exception) {
    // This will happen if Device have no MMS application. Handle this exception. 
}

This will show android default application to send MMS.

Pankaj Kumar
  • 81,967
  • 29
  • 167
  • 186
  • Thanks for the sample, will try this. But is that we need to put `break;` after line `shareIntent.setPackage`? – User7723337 Jan 16 '14 at 13:10
  • On My device it is creating draft of message to be sent, and displaying conversation list. Device: Sony xperia. But i want to display Message edit dialog. – User7723337 Jan 16 '14 at 13:16
  • Yes you need to add break. I added into answer, Thank you for indicating my mistake. – Pankaj Kumar Jan 16 '14 at 13:18
  • Actually this is default class which handles this Intent. If you see logs `Displayed com.android.mms/.ui.ConversationComposer` will be shown. So, you can not decide which Activity should be open. The main reason is that, activity can not handle extras given by you. – Pankaj Kumar Jan 16 '14 at 13:29
  • In my logs i see "packageName:com.sonyericsson.conversations", so we can not guaranty that this will open edit SMS dialog/Screen on all the devices. Humm but that's what i am looking for. – User7723337 Jan 16 '14 at 13:37
  • On my device Sony Xperia L i get Exception ActivityNotFoundException. – User7723337 Jan 17 '14 at 09:12
  • @A_user ok it seems that your device does not have `com.android.mms` package. I tested on Samsung s4 and it has this application so working. I think for generic solution you should use your code with chooser, like default behavior. – Pankaj Kumar Jan 17 '14 at 09:14
  • Humm i feel the same, as i listed all the system supported activities but i don't find any activity that does have sms or mms in it. – User7723337 Jan 17 '14 at 10:42
  • I did not work on Sony devices, so don't know which app is default for SMS/MMS. But as you mentioned an app, which is not generic (Must not present into all manufacturer) so, result it sad :( – Pankaj Kumar Jan 17 '14 at 11:13
0

You can resolve the intent before using it, to see what is matched :

List<ResolveInfo> infos = getPackageManager().queryIntentActivities(intent, PackageManager.MATCH_DEFAULT_ONLY);
for (ResolveInfo info : infos) {
    // Searching for a System installed package
    if (info.activityInfo.applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM > 0) {
        intent.setComponent(new ComponentName(
               info.activityInfo.applicationInfo.packageName,
               info.activityInfo.name));
    }
}

This has not been tested, but should work, as the flag 'SYSTEM' is apparently the flag for apps that are in the system image.

If this does not work, you can take the first item in the list, or search for a package name such as google or android, or see if preferredOrder can be of any help.

edit

queryIntentActivities is of course queryIntentActivities and ActivityInfo.FLAG_SYSTEM is ApplicationInfo.FLAG_SYSTEM. I must have beed tired when I wrote this...

njzk2
  • 38,969
  • 7
  • 69
  • 107
  • NO `ActivityInfo.FLAG_SYSTEM` and No `queryResolveActivities`. – User7723337 Jan 16 '14 at 13:24
  • Few more edits we need to do for to compile this code, change "getPackageManager" to "getPackageManager()" and change "setComponentName" to "setComponent", but still code displays setting screen not the SMS message edit screen. – User7723337 Jan 17 '14 at 10:39