我想在手机摇晃的情况下锁定屏幕,我已经创建了摇晃的听众,并且检测到手机发抖 代码是..
Receiver myReceiver;
Context context1;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final Vibrator vibe = (Vibrator)getSystemService(Context.VIBRATOR_SERVICE);
mShaker = new ShakeListener(this);
mShaker.setOnShakeListener(new ShakeListener.OnShakeListener () {
public void onShake()
{
vibe.vibrate(100);
new AlertDialog.Builder(MainActivity.this)
.setPositiveButton(android.R.string.ok, null)
.setMessage("Shooken!")
.show();
PowerManager manager = (PowerManager) getSystemService(Context.POWER_SERVICE);
// Choice 1
// manager.goToSleep(amountOfTime);
// Choice 2
PowerManager.WakeLock wl = manager.newWakeLock(PowerManager.SCREEN_DIM_WAKE_LOCK, "Your Tag");
wl.acquire();
wl.release();
}
});
}
@Override
public void onResume()
{
mShaker.resume();
super.onResume();
}
@Override
public void onPause()
{
mShaker.pause();
super.onPause();
}
}
警告对话框在那里,但它没有锁定手机我该怎么办