【发布时间】: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);
【问题讨论】:
-
是的,但也许还有其他解决方案。我如何检查我是否需要这样做? (手机是否处于睡眠状态?)
-
重点是,如果你使用唤醒锁,无论手机是否开机,手机都会执行它的动作。它专门用于将手机从睡眠状态唤醒并执行您的操作(因此您无需检查 - 您只知道它将执行,无论手机状态如何)