【问题标题】:How to find whether phone is in sleep/idle mode for Android如何查找手机是否处于 Android 的睡眠/空闲模式
【发布时间】:2011-07-14 10:25:16
【问题描述】:

如何在 Android 上查看手机是否处于睡眠/空闲模式?

我的问题是我可以使用闹钟管理器将手机从睡眠模式中唤醒

但是当手机没有进入睡眠模式并且在同一时刻如果使用闹钟管理器唤醒手机..android强制关闭App..

有没有办法查到手机是休眠还是空闲?(黑屏)

更新:

我的要求:

当手机处于睡眠模式时 ---> Intent 应该从服务中启动 当手机不处于睡眠模式时 --> 应该从服务中启动相同的 Intent

以下解决方案都不能完美运行,所以这是我的小调整,效果很好:):)

  //Pending intent for my alarm manager 
  Intent intentaa=new Intent(this,MyBroadcastReceiver.class);
  PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, intentaa, 0);

  //Intent to be launched where MyIntent is the intent
  Intent intenta = new Intent();
  intenta.setClass(this, MyIntent.class);
  intenta.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

  //Real code
  try
  {
   startActivity(intenta); 

  }catch(Exception E)
  {

      AlarmManager am = (AlarmManager)getSystemService(ALARM_SERVICE);
      am.set(AlarmManager.ELAPSED_REALTIME_WAKEUP,SystemClock.elapsedRealtime(),
      pendingIntent);
      startActivity(intenta); 

   }    

【问题讨论】:

    标签: android


    【解决方案1】:

    您可以通过调用isScreenOn方法检查屏幕是否关闭。

    注意:这只能用于2.1及以上..

    【讨论】:

    • PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE); boolean isScreenOn = ((Object) pm).isScreenOn();我收到一条错误消息或 isScreenOn 方法用于添加演员
    • 当我使用那里给出的 isScreenon 代码时..我收到此错误“将演员表添加到 pm”,当我添加演员表时,我再次遇到相同的错误
    • 您使用的是哪个版本的 sdk。它为 api level >= level 7 定义
    • 所以在这种情况下你将不得不使用 MByD 的解决方案
    • boolean isScreenOn = ((Object) pm).isScreenOn(); 你到底想做什么?
    【解决方案2】:

    isScreenOn 方法仅在屏幕打开或关闭时返回。它不表示休眠模式,因为Android休眠模式与Android屏幕关闭模式不同。在 Android 中,睡眠模式可能会在屏幕关闭(空闲模式)后的几毫秒内(取决于唤醒锁)进入 - 但不能保证(参考 this post)。

    我认为您可以尝试使用SystemClock.uptimeMillis(),SystemClock.elapsedRealtime() 进行比较。

    【讨论】:

      【解决方案3】:

      查看isInteractive()

      PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
      if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT_WATCH) {
        return pm.isInteractive();
      } else {
        return pm.isScreenOn();
      }
      

      【讨论】:

        【解决方案4】:

        我不确定这就是你想要的,但你可以使用Intent.ACTION_SCREEN_OFF 来确定。见here an example

        【讨论】:

        • 您的解决方案工作正常,除非在发送 ACTION_SCREEN_OFF 之后安装应用程序 - 例如当应用程序在屏幕关闭的情况下安装时。
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-05-17
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多