Xperia Arc S 光传感器

发布于 2025-01-03 00:40:18 字数 561 浏览 5 评论 0原文

我正在尝试使用光传感器开发简单的 Android 应用程序。不幸的是,虽然我的 SE Xperia Arc S 确实有光传感器,但我无法让它工作。下面给出的简单代码返回 null。我正在使用 *#*#7378423#*#* 和服务测试 -> 检查服务测试中的光传感器环境光传感器,它在那里工作。

返回 null:

sensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
return mySensorManager.getDefaultSensor(Sensor.TYPE_LIGHT);

此代码返回传感器列表,缺少光传感器:

mySensorManager.getSensorList(Sensor.TYPE_ALL);

我检查了市场上的光传感器应用程序,也不起作用。

手机信息:

LT18i,Android 版本:2.3.4,编译:4.0.2.A.0.42

有什么想法吗?

感谢您的帮助。

I'm trying to develop simple Android app using light sensor. Unfortunately although my SE Xperia Arc S does have light sensor I can't get it working. Simple code presented below returns null. I was checking light sensor in Service Test using *#*#7378423#*#* and Service Test -> Ambient Light Sensor and it is working there.

Returning null:

sensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
return mySensorManager.getDefaultSensor(Sensor.TYPE_LIGHT);

This code returns list of sensors, missing light sensor:

mySensorManager.getSensorList(Sensor.TYPE_ALL);

I have checked light sensor apps in the market, doesn't work either.

Phone info:

LT18i, Android version: 2.3.4, Compilation: 4.0.2.A.0.42

Any ideas?

Thanks for help.

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

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

发布评论

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

评论(1

雨落□心尘 2025-01-10 00:40:18

Android 2.1 的 Camera.Parameters.FLASH_MODE_TORCH 替换

检查上面的链接。我用它在 Arc S 上进行了测试。它有效。

Xperia Arc S 中的传感器 API 不支持光传感器。您需要使用相机 API 访问光传感器。您可以使用以下代码。

/***
 * Attempts to set camera flash torch/flashlight mode on/off
 * @param isOn true = on, false = off
 * @return boolean whether or not we were able to set it
 */
public boolean setFlashlight(boolean isOn)
{
    if (mCamera == null)
    {
        return false;
    }
    Camera.Parameters params = mCamera.getParameters();
    String value;
    if (isOn) // we are being ask to turn it on
    {
        value = Camera.Parameters.FLASH_MODE_TORCH;
    }
    else  // we are being asked to turn it off
    {
        value =  Camera.Parameters.FLASH_MODE_AUTO;
    }

    try{    
        params.setFlashMode(value);
        mCamera.setParameters(params);

        String nowMode = mCamera.getParameters().getFlashMode();

        if (isOn && nowMode.equals(Camera.Parameters.FLASH_MODE_TORCH))
        {
            return true;
        }
        if (! isOn && nowMode.equals(Camera.Parameters.FLASH_MODE_AUTO))
        {
            return true;
        }
        return false;
    }
    catch (Exception ex)
    {
        MyLog.e(mLOG_TAG, this.getClass().getSimpleName() +  " error setting flash mode to: "+ value + " " + ex.toString());
    }
}

只需从上面的链接复制代码以使其更清晰。

Camera.Parameters.FLASH_MODE_TORCH replacement for Android 2.1

Check the link above. I used this to test on an Arc S. It works.

The sensor API does not support light sensor in Xperia Arc S. You need to access Light sensor using Camera API. You can use the following code.

/***
 * Attempts to set camera flash torch/flashlight mode on/off
 * @param isOn true = on, false = off
 * @return boolean whether or not we were able to set it
 */
public boolean setFlashlight(boolean isOn)
{
    if (mCamera == null)
    {
        return false;
    }
    Camera.Parameters params = mCamera.getParameters();
    String value;
    if (isOn) // we are being ask to turn it on
    {
        value = Camera.Parameters.FLASH_MODE_TORCH;
    }
    else  // we are being asked to turn it off
    {
        value =  Camera.Parameters.FLASH_MODE_AUTO;
    }

    try{    
        params.setFlashMode(value);
        mCamera.setParameters(params);

        String nowMode = mCamera.getParameters().getFlashMode();

        if (isOn && nowMode.equals(Camera.Parameters.FLASH_MODE_TORCH))
        {
            return true;
        }
        if (! isOn && nowMode.equals(Camera.Parameters.FLASH_MODE_AUTO))
        {
            return true;
        }
        return false;
    }
    catch (Exception ex)
    {
        MyLog.e(mLOG_TAG, this.getClass().getSimpleName() +  " error setting flash mode to: "+ value + " " + ex.toString());
    }
}

Just copied the code from the above link to make it more clear.

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