How to verify existance of a component before launching it...

alexb

Member
Licensed User
Longtime User
I would like to launch some external programs by creating an intent.

Example: opening the Android Music Album manager "Music"

Dim Intent1 As Intent
Intent1.Initialize(Intent1.ACTION_MAIN, "")
Intent1.SetComponent("com.android.music/.MusicBrowserActivity")
StartActivity(Intent1)

Now this works fine as long as this application package is really installed on the phone/tablet. But on some devices it's not, replaced by something else or possibly removed by the end user => nasty error message for the end user:sign0085:

1. Does anybody have a code sample how to verify the existence of the component (here: com.android.music) and of the activity to launch (here: MusicBrowserActivity) BEFORE actually starting the activity.

2. It seems you can (sometimes?) simply use a generalized descriptor for a certain package/component rather than the exact package name. Example for opening 'GoggleMaps':

SIMPLY
Intent1.SetComponent("googlemaps")

INSTEAD OF LENGTHY (AND POSSIBLY WRONG)
Intent1.SetComponent("com.google.android.apps.m4ps/com.google.android.maps.MapsActivity")

=> where can I find a list of this simplified descriptors such as "googlemaps"
=> is it possible to verify the existence of targeted component ?

3. Similar to (2): is there a list of all the MIME types?

Thanks for your help!
 

alexb

Member
Licensed User
Longtime User
Cor, Erel - thanks for your replies!

I have looked up the package manager documentation and modified some presented source code, so I get now a complete list of installed apps with this:
Dim pm As PackageManager
Dim packages As List
packages = pm.GetInstalledPackages

For i = 0 To packages.Size - 1
Log("-------------------------------------------------")
Log(packages.Get(i))
Log(pm.GetApplicationIntent(packages.Get(i)))
Log(pm.GetApplicationLabel(packages.Get(i)))
Next

As a result I see somehting like this in the LOG output:
com.google.android.apps.m4ps
Intent { act=android.intent.action.MAIN flg=0x10000000 cmp=com.google.android.apps.m4ps/com.google.android.maps.MapsActivity }
Maps

Unfortunately the type of information I was looking for (ex. "googlemaps") doesn't seem to be provided by package manager either. I was hoping to get a list of these simplified labels/descriptors, so that I can simply launch an app like GoogleMaps hoping that this type of information doesn't change across the different Android releases or languages. I understand I could search for package recursively according to the 'Label' provided by PackageManager but then the label (= name displayed on desktop - right?) would need to be the same across all versions and languages. I think this is often not the case.

Having read 200+ blogs on search terms 'intent', 'startactivity' and now 'packagemanager' I still don't see the exact solution. Thanks to your hints I can verify the existence of a certain package or just ignore the error (catch exception) but I try to identify securely the corresponding target app for a certain file type (therefore my question for MIME).

I have also tried to use Intent1.SetType(MIME) but it seems it doesn't work with ACTION_MAIN but only with ACTION_VIEW. ACTION_VIEW will launch the fitting program for a certain MIME but then also play/show the stated file. I do not need to play/show a certain file (don't have one), I just want to open program corresponding to the file type. User will then decide what to play/show etc.:sign0137:

Any thoughts how to find the program?
 
Upvote 0
Top