【问题标题】:android show activity when phone sleepandroid在手机睡眠时显示活动
【发布时间】:2012-10-21 12:30:08
【问题描述】:

我有一个广播接收器,他的工作是打开一个活动。 (特别是当我收到某条短信时)。 当我的手机开机并使用它时,它工作得很好。
但是当我的手机处于睡眠模式时,什么也没有发生。
我是否需要在我的广播接收器中添加一个代码来唤醒手机并释放屏幕锁定(如果有)然后打开活动?
还是有更好的解决方案?

这是广播接收器onReceive函数中的相关代码:

    //Create a new PendingIntent and add it to the AlarmManager
Intent intent = new Intent(context, Alarm.class);
intent.putExtra("coordinates", coordinates);
PendingIntent pendingIntent = PendingIntent.getActivity(context,
    0, intent, PendingIntent.FLAG_ONE_SHOT);
AlarmManager am = (AlarmManager)context.getSystemService(Activity.ALARM_SERVICE);
am.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + 100, pendingIntent);

【问题讨论】:

  • 是的,但也许还有其他解决方案。我如何检查我是否需要这样做? (手机是否处于睡眠状态?)
  • 重点是,如果你使用唤醒锁,无论手机是否开机,手机都会执行它的动作。它专门用于将手机从睡眠状态唤醒并执行您的操作(因此您无需检查 - 您只知道它将执行,无论手机状态如何)

标签: android android-activity


【解决方案1】:

您需要使用 WakeLock 来完成此操作。

这个特别的教程帮助了我:http://it-ride.blogspot.com/2010/10/android-implementing-notification.html

另外一个更好的关于唤醒锁的教程:https://github.com/commonsguy/cwac-wakeful

【讨论】:

  • 如何检查我是否需要这样做? (手机是否处于睡眠状态?)
  • 如果您想确保某事发生,无论手机是否开机 - 您都将使用唤醒锁。这样可以确保无论手机是否处于唤醒状态,都可以执行您的操作(因此您无需检查)。我添加了指向我使用的另一个有用教程的链接。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-06-20
  • 1970-01-01
  • 1970-01-01
  • 2021-02-22
  • 1970-01-01
  • 2011-12-22
相关资源
最近更新 更多