广播接收器中的Android startActivity';s ON接收函数

广播接收器中的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

我的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, 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;
}