0

I am working with this code which Ragu Swaminathan helped me with on my original post found at: How to show both texting and dialer apps with a single Intent on Android?.

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);

I have this code that brings up snackup separating sms and dialer apps installed on users phone and allows them to pick one to either send a message / call a person.

What I want to know is, is it possbile to add another section on this snackbar that checks if the user has whatsapp installed or another specfic app installed.

Also, being new to android, I have tried playing around with the code but ended up messing it up. another thing that I would like to do is create sections like theses;

Sms apps:

.....

.....

.....

Dialer apps:

.....

.....

.....

Whatsapp:

.....

Any help is welcomed, please let me know if my question is not clear.

Edited;

enter image description here

Community
  • 1
  • 1
Henry
  • 1,042
  • 2
  • 17
  • 47

1 Answers1

0

You can use getPackageInfo method

PackageManager pm = getPackageManager();
PackageInfo pi = pm.getPackageInfo(certainAppPackageName, 0);

if (pi != null) {
    //app is installed, do smth
}

Google play links exist package names. For example:

 https://play.google.com/store/apps/details?id=org.telegram.messenger

where org.telegram.messenger is a package name

IEVGEN
  • 117
  • 8
  • how can I get the package name or is this the name of the app? – Henry Jan 25 '16 at 17:55
  • Google play links exist package names: `https://play.google.com/store/apps/details?id=org.telegram.messenger` – IEVGEN Jan 25 '16 at 17:56
  • For example, I want to check if the user has whatsapp, if so, I want to allow them to send a message through whatsapp. Do I can I achieve this? – Henry Jan 25 '16 at 17:58
  • As in their google play link: `https://play.google.com/store/apps/details?id=com.whatsapp`. Use `"com.whatsapp"` as a package name, – IEVGEN Jan 25 '16 at 18:01
  • Its not working, it gives me an error on getPackageManager(), it says cannot resolve method getPackageManager() and also it does not recognise the packageName. – Henry Jan 25 '16 at 18:01
  • `getPackageManager()` is a `Context` method, you need a context object to use it. – IEVGEN Jan 25 '16 at 18:06
  • Please could you give me an example, I have never came across context before – Henry Jan 25 '16 at 18:08
  • pass an `Activity` object to your function (as it inherits `Context`) or use [a static way to get 'Context'](http://stackoverflow.com/questions/2002288/static-way-to-get-context-on-android) – IEVGEN Jan 25 '16 at 18:12
  • I have done this; PackageManager pm = getActivity().getPackageManager(); PackageInfo pi = pm.getPackageInfo("com.whatsapp", 0); if (pi != null) { //do smth } but it still doesn't understand the package name – Henry Jan 25 '16 at 18:16
  • do you want to show only selected apps in the dialog. ? – Swaminathan V Jan 25 '16 at 18:17
  • Yes, so I want to get three sections in the dialog, one that says sms apps containing list of texting apps, another section called dialer apps, containing dialer apps and finally another section whatsapp, and if whatapp is found I want to show whats app inside it. – Henry Jan 25 '16 at 18:19
  • then you can go with custom view. You cannot do sectioning in the native intent. – Swaminathan V Jan 25 '16 at 18:20
  • oh ok, when I write the package name, it does not recognise it. – Henry Jan 25 '16 at 18:27
  • @henry, nothing happens in `if`-section? – IEVGEN Jan 25 '16 at 18:28
  • You need to handle this exception with `try-catch`. It is thrown when a given package cannot be found. – IEVGEN Jan 25 '16 at 18:35
  • Thanks, putting try-catch removed the error but when I run the app, whatsapp doesn't show up. – Henry Jan 25 '16 at 18:41
  • yep, I tried running the code in if-section it still doesn't show whatsapp – Henry Jan 25 '16 at 19:06
  • put there `Log.d("Tag","Whatsapp is installed");` and check your log records – IEVGEN Jan 25 '16 at 19:09
  • Whats app is installed because I am testing it on my device which has whatsapp – Henry Jan 25 '16 at 20:05
  • it says: 01-25 20:05:54.629 21982-21982/com.contact.others D/Tag: Whatsapp is installed – Henry Jan 25 '16 at 20:06