1

我想在手机震动时锁定屏幕我已经创建了震动监听器并且它检测到手机的震动代码是..

 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();
  }
}

警报对话框在那里,但它没有锁定手机我该怎么做

4

0 回答 0