我只想为Android开发一个后台服务,我可以在其中禁用接近传感器的功能。我不想在通话期间关闭显示器。
以下代码示例不起作用。
1
PowerManager powerManager;
PowerManager.WakeLock wakeLock;
int field = 0x00000020;
try {
// Yeah, this is hidden field.
field = PowerManager.class.getField("PROXIMITY_SCREEN_OFF_WAKE_LOCK").getInt(null);
} catch (Exception e) {
}
powerManager = (PowerManager) context.getSystemService(context.POWER_SERVICE);
wakeLock = powerManager.newWakeLock(field, "MyWakeLock");
wakeLock.acquire();
2
PowerManager mPowerManager;
PowerManager.WakeLock mWakeLock;
mPowerManager = (PowerManager) getSystemService(Context.POWER_SERVICE);
KeyguardManager km = (KeyguardManager) getSystemService(Context.KEYGUARD_SERVICE);
final KeyguardManager.KeyguardLock kl = km.newKeyguardLock("MyKeyguardLock");
kl.disableKeyguard();
PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
mWakeLock = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK | PowerManager.ACQUIRE_CAUSES_WAKEUP
| PowerManager.ON_AFTER_RELEASE, "MyWakeLock");
// mWakeLock = mPowerManager.newWakeLock(PowerManager.SCREEN_DIM_WAKE_LOCK | PowerManager.ACQUIRE_CAUSES_WAKEUP, "Service");
mWakeLock.acquire();
有解决方案吗?