首先、一个工具类 方法,检测该包名下的应用是否存在
public static boolean checkPackage(Context context ,String packageName) { if (packageName == null || "".equals(packageName)) return false; try{ context.getPackageManager().getApplicationInfo(packageName, PackageManager .GET_UNINSTALLED_PACKAGES); return true; }catch (PackageManager.NameNotFoundException e){ return false; } }
然后就是在需要的地方调用
以下是打开淘宝的方法
调用工具类,判断该包名下的应用是否存在,如果存在,则跳转,如果不存在,则打开网页版默认地址
private void openTaobaoShopping() { if (DeviceUtil.checkPackage(this, "com.taobao.taobao")) { Intent intent = new Intent(); intent.setAction("android.intent.action.VIEW"); // https://detail.tmall.com/item.htm?id=535823983028&spm=a21bo.7932663.item.2.0mvUrk&scm=1007.13596.65361.100200300000014 String url = "taobao://shop.m.taobao.com/shop/shop_index.htm?shop_id=131259851&spm=a230r.7195193.1997079397.8.Pp3ZMM&point"; Uri uri = Uri.parse(url); intent.setData(uri); startActivity(intent); } else { WebViewActivity.open(MainActivity.this, "https://shop131259851.taobao.com/?spm=a230r.7195193.1997079397.8.Pp3ZMM"); } }
接下来 是WebViewActivity中的open方法 它是打开网页版路径:
public static void open(Context context,String url){ Intent intent = new Intent(); intent.setClass(context,WebViewActivity.class); intent.putExtra("webUrl",url); context.startActivity(intent); }
其中 webviewactivity中 布局只是一个webview
<WebView android:id="@+id/webview" android:layout_width="match_parent" android:layout_height="match_parent" android:fadingEdge="none" android:visibility="invisible" android:fadingEdgeLength="0dp" android:scrollbars="none" />