简体   繁体   English

Android:根据JB上的接近发送器关闭屏幕

[英]android: turn screen off based on proximity sendor on JB

I need to replicate the functionality of the stock dialer in my app - when the proximity sensor detects the phone is close to my face, turn the screen off, and turn it back on when I take the phone away from my face. 我需要在应用程序中复制股票拨号器的功能-当接近传感器检测到手机靠近我的脸时,请关闭屏幕,然后再将手机从脸部拿开时再打开。

There's a partially working solution in this thread . 该线程中有一个部分有效的解决方案。 It relies on a private API which is suboptimal, but what's way worse, works on one out of 3 devices I've tested. 它依赖于次优的私有API,但更糟糕的是,它只能在我测试过的3台设备中运行。 I'm targeting Android 4.03 and above and I'm testing against the Galaxy S3, Galaxy Nexus and Nexus 4. The solution I linked to works on the GN. 我的目标是Android 4.03及更高版本,并且正在针对Galaxy S3,Galaxy Nexus和Nexus 4进行测试。我链接的解决方案可在GN上使用。 On the Nexus 4, it doesn't nothing whatsoever (which I find particularly puzzling seeing that the official androidnadroid dialer uses the same code and it works there. On the S3, acquiring the wakeLock seems to perform the same action as pushing the power button (locks the phone, and thus turns off the proximity sensor). 在Nexus 4上,它什么也没做(我感到特别困惑,因为官方的androidnadroid拨号器使用相同的代码并且可以在那儿工作。在S3上,获取akeLock似乎与按电源按钮执行相同的操作(锁定手机,从而关闭接近传感器)。

As calling powerManager.goToSleep requires permissions that require a phone root this is off the table, setting screen brightness seems hit and miss (and doesn't work properly on any of my 3 phones - I can vary brightness but never manage to turn the screen off) and the only other way seems to be yet another private API (setScreenState - which in my case would mean Java interop as I'm using Mono4Android), I'm wondering - is there any way to comprehensively get this done, or is this just one of those things that we're not supposed to do? 由于调用powerManager.goToSleep需要权限,而该权限要求手机根目录不在桌面上,因此设置屏幕亮度似乎有点麻烦(并且在我的3部手机中都无法正常工作-我可以改变亮度,但从未设法打开屏幕)关闭),而唯一的另一种方法似乎是另一个私有API(setScreenState-在我的情况下,这意味着Java互操作,因为我正在使用Mono4Android),我想知道-是否有任何方法可以全面完成此操作,或者这只是我们不应该做的事情之一? Still though, there's plenty of call making apps where such functionality is required, so the Skypes, Vibers & Co. of the world need to have this figured out somehow and I'd appreciate some insight as I'm at the end of my wisdom. 尽管如此,仍有大量需要此类功能的呼叫应用程序,因此世界各地的Skypes,Vibers&Co.都需要以某种方式解决这一问题,在此我不知所措,我将不胜感激。

Here's what I learned in the meantime - don't try to acquire that special wakeLock unless the call state is something other than idle. 这是我在此期间学到的-除非调用状态不是空闲状态,否则不要尝试获取特殊的唤醒锁。 If there is a call, the lock works, but there's a catch: On Google Phones (tried with GN and G4), the activity stays active even though the screen is off. 如果有电话,锁将起作用,但有一个陷阱:在Google Phones(尝试GN和G4)上,即使屏幕关闭,活动仍保持活动状态。 On my S3, once I acquire the lock, the activity is stopped (not paused... stopped!). 在我的S3上,一旦我获得了锁,活动即停止(不暂停...已停止!)。 So, you need to write your code in a way to keep the proximity sensor active (I had it unregister after onPause which lead me to believe it didn't work on the S3) - having to keep the sensor with an stopped activity sure makes things harder on you, but it's still doable. 因此,您需要以某种方式编写代码以保持接近传感器处于活动状态(在onPause之后我注销了它,这使我相信它在S3上不起作用)-必须保持传感器处于停止状态才能确保事情对您来说很难,但仍然可行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM