如何检查电话和照相机可用性SDK版本<五可用性、照相机、版本、电话

2023-09-05 02:26:43 作者:奇葩小超银

检查相机和电话硬件可用性的标准方法只适用,因为SDK> = 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);

我的问题,我需要在运行时SDK 3定义电话和相机的可用性(安卓1.5)

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

任何想法?

PS 我明白了Android 1.5是非常过时,但还是我有一大堆的客户运行这些设备的,所以我要与他们保持兼容。

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.

推荐答案

好了,我已经找到解决方案 - 很奇怪,但它的工作。

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

基本方法,试图让电话服务,如果它是空 - 返回false,如果它不为空(如HTC Flyer的 TelephonyManager 不为null)方法尝试运行 PackageManager.hasSystemFeature(PackageManager.FEATURE_TELEPHONY)使用反射,因为这种方法不适用于旧版本的SDK。

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.

下面是一个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