Android Fetch installed and default browser in dev

2020-03-24 06:13发布

问题:

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.

回答1:

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.



回答2:

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.