Search code examples
javaandroidkotlinandroid-intentandroid-implicit-intent

How to open exact app with Intent.ACTION_VIEW and Uri


I am trying to open zoom app by passing uri to the intent with below and it works fine.

 val intent = Intent(Intent.ACTION_VIEW, Uri.parse("<zoom url>"))
 val packageManager = packageManager
 if (intent.resolveActivity(packageManager) != null) {
     startActivity(launchApp)
 }

But this shows my browser also as user can select it and open the uri I pass. what I want to do is only open zoom app with the uri. By using val activities = packageManager.queryIntentActivities(intent, PackageManager.MATCH_DEFAULT_ONLY) I can filter the apps that can open my intent but how can I select the exact app(zoom) and pass uri to it and open zoom app only with my meeting url?


Solution

  • May be need a packageName?

    I referenced some code from Google in this

    public static void openCustomTab(Activity activity,
                                         CustomTabsIntent customTabsIntent,
                                         Uri uri,
                                         CustomTabFallback fallback) {
            String packageName = CustomTabsHelper.getPackageNameToUse(activity);
    
            //If we cant find a package name, it means theres no browser that supports
            //Chrome Custom Tabs installed. So, we fallback to the webview
            if (packageName == null) {
                if (fallback != null) {
                    fallback.openUri(activity, uri);
                }
            } else {
                customTabsIntent.intent.setPackage(packageName);
                customTabsIntent.launchUrl(activity, uri);
            }
        }
    

    it use setPackage(),then the app will open the chrome app without chooser.

    This is the method of getPackageNameToUse

    
    static final String STABLE_PACKAGE = "com.android.chrome";
    static final String BETA_PACKAGE = "com.chrome.beta";
    static final String DEV_PACKAGE = "com.chrome.dev";
    static final String LOCAL_PACKAGE = "com.google.android.apps.chrome";
    
    ...
    ...
    
    public static String getPackageNameToUse(Context context) {
            if (sPackageNameToUse != null) return sPackageNameToUse;
    
            PackageManager pm = context.getPackageManager();
            // Get default VIEW intent handler.
            Intent activityIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.example.com"));
            ResolveInfo defaultViewHandlerInfo = pm.resolveActivity(activityIntent, 0);
            String defaultViewHandlerPackageName = null;
            if (defaultViewHandlerInfo != null) {
                defaultViewHandlerPackageName = defaultViewHandlerInfo.activityInfo.packageName;
            }
    
            // Get all apps that can handle VIEW intents.
            List<ResolveInfo> resolvedActivityList = pm.queryIntentActivities(activityIntent, 0);
            List<String> packagesSupportingCustomTabs = new ArrayList<>();
            for (ResolveInfo info : resolvedActivityList) {
                Intent serviceIntent = new Intent();
                serviceIntent.setAction(ACTION_CUSTOM_TABS_CONNECTION);
                serviceIntent.setPackage(info.activityInfo.packageName);
                if (pm.resolveService(serviceIntent, 0) != null) {
                    packagesSupportingCustomTabs.add(info.activityInfo.packageName);
                }
            }
    
            // Now packagesSupportingCustomTabs contains all apps that can handle both VIEW intents
            // and service calls.
            if (packagesSupportingCustomTabs.isEmpty()) {
                sPackageNameToUse = null;
            } else if (packagesSupportingCustomTabs.size() == 1) {
                sPackageNameToUse = packagesSupportingCustomTabs.get(0);
            } else if (!TextUtils.isEmpty(defaultViewHandlerPackageName)
                    && !hasSpecializedHandlerIntents(context, activityIntent)
                    && packagesSupportingCustomTabs.contains(defaultViewHandlerPackageName)) {
                sPackageNameToUse = defaultViewHandlerPackageName;
            } else if (packagesSupportingCustomTabs.contains(STABLE_PACKAGE)) {
                sPackageNameToUse = STABLE_PACKAGE;
            } else if (packagesSupportingCustomTabs.contains(BETA_PACKAGE)) {
                sPackageNameToUse = BETA_PACKAGE;
            } else if (packagesSupportingCustomTabs.contains(DEV_PACKAGE)) {
                sPackageNameToUse = DEV_PACKAGE;
            } else if (packagesSupportingCustomTabs.contains(LOCAL_PACKAGE)) {
                sPackageNameToUse = LOCAL_PACKAGE;
            }
            return sPackageNameToUse;
        }
    

    Maybe it will help you.