1. 获取到 url

2. 根据 url 类型做处理

schemes

在 Android ,我们可以定义特定的 schemes url. 然后通过代码:

Intent intent = Intent.parseUri(url, Intent.URI_INTENT_SCHEME);

只要有 app 在 AndroidManifest 中定义了相同的 schemes url ,就能直接跳转。

url

如果是普通的以 http,https,ftp 为开头的 url ,我们则可以直接

Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));

这个时候,系统会弹出多个选项让用户选择用哪个应用启动。但是如果我们想要直接启动对应的应用呢?

private static Intent handleHttpIntent(String url, String tag) {    Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));    ResolveInfo resolveInfo = getResolveInfo(intent, tag);    if (null != resolveInfo) {      try {        intent.setClassName(resolveInfo.activityInfo.packageName, resolveInfo.activityInfo.name);        return intent;      } catch (Exception e) {        e.printStackTrace();      }    }    return null;  }  private static ResolveInfo getResolveInfo(Intent intent, String tag) {    List resolveInfos = AppUtil.getApp()                                            .getPackageManager()                                            .queryIntentActivities(intent,                                                                   PackageManager.MATCH_DEFAULT_ONLY);    for (ResolveInfo resolveInfo : resolveInfos) {      //use the title or Remotely configured package name to find the target app      if (resolveInfo.activityInfo.packageName.contains(tag)) {        return resolveInfo;      }    }    return null;  }

这种情况,我们就只能获取所有能跳转的应用列表,通过我们远程配置的 tag 来获取指定跳转的应用了。

完整代码

  /**   * return the intent of start other apps by url intent   *   * @param url   *     url   * @param tag   *     Used to pick the specified application among the candidates,   *     it should be the package name or the key word of the package name   *   * @return the intent of the specified app   */  public static Intent getCallOtherAppsByUrlIntent(String url, String tag) {    if (url.startsWith("https") || url.startsWith("http") || url.startsWith("ftp")) {      return handleHttpIntent(url, tag);    } else {      try {        return Intent.parseUri(url, Intent.URI_INTENT_SCHEME);      } catch (URISyntaxException e) {        e.printStackTrace();      }    }    return null;  }  private static Intent handleHttpIntent(String url, String tag) {    Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));    ResolveInfo resolveInfo = getResolveInfo(intent, tag);    if (null != resolveInfo) {      try {        intent.setClassName(resolveInfo.activityInfo.packageName, resolveInfo.activityInfo.name);        return intent;      } catch (Exception e) {        e.printStackTrace();      }    }    return null;  }  private static ResolveInfo getResolveInfo(Intent intent, String tag) {    List resolveInfos = AppUtil.getApp()                                            .getPackageManager()                                            .queryIntentActivities(intent,                                                                   PackageManager.MATCH_DEFAULT_ONLY);    for (ResolveInfo resolveInfo : resolveInfos) {      //use the title or Remotely configured package name to find the target app      if (resolveInfo.activityInfo.packageName.contains(tag)) {        return resolveInfo;      }    }    return null;  }

更多相关文章

  1. Android 在代码中同时给控件设置圆角和背景色
  2. Android webview调用js代码无效 webView.loadUrl("javascript:al
  3. Android 如何在IDEA Eclipse 的UI Editor 中显示自定义的字体 Pr
  4. 自定义ViewPager实现图片自动轮播无限循环
  5. Android属性动画之XML定义方式
  6. android之xml定义数组
  7. android 代码混淆遭遇conversion to Dalvik format failed with
  8. 自定义ProgressBar样式

随机推荐

  1. Android(安卓)之Activity , Window和View
  2. Android(安卓)Studio上传SVN
  3. android FragmentActivity+FragmentTabHo
  4. 详解Android(安卓)视频播放时停止后台运
  5. Android(安卓)WiFi ADB
  6. react-native调用Android原生控件
  7. Android(安卓)4.4 Kitkat Phone工作流程
  8. Android支持Java8新特性
  9. android 报Unable to resolve target 'an
  10. Android(安卓)获取手机联系人列表