javascript - 如何判断手机上是否装有什么app

查看:793
本文介绍了javascript - 如何判断手机上是否装有什么app的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问 题

我是一名前端,最近做了一个H5,需要用到地图导航,各大地图厂商给web端封装的SDK插件,只有路线规划的功能,没有像地图app里可以实时导航的方法!所以就得引导用户唤醒打开手机上有的地图APP;各大地图厂商都有URI-Schema的方法唤醒其APP,那么现在的问题就是,当你手机没有对应的地图APP时,唤醒会直接跳到App Store或是应用商店,让你下载APP;想让用户的体验更好一点,唤醒地图前首先判断一个手机上有什么地图APP然后遍历出来列表显示让用户选择!
所以所以所以如何判断手机上有什么app,感觉应该是ios 安卓开发者才能判断吧!貌似和我前端没啥个关系;

解决方案

android可按照包名来判断app是否存在:

方法:

    /*
     * check the app is installed
     */
    private boolean isAppInstalled(Context context, String packagename) {
        PackageInfo packageInfo;
        try {
            packageInfo = context.getPackageManager().getPackageInfo(packagename, 0);
        } catch (PackageManager.NameNotFoundException e) {
            packageInfo = null;
            e.printStackTrace();
        }
        if (packageInfo == null) {
            //System.out.println("没有安装");
            return false;
        } else {
            //System.out.println("已经安装");
            return true;
        }
    }

这是调用,我的是直接调用启动地图。你可以用原生实现一个操作表让用户选择后启动相应的APP。

   if (isAppInstalled(context, "com.autonavi.minimap")) {
        url = "amapuri://poi?sourceApplication=ewpower.com&keywords="+address;
        showToast("启动高德地图");
    }else if (isAppInstalled(context, "com.baidu.BaiduMap")) {
        url = "baidumap://map/geocoder?src=openApiDemo&address="+address;
        showToast("启动百度地图");
    } else {
        showToast("检测到您未安装地图APP,无法开始导航,建议您安装最新版的高德地图或百度地图");
        return;
    }

IOS可用canOpenURL来判断Schema是否存在判断,代码如下:
记得添加 lsapplicationqueriesschemes

if([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"iosamap://"]])
    {
        url = [[NSString stringWithFormat:@"iosamap://poi?sourceApplication=applicationName&name=%@",address]stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    }
    else if([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"baidumap://"]])
    {
        url = [[NSString stringWithFormat:@"baidumap://map/geocoder?address=%@&src=%@",address,@"ewpower.com"] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    }else
    {
        UIAlertView *alert = [[UIAlertView alloc]initWithTitle:nil message:@"检测到您未安装地图APP,无法开始导航,建议您安装最新版的高德地图或百度地图" delegate:self cancelButtonTitle:@"知道啦"otherButtonTitles:nil, nil];
        [alert show];
        return;
    }
    
    NSURL *schema = [NSURL URLWithString:url];
    if ([[UIDevice currentDevice].systemVersion integerValue] >= 10) {
        //iOS10以后,使用新API
        
        [[UIApplication sharedApplication] openURL:schema options:@{} completionHandler:^(BOOL success) { NSLog(@"scheme调用结束"); }];
    } else {
        //iOS10以前,使用旧API
        [[UIApplication sharedApplication] openURL:schema];
    }

这篇关于javascript - 如何判断手机上是否装有什么app的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆