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. Building TensorFlow on Android
  2. Android高版本P/Q/R源码编译指南
  3. NestedScrollView嵌套RecyclerView 能默
  4. Android(Kotlin版本)MVC框架的实例以及代码
  5. 【Android】debug 状态下其签名文件 debu
  6. Context的子类实现:ContextImpl的初始化
  7. 解决Android Studio XML布局格式化无法换
  8. Android 修改系统屏幕亮度及监听
  9. android: what is the difference betwee
  10. Android中对图像进行Base64编码