如何找到手机是否处于休眠/待机模式下为Android [英] How to find whether phone is in sleep/idle mode for Android

查看:846
本文介绍了如何找到手机是否处于休眠/待机模式下为Android的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何找到手机是否处于休眠/待机模式下为Android?

我的问题是我能够通过报警经理唤醒从sleepmode电话

但是,当手机没有睡眠模式,并在同一时刻,如果报警管理器用于唤醒phone..android力关闭应用程序。

是否有反正找到电话是否处于休眠或空闲模式?(黑屏)

更新:

我的要求:

当手机处于睡眠模式 - >意图应该从服务启动 当手机不在睡眠模式 - >同样的意图应该从服务启动

下面没有一个解决方案,完美地工作所以这里是我这工作完美小调整:):)

  //待意图我报警经理
  意图int​​entaa =新的意图(这一点,MyBroadcastReceiver.class);
  PendingIntent pendingIntent = PendingIntent.getBroadcast(此,0,intentaa,0);

  //意图将推出,其中MyIntent是意图
  意图int​​enta =新意图();
  intenta.setClass(这一点,MyIntent.class);
  intenta.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

  //真code
  尝试
  {
   startActivity(intenta);

  }赶上(例外五)
  {

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

   }
 

解决方案

您可以检查画面调用的 isScreenOn 方法。

请注意:这仅可用于2.1及以上。

How to find whether phone is in sleep/idle mode for Android?

My problem is I am able to wake up the phone from sleepmode by using alarm manager

But when the phone is not sleep mode and at the same instant if Alarm manager is used to wake the phone..android force closes the App..

whether there is anyway to find whether the Phone is in sleep or idle mode?(Black screen)

Update:

My requirement:

When the phone is in sleep mode ---> Intent should be launched from the service When the phone is not in sleep mode --> The same Intent should be launched from the service

None of the solutions below worked perfectly so here is my little tweak which worked perfectly :):)

  //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); 

   }    

解决方案

You can check if the screen is turned off by calling isScreenOn method.

Note:This only can be used for 2.1 and above..

这篇关于如何找到手机是否处于休眠/待机模式下为Android的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆