How to find out whether android device has cellular radio module?

后端 未结 2 1646
-上瘾入骨i
-上瘾入骨i 2021-01-14 08:59

How can I find out for sure that device really has gsm, cdma or other cellular network equipment (not just WiFi)? I don\'t want to check current connected network state, bec

相关标签:
2条回答
  • 2021-01-14 09:39

    If you're publishing in the store, and you want to limit your application only being visible to actual phones, you could add a <uses-feature> into your manifest that asks for android.hardware.telephony. Check out if that works for you from the documentation.

    0 讨论(0)
  • 2021-01-14 09:53

    Just in case somebody needs complete solution for this: Reflection is used because some things may not exist on some firmware versions. MainContext - main activity context.

        static public int getSDKVersion()
    {
        Class<?> build_versionClass = null;
    
        try
        {
            build_versionClass = android.os.Build.VERSION.class;
        }
        catch (Exception e)
        {
        }
    
        int retval = -1;
        try
        {
            retval = (Integer) build_versionClass.getField("SDK_INT").get(build_versionClass);
        }
        catch (Exception e)
        {
        }
    
        if (retval == -1)
            retval = 3; //default 1.5
    
        return retval;
    }
    
    static public boolean hasTelephony()
    {
        TelephonyManager tm = (TelephonyManager) Hub.MainContext.getSystemService(Context.TELEPHONY_SERVICE);
        if (tm == null)
            return false;
    
        //devices below are phones only
        if (Utils.getSDKVersion() < 5)
            return true;
    
        PackageManager pm = MainContext.getPackageManager();
    
        if (pm == null)
            return false;
    
        boolean retval = false;
        try
        {
            Class<?> [] parameters = new Class[1];
            parameters[0] = String.class;
            Method method = pm.getClass().getMethod("hasSystemFeature", parameters);
            Object [] parm = new Object[1];
            parm[0] = "android.hardware.telephony";
            Object retValue = method.invoke(pm, parm);
            if (retValue instanceof Boolean)
                retval = ((Boolean) retValue).booleanValue();
            else
                retval = false;
        }
        catch (Exception e)
        {
            retval = false;
        }
    
        return retval;
    }
    
    0 讨论(0)
提交回复
热议问题