1

From my app, the user can text/call a person. I am starting an Activity through Intent in my app, currently 1 for texting and 1 for the dialer, is it possible to combine these, e.g. so when user clicks a button to call/text it brings up a list of texting/dialer apps in 1 window ?

Dialer:

Intent callIntent = new Intent(Intent.ACTION_CALL);
 callIntent.setData(Uri.parse("tel:" + somenumber));
 startActivity(callIntent);

Messaging app:

Intent callIntent = new Intent(Intent.ACTION_PROCESS_TEXT);
callIntent.setData(Uri.parse("tel:" + somenumber));
startActivity(callIntent);

What I want to do is, instead of having two separate buttons 1 for text and 1 for call, I want to have 1 and when clicked, it should allow users to either call or text.

Henry
  • 1,042
  • 2
  • 17
  • 47
  • I don't think it is possible. because when you call intent with Intent.ACTION_CALL or Intent.ACTION_PROCESS_TEXT, android opens the default app for call and text. so unless there is a app in your mobile supporting both calls and text, in the same app, this doesn't seem possible. – Ankit Aggarwal Jan 25 '16 at 11:12
  • I'll wait a bit more then probably give up but its a real shame if google hasn't made this possible because I don't see a point of repeating code to carry out two similar activties. – Henry Jan 25 '16 at 11:22
  • A possibility which might work may be instead of using Android's `startActivity` to either call or text, you could create your own dialog with a list of apps, and use `PackageManager` to find get all the apps which can call or text, add each one to the list in your dialog, and when the user chooses an app from that list, start that app directly. Would be more complex though, but you could have a single dialog with two lists, one for apps that can text and one for calls, or whatever else you want. Will try to find some details and write a real answer. It's a little more complex, but possible. – Jonas Czech Jan 25 '16 at 12:08
  • Custom dialog will still mean having two buttons 1 for call intent and 1 for text and will still require both sets of code to be written. – Henry Jan 25 '16 at 12:11
  • No, you would only need one button to show this dialog, since your dialog could contain both texting and dialler apps. I'll write an answer with some code to do this. – Jonas Czech Jan 25 '16 at 12:14
  • Sorry, I think I read your response wrongly, I thought you said create a custom dialog that has two options (1 button for dialer and another for texting). – Henry Jan 25 '16 at 13:28

1 Answers1

1

You can achieve this using Intent.EXTRA_INITIAL_INTENTS accompanied by queryIntentActivities.

Little complex but it will work try the below code..

final List<Intent> finalIntents = new ArrayList<Intent>();
final Intent textIntent = new Intent(Intent.ACTION_VIEW);
textIntent.setType("text/plain");
textIntent.setData(Uri.parse("sms:"));
final PackageManager packageManager = getActivity().getPackageManager();
final List<ResolveInfo> listCam = packageManager.queryIntentActivities(textIntent, 0);
for (ResolveInfo res : listCam) {
  final String packageName = res.activityInfo.packageName;
  final Intent intent = new Intent(textIntent);
  intent.setComponent(new ComponentName(res.activityInfo.packageName, res.activityInfo.name));
  intent.setPackage(packageName);
  finalIntents.add(intent);
}

Intent callIntent = new Intent(Intent.ACTION_DIAL);
callIntent.setData(Uri.parse("tel:121"));
Intent chooserIntent = Intent.createChooser(
        callIntent, "Select app to share");

chooserIntent.putExtra(
        Intent.EXTRA_INITIAL_INTENTS, finalIntents.toArray(new Parcelable[]{}));

startActivity(chooserIntent);

Best Luck..!

Swaminathan V
  • 4,663
  • 2
  • 22
  • 32
  • Cheers it works, is it possible to check if the user has whatsapp installed, if so, make a section on the snackbar to show whatsapp. – Henry Jan 25 '16 at 15:11
  • Also, is it possible to have title for each section. For example, instead of Select app to share, I would like to have Select a app to send message (list of sms apps) and another title for select a app to call (list of dialers) and then if possible, check if the user has whatsapp, if so, then make another section to show whats app on the snackbar. (Sorry if these are stupid questions, I don't have much knowledge of android and if i play around with the code, I will probably mess something up). – Henry Jan 25 '16 at 15:14
  • I'm not sure, let me check regarding this, if the answer is correct kindly accept it. – Swaminathan V Jan 25 '16 at 15:53
  • @Henry One question per post, please. If you have other questIons, please ask them separately. – Mike M. Jan 25 '16 at 17:22
  • @MikeM. I have created another post http://stackoverflow.com/questions/34998759/checking-through-intent-if-user-has-certain-messaging-app-installed-android, can you please have a look and see if you can help please. – Henry Jan 25 '16 at 17:40