How to show both texting and dialer apps with a si

2019-04-15 07:51发布

问题:

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.

回答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..!