Android - 检查是否存在Facebook应用程序返回错误的结果 [英] Android - check if Facebook app exists returns wrong result

查看:361
本文介绍了Android - 检查是否存在Facebook应用程序返回错误的结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 

我在FB本机应用程序的通知中收到通知,如果它已安装在应用程序和浏览器中,请使用以下代码: code> PackageManager packageManager = context.getPackageManager();
尝试{
packageManager.getPackageInfo(com.facebook.katana,PackageManager.GET_ACTIVITIES);
return context.getString(R.string.fb_app_prefix)+ fb_url;
} catch(PackageManager.NameNotFoundException e){
return context.getString(R.string.fb_site_prefix)+ fb_url; //普通网络mUrl
}

它适用于大多数设备(包括模拟器)但是在其中一些中,尽管未安装该应用程序,但它并没有发生错误。



我的代码有什么问题?



我可以为每个链接添加以下代码,但不能确定它是健康:

 意图testIntent = new Intent(Intent.ACTION_VIEW,Uri.parse(fb_app_url)); 
if(testIntent.resolveActivity(packageManager)!= null){
return fb_app_url;
}


解决方案

  public static boolean isPackageExisted(Context c,String targetPackage){
PackageManager pm = c.getPackageManager();
try {
PackageInfo info = pm.getPackageInfo(targetPackage,
PackageManager.GET_META_DATA);
} catch(NameNotFoundException e){
return false;
}
返回true;
}


I'm opening links received in notifications in FB native app if it's installed on the app and in the browser otherwise, using this code:

PackageManager packageManager = context.getPackageManager();
try {
    packageManager.getPackageInfo("com.facebook.katana", PackageManager.GET_ACTIVITIES);
    return context.getString(R.string.fb_app_prefix) + fb_url;
} catch (PackageManager.NameNotFoundException e) {
    return context.getString(R.string.fb_site_prefix) + fb_url; //normal web mUrl
}

It works on most devices (including the emulator), but in some of them it doesn't throw an error although the app isn't installed.

What's wrong with my code?

I can add the following code for every link I have but not sure it's "healthy":

Intent testIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(fb_app_url));
if (testIntent.resolveActivity(packageManager) != null) {
    return fb_app_url;
}

解决方案

public static boolean isPackageExisted(Context c, String targetPackage) {
 PackageManager pm = c.getPackageManager();
    try {
        PackageInfo info = pm.getPackageInfo(targetPackage,
                PackageManager.GET_META_DATA);
    } catch (NameNotFoundException e) {
        return false;
    }
    return true;
}

这篇关于Android - 检查是否存在Facebook应用程序返回错误的结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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