Find the default app of a particular type of intent

Inman

Well-Known Member
Licensed User
Longtime User
I am trying to find the default app that will handle a type of intent. For example find the web browser that will handle all the links when there are multiple browsers installed. I found some Java code to do that:

Get Preferred/Default app on Android - Stack Overflow

B4X:
Intent i = (new Intent(Intent.ACTION_VIEW, Uri.parse("https://www.google.com"));
PackageManager pm = context.getPackageManager();
final ResolveInfo mInfo = pm.resolveActivity(i, 0);
Toast.makeText(context, pm.getApplicationLabel(mInfo.activityInfo.applicationInfo), Toast.LENGTH_LONG).show();

I need to find the package name of the default app. Can someone please translate this code to B4A?
 

Inman

Well-Known Member
Licensed User
Longtime User
No I only want to get the package name. I want to see if my app is the default app for a particular intent.
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
Here (requires the reflection library):
B4X:
Sub Activity_Create(FirstTime As Boolean)
   Dim in As Intent
   in.Initialize(in.ACTION_VIEW, "http://www.google.com")
   Log(FindDefaultApp(in))
End Sub

Sub FindDefaultApp(In As Intent) As String
   Dim r As Reflector
   r.Target = r.GetContext
   Dim pm As Object
   pm = r.RunMethod("getPackageManager")
   r.Target = pm
   Dim mInfo As Object
   mInfo = r.RunMethod4("resolveActivity", Array As Object(In, 0), _
      Array As String("android.content.Intent", "java.lang.int"))
   If mInfo = Null Then Return "" 'no activity found
   r.Target = mInfo
   r.Target = r.GetField("activityInfo")
   r.Target = r.GetField("applicationInfo")
   Return r.GetField("packageName")
End Sub
 
Upvote 0

MrKim

Well-Known Member
Licensed User
Longtime User
I have a couple of related questions. I am using SetType but without really understanding it.
B4X:
i.SetType("application/pdf")
i.SetType("application/vnd.ms-excel")
i.SetType("application/vnd.ms-word")
On my device this opens .pdf/.xls/.doc files with QuickOffice.
1. Is SetType application specific? That is, are these particular commands forcing the use of Quickoffice or simply the default program for each?
2. How do you set the default program?
3. If 1 is NOT application specific how would I force the use of QuickOffice?

We are going to wind up with a lot of users on tablets, and I want to force the same experience for all of them. Experience has shown that you wind up with a mess if you don't standardize.
 
Upvote 0

NJDude

Expert
Licensed User
Longtime User
1. Is SetType application specific? That is, are these particular commands forcing the use of Quickoffice or simply the default program for each?
If there's a default app to open that particular type then it will open, otherwise, if there's more than one installed then a prompt to select one will show.
2. How do you set the default program?
That's up to the user, in some devices under the SETTINGS screen an option for APP ASSOCIATIONS will show up.
3. If 1 is NOT application specific how would I force the use of QuickOffice?
If you want to "force" a particular app, then you must know its package name and start it or, prompt the user to install it if not found.
 
Last edited:
Upvote 0

MrKim

Well-Known Member
Licensed User
Longtime User
Sigh and on and on it goes. Here is what I have found on new my Nexus7 With QuickOffice and Adobe reader both loaded. (Adobe was installed After QuickOffice)

This code will open the Chooser and allow you to SET THE DEFAULT.
B4X:
i.Initialize(i.ACTION_VIEW, "file://" & File.DirDefaultExternal & "/713-042124-001.pdf")
i.SetType("application/pdf")
StartActivity(I)


This code will ALWAYS open the Chooser IF THERE IS MORE THAN ONE PROGRAM but WITHOUT the Option of setting the default.
B4X:
    i.Initialize(i.ACTION_VIEW, "file://" &  File.DirDefaultExternal & "/713-042124-001.pdf")
    i.SetType("application/pdf")
    i.WrapAsIntentChooser("Choose PDF Viewer")
    StartActivity(i)


This code will open the FILE DIRECTLY WITH ADOBE READER - NO CHOOSER!
B4X:
    i.Initialize(i.ACTION_VIEW, "file://" &  File.DirDefaultExternal & "/713-042124-001.pdf")
    StartActivity(i)
 
Last edited:
Upvote 0
Top