如何检查 SDK 版本的电话和摄像头可用性 < 5

发布于 2024-11-16 21:02:38 字数 419 浏览 0 评论 0 原文

检查摄像头和电话硬件可用性的标准方法仅在 SDK >= 5 后才有效:

PackageManager pm = this.getPackageManager();
boolean hasTelephony=pm.hasSystemFeature(PackageManager.FEATURE_TELEPHONY);
boolean hasCamera=pm.hasSystemFeature(PackageManager.FEATURE_CAMERA);

我的问题是我需要在 SDK 3 (Android 1.5) 中运行时定义电话和摄像头的可用性

有什么想法吗?

PS 我知道 Android 1.5 已经非常过时了,但我仍然有很多客户运行这些设备,所以我必须保持与它们的兼容性。

Standard way of checking camera and telephony hardware availability works only since SDK >= 5:

PackageManager pm = this.getPackageManager();
boolean hasTelephony=pm.hasSystemFeature(PackageManager.FEATURE_TELEPHONY);
boolean hasCamera=pm.hasSystemFeature(PackageManager.FEATURE_CAMERA);

My problem that I need to runtime define availability of telephony and camera in SDK 3 (Android 1.5)

Any ideas?

P.S. I understand that Android 1.5 is very outdated, but still I do have bunch of customers running these devices, so I have to keep compatibility with them.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

丑丑阿 2024-11-23 21:02:38

好吧,我找到了解决方案 - 很奇怪,但它有效。

基本上,方法尝试获取电话服务,如果为空,则返回 false,如果不为空(例如,对于 HTC Flyer TelephonyManager 不为空),方法会尝试运行 PackageManager.hasSystemFeature(PackageManager.FEATURE_TELEPHONY ) 使用反射,因为此方法不适用于旧版本的 SDK。

这是一个代码:

private Boolean hasTelephony;

public boolean hasTelephony()
{
    if(hasTelephony==null)
    {
        TelephonyManager tm=(TelephonyManager )this.getSystemService(Context.TELEPHONY_SERVICE);
        if(tm==null)
        {
            hasTelephony=new Boolean(false);
            return hasTelephony.booleanValue();
        }
        if(this.getSDKVersion() < 5)
        {
            hasTelephony=new Boolean(true);
            return hasTelephony;
        }
        PackageManager pm = this.getPackageManager();
        Method method=null;
        if(pm==null)
            return hasCamera=new Boolean(false);
        else
        {
            try
            {
                Class[] parameters=new Class[1];
                parameters[0]=String.class;
                method=pm.getClass().getMethod("hasSystemFeature", parameters);
                Object[] parm=new Object[1];
                parm[0]=new String(PackageManager.FEATURE_TELEPHONY);
                Object retValue=method.invoke(pm, parm);
                if(retValue instanceof Boolean)
                    hasTelephony=new Boolean(((Boolean )retValue).booleanValue());
                else
                    hasTelephony=new Boolean(false);
            }
            catch(Exception e)
            {
                hasTelephony=new Boolean(false);
            }
        }
    }
    return hasTelephony;
}

或多或少相同的方法可用于检查相机可用性

Well, I have found solution - very odd but it's working.

Basically method tries to get telephony service if it's null - it returns false, if it's not null (e.g. for HTC Flyer TelephonyManager is not null) method tries to run PackageManager.hasSystemFeature(PackageManager.FEATURE_TELEPHONY) using reflection, since this method is not available for old versions of SDK.

Here is a code:

private Boolean hasTelephony;

public boolean hasTelephony()
{
    if(hasTelephony==null)
    {
        TelephonyManager tm=(TelephonyManager )this.getSystemService(Context.TELEPHONY_SERVICE);
        if(tm==null)
        {
            hasTelephony=new Boolean(false);
            return hasTelephony.booleanValue();
        }
        if(this.getSDKVersion() < 5)
        {
            hasTelephony=new Boolean(true);
            return hasTelephony;
        }
        PackageManager pm = this.getPackageManager();
        Method method=null;
        if(pm==null)
            return hasCamera=new Boolean(false);
        else
        {
            try
            {
                Class[] parameters=new Class[1];
                parameters[0]=String.class;
                method=pm.getClass().getMethod("hasSystemFeature", parameters);
                Object[] parm=new Object[1];
                parm[0]=new String(PackageManager.FEATURE_TELEPHONY);
                Object retValue=method.invoke(pm, parm);
                if(retValue instanceof Boolean)
                    hasTelephony=new Boolean(((Boolean )retValue).booleanValue());
                else
                    hasTelephony=new Boolean(false);
            }
            catch(Exception e)
            {
                hasTelephony=new Boolean(false);
            }
        }
    }
    return hasTelephony;
}

More or less the same approach is workable for checking of camera availability

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文