广播接收器中的Android startActivity';s ON接收函数
我的BroadcastReceive的onReceive函数中有以下代码广播接收器中的Android startActivity';s ON接收函数,android,broadcastreceiver,alarmmanager,broadcast,Android,Broadcastreceiver,Alarmmanager,Broadcast,我的BroadcastReceive的onReceive函数中有以下代码 public void onReceive(Context context, Intent intent) { String action = intent.getAction(); if (action == null) return; if (action.equals(ACTION_ALARM)) { Intent alarmPopup = new Intent(context
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (action == null) return;
if (action.equals(ACTION_ALARM)) {
Intent alarmPopup = new Intent(context, AlarmPopup.class);
int vibrateDuration = context.getSharedPreferences(PREF, 0)
.getInt(VIBRATE_DURATION, DEFAULT_VIBRATE_DURATION)
alarmPopup.putExtra(VIBRATE_DURATION, vibrateDuration);
alarmPopup.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(alarmPopup);
}
}
此代码在接收报警管理器的广播时启动活动AlarmPopup
。
启动AlarmPopup活动后,它将显示一条典型的报警消息,并在通过Intent\putExtra
传递的VibrationDuration
期间振动
在AlarmPopup的onCreate方法中,活动保持WakeLock
,使设备保持开启状态
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
wl = getLock(this);
if (!wl.isHeld()) {
Log.d(PREF, "Alarm popup acquires wake lock");
wl.acquire();
thread.run();
}
.
.
.
}
getLock
是一种同步方法,它与其他方法一样管理WakeLock
现在问题来了:即使调用了context.startActivity(alarmPopup)
,但startActivity
很少不启动活动或不按时启动,通常是1-2分钟后
<>似乎OS在创建过程中杀死了我的Alp弹出活动,或者让活动比“代码> >启动活动< /代码>实际上被调用的时间晚了一点。
真正有趣的是,当上述问题发生时,有时会记录日志消息“Alarm popup Acquisites wake lock”
,有时甚至不会记录。我认为,在这种情况下,操作系统会在执行onCreate方法的第一行或第二行时终止活动
我怎样才能解决这个问题
我是否应该在另一个线程创建AlarmPopup活动时,在onReceive的末尾放置一些虚拟代码来保存CPU
似乎OS在创建过程中杀死了我的Alp弹出活动,或者让活动比StcActudio实际上被调用的时间晚一点创建。
没有。设备只是睡着了startActivity()
是一种异步操作。当onReceive()
返回时,操作系统为AlarmManager
工作(假设您确实在使用AlarmManager
)持有的WakeLock
将被释放<在onReceive()
返回时,活动的code>onCreate()将不会运行。因此,设备可能在onReceive()
结束和您在onCreate()
中获取WakeLock
之间的时间窗口中睡着
我怎样才能解决这个问题
在onReceive()
中获取WakeLock
似乎OS在创建过程中杀死了我的Alp弹出活动,或者让活动比StcActudio实际上被调用的时间晚一点创建。
没有。设备只是睡着了startActivity()
是一种异步操作。当onReceive()
返回时,操作系统为AlarmManager
工作(假设您确实在使用AlarmManager
)持有的WakeLock
将被释放<在onReceive()
返回时,活动的code>onCreate()将不会运行。因此,设备可能在onReceive()
结束和您在onCreate()
中获取WakeLock
之间的时间窗口中睡着
我怎样才能解决这个问题
在
onReceive()
中获取WakeLock
设置alarmPopup.setFlags(Intent.FLAG\u FROM\u BACKGROUND)
在接收广播接收时尝试设置alarmPopup.setFlags(Intent.FLAG\u FROM\u BACKGROUND)
在接收广播接收时感谢您的回答。事实上,当我写这个问题的时候,我希望你能回答我的问题,这真的发生了!:)那么你是说AlarmPopup的静态唤醒锁应该在BroadcastReceiver的onReceive中获取,并在AlarmPopup的OnDestroy中释放?@Heejin:理想情况下,对于一个活动,您不需要长时间使用手动唤醒锁。如果您希望在活动处于前台时屏幕保持打开状态,我会使用android:keepScreenOn
作为小部件。无论如何,我会尽快release()
手册WakeLock
,最好是在onDestroy()之前。但是,是的,您将在onReceive()
中获得它。谢谢您的回答。事实上,当我写这个问题的时候,我希望你能回答我的问题,这真的发生了!:)那么你是说AlarmPopup的静态唤醒锁应该在BroadcastReceiver的onReceive中获取,并在AlarmPopup的OnDestroy中释放?@Heejin:理想情况下,对于一个活动,您不需要长时间使用手动唤醒锁。如果您希望在活动处于前台时屏幕保持打开状态,我会使用android:keepScreenOn
作为小部件。无论如何,我会尽快release()
手册WakeLock
,最好是在onDestroy()之前。但是,是的,您将在onReceive()
中获取它。
private static volatile PowerManager.WakeLock wlStatic = null;
synchronized private static PowerManager.WakeLock getLock(Context context) {
if (wlStatic == null) {
PowerManager mgr = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
wlStatic = mgr.newWakeLock(PowerManager.SCREEN_BRIGHT_WAKE_LOCK
| PowerManager.ACQUIRE_CAUSES_WAKEUP
| PowerManager.ON_AFTER_RELEASE, PREF);
wlStatic.setReferenceCounted(true);
}
return wlStatic;
}