即使在屏幕关闭后我也会进行烤面包(我正在使用Moto e)。当屏幕关闭时,我将手移过传感器区域两次或更多次,立即使用电源按钮打开屏幕并解锁手机我可以看到吐司。我可以看到"看到"和"哟"因为我编写了近距离和远距离,我使用了Toast.LENGTH_LONG所以吐司有几秒钟。
现在我想打开屏幕并使用接近传感器打开屏幕锁。
我仍然感到困惑,我有Android问题3708吗? 非常感谢。
public void onSensorChanged(SensorEvent event) {
Log.i(TAG, "onSensorChanged().");
if(event.values[0]==3.0)
{
Toast.makeText(getApplicationContext(), "see", Toast.LENGTH_LONG).show();
// startService(new Intent(this, TheService.class));
// i want to open screen lock here
new ServiceLockTest().data();
}
else
Toast.makeText(getApplicationContext(), "yo", Toast.LENGTH_LONG).show();
}
@Override
public void onCreate() {
super.onCreate();
mSensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
manager = (PowerManager) getSystemService(Context.POWER_SERVICE);
mWakeLock = manager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, TAG);
registerReceiver(mReceiver, new IntentFilter(Intent.ACTION_SCREEN_OFF));
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
super.onStartCommand(intent, flags, startId);
Notification.Builder mBuilder =
new Notification.Builder(getApplicationContext())
.setContentTitle("see")
.setSmallIcon(R.drawable.ic_lock_idle_alarm)
.setContentText("yo");
notification = mBuilder.build();
startForeground(Process.myPid(),notification);
registerListener();
mWakeLock.acquire();
return START_STICKY;
}
//-------------------------------------------------------------------------------
public class ServiceLockTest extends Activity {
public static final String TAG = ServiceLockTest.class.getName();
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
@Override
public void onResume() {
super.onResume();
startService(new Intent(this, TheService.class));
}
void data() {
// TODO Auto-generated method stub
Window window = this.getWindow();
window.addFlags(LayoutParams.FLAG_DISMISS_KEYGUARD);
window.addFlags(LayoutParams.FLAG_SHOW_WHEN_LOCKED);
window.addFlags(LayoutParams.FLAG_TURN_SCREEN_ON);
}
}
答案 0 :(得分:0)
试试这个:
Window window = this.getWindow();
window.addFlags(LayoutParams.FLAG_DISMISS_KEYGUARD);
window.addFlags(LayoutParams.FLAG_SHOW_WHEN_LOCKED);
window.addFlags(LayoutParams.FLAG_TURN_SCREEN_ON);
OR
PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
WakeLock wakeLock = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK | PowerManager.ACQUIRE_CAUSES_WAKEUP | PowerManager.ON_AFTER_RELEASE, "MyWakeLock");
wakeLock.acquire();
需要许可<uses-permission android:name="android.permission.WAKE_LOCK" />
OR
WindowManager.LayoutParams params = getWindow().getAttributes();
params.flags |= LayoutParams.FLAG_KEEP_SCREEN_ON;
params.screenBrightness = 1;
getWindow().setAttributes(params);
OR
KeyguardManager km = (KeyguardManager) context.getSystemService(Context.KEYGUARD_SERVICE);
final KeyguardManager.KeyguardLock kl = km.newKeyguardLock("MyKeyguardLock");
kl.disableKeyguard();
PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
wakeLock = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK | PowerManager.ACQUIRE_CAUSES_WAKEUP
| PowerManager.ON_AFTER_RELEASE, "MyWakeLock");
wakeLock.acquire();
当你完成它时,不要忘记释放唤醒锁。
您需要申请权限<uses-permission android:name="android.permission.DISABLE_KEYGUARD" />
和<uses-permission android:name="android.permission.WAKE_LOCK" />
警告: kl.disableKeyguard()
绕过任何PIN或模式解锁