java - Android - 我如何检测 Android 将进入屏幕锁定模式,我需要在该阶段完成一项工作

标签 java android android-intent

我有一个运行良好的应用程序,但问题是当它自动进入屏幕锁定模式时,我需要完成一项工作。解锁屏幕我可以使用 Intent 接收器进行检测,但无法找到如何知道何时通过按电源按钮或自动进入屏幕锁定模式。

有办法找到吗?我尝试过关注但没有成功。

<uses-permission android:name="android.permission.WAKE_LOCK" />

PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
wl = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK, "DoNjfdhotDimScreen");   



@Override
protected void onPause() {
  super.onPause();
  wl.release();
  Toast.makeText(this, "up!!!!." , Toast.LENGTH_LONG).show();
  Vibrator vibrator = (Vibrator) this.getSystemService(Context.VIBRATOR_SERVICE);
  vibrator.vibrate(2000);

}//End of onPause

@Override
protected void onResume() {
  super.onResume();
  wl.acquire();
  Toast.makeText(this, "up!!!!." , Toast.LENGTH_LONG).show();
  Vibrator vibrator = (Vibrator) this.getSystemService(Context.VIBRATOR_SERVICE);
  vibrator.vibrate(2000);

}//End of onResume 

编辑:尝试过,但在 onCreate 下没有运气

KeyguardManager myKM = (KeyguardManager) this.getSystemService(Context.KEYGUARD_SERVICE);
if( myKM.inKeyguardRestrictedInputMode()) {
 //it is locked
  Toast.makeText(this, "up!!!!." , Toast.LENGTH_LONG).show();
  Vibrator vibrator = (Vibrator) this.getSystemService(Context.VIBRATOR_SERVICE);
  vibrator.vibrate(2000);      
} else {
 //it is not locked
  Toast.makeText(this, "up!!!!." , Toast.LENGTH_LONG).show();
  Vibrator vibrator = (Vibrator) this.getSystemService(Context.VIBRATOR_SERVICE);
  vibrator.vibrate(2000);      
} 

编辑:由于系统性能的原因,你不能用 mainfest 做 3 件事,要仍然做到这一点,必须使用以下动态注入(inject)。

  private BroadcastReceiver mPowerKeyReceiver = null;

  private void registBroadcastReceiver() {
      final IntentFilter theFilter = new IntentFilter();
      /** System Defined Broadcast */
      theFilter.addAction(Intent.ACTION_SCREEN_ON);
      theFilter.addAction(Intent.ACTION_SCREEN_OFF);

      mPowerKeyReceiver = new BroadcastReceiver() {
          @Override
          public void onReceive(Context context, Intent intent) {
              String strAction = intent.getAction();

              if (strAction.equals(Intent.ACTION_SCREEN_OFF) || strAction.equals(Intent.ACTION_SCREEN_ON)) {
                  // > Your playground~!
                Log.d(TAG, ">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>onDestroy");
              }
          }
      };

      getApplicationContext().registerReceiver(mPowerKeyReceiver, theFilter);
  }

  private void unregisterReceiver() {
      int apiLevel = Integer.parseInt(Build.VERSION.SDK);

      if (apiLevel >= 7) {
          try {
              getApplicationContext().unregisterReceiver(mPowerKeyReceiver);
          }
          catch (IllegalArgumentException e) {
              mPowerKeyReceiver = null;
          }
      }
      else {
          getApplicationContext().unregisterReceiver(mPowerKeyReceiver);
          mPowerKeyReceiver = null;
      }
  }

最佳答案

也许您可以将广播接收器用于“android.intent.action.SCREEN_ON”和“android.intent.action.SCREEN_OFF”。

关于java - Android - 我如何检测 Android 将进入屏幕锁定模式,我需要在该阶段完成一项工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17582314/

相关文章:

java - 不同的 Intent 构造函数有什么作用?

android - 如何从广播接收器访问我的主要 Activity 中的 webview?

java - 无法将 Junit 添加到我的 Maven 项目 (Netbeans)

Java 树与列

java - 尝试使用 Spring 和扩展 Hibernate 的 JpaRepository 的自定义 GenericDao 接口(interface)来使用 EhCache

Android TransactionTooLargeException 不知道是什么原因造成的

java - GWT devmode netbeans - 错误

android - Google Play Location Services 9.2.0 缺少谷歌归因 Android 资源

java - 安卓/ Gradle : how to find the good mix of Google dependencies versions?

android - getExtras().getSerializable() 和 getSerializableExtra() 的区别