Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Fetch installed and default browser in device

I am able to get all the applications which are in Launcher with the help of Intent.CATEGORY_LAUNCHER.

So for testing I created a test activity, this activity contains a single button, if I press the button, it should display the applications in the device

NOTE: `it should not display specific. for example i needed only Browser it should display all browser applications.`

I tried in this way a small CODE:

btn.setOnClickListener(new OnClickListener() {

        public void onClick(View v) {
            Intent intent = new Intent(Intent.ACTION_MAIN);
            intent.addCategory(Intent.CATEGORY_APP_BROWSER);

            List<ResolveInfo> mainLauncherList = getPackageManager().queryIntentActivities(intent, 0);
            System.out.println("the list iss = " +mainLauncherList);
        }
    });

The list is returning only one browser that is single Browser.

like image 741
Sukesh Avatar asked May 09 '12 08:05

Sukesh


3 Answers

Instead of a category, give the intent an action and query the ones that could ACTION_VIEW a url, like so:

Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setData(Uri.parse("http://www.google.com"));

List<ResolveInfo> mainLauncherList = getPackageManager().queryIntentActivities(intent, 0);
Log.e("Browsers","the list iss = " +mainLauncherList);

Which returns something like:

[ResolveInfo{44e9d350 com.android.browser.BrowserActivity p=0 o=0 m=0x208000}]

And assuming you have more than one browser installed, it will contain them all. Maybe I misunderstood your question but this technically returns the same applications you'd get if you tried launching the intent to open the url.

As to getting the launcher activity of those applications, since you already know how to get all the main applications and by the code I gave, the one you're after, you could match the package names (assumably) to find the launcher one (?)

UPDATE

ArrayList<String> allLaunchers = new ArrayList<String>();

Intent allApps = new Intent(Intent.ACTION_MAIN);
List<ResolveInfo> allAppList = getPackageManager().queryIntentActivities(allApps, 0);
for(int i =0;i<allAppList.size();i++) allLaunchers.add(allAppList.get(i).activityInfo.packageName);

Intent myApps = new Intent(Intent.ACTION_VIEW);
       myApps.setData(Uri.parse("http://www.google.es"));
List<ResolveInfo> myAppList = getPackageManager().queryIntentActivities(myApps, 0);
for(int i =0;i<myAppList.size();i++){
    if(allLaunchers.contains(myAppList.get(i).activityInfo.packageName)){
        Log.e("match",myAppList.get(i).activityInfo.packageName+"");
    }
}

As I was saying, you get all packages, from the launcher and match them against the packages from the ones that are able to perform the action, be it take a photo, or browse the web. You should be able to get that thing going with this.

like image 114
Juan Cortés Avatar answered Sep 23 '22 10:09

Juan Cortés


Above API version 30, I needed to add a queries section to AndroidManifest.xml, due to new package visibility rules (in addition to using PackageManager.MATCH_ALL as mentioned above).

<manifest ...>
    <application ... />
    <queries>
        <intent>
            <action android:name="android.intent.action.VIEW" />
            <data android:scheme="http" />
        </intent>
    </queries>
</manifest>
like image 21
neu242 Avatar answered Sep 23 '22 10:09

neu242


If anyone else needed the answer:

public static void getBrowserApps(Context mcontext) {
        PackageManager packageManager = mcontext.getPackageManager();
        Intent intent = new Intent(Intent.ACTION_VIEW);
        intent.setData(Uri.parse("http://www.google.com"));
        List<ResolveInfo> list = packageManager.queryIntentActivities(intent,
        PackageManager.MATCH_ALL);}

list will contain package information of all browser.

like image 24
Saad Mahmud Avatar answered Sep 21 '22 10:09

Saad Mahmud