0

我有一个在给定时间(基于用户设置)启动全屏活动的应用程序。发生这种情况时,我需要唤醒设备,以便用户可以看到活动。我使用 AlarmManager 来安排这些事件,它似乎工作正常(我没有注意到任何错过的警报事件),但是,在某些设备上,屏幕没有打开。在这种情况下,如果我手动唤醒设备,那么我可以看到我的活动在那里并且它正在运行它只是没有打开屏幕。我不能每次都复制它,有时它有效,有时它不。有些设备一直运行良好。我可以在不同的操作系统版本上看到这个问题,所以我猜它不是特定于 SDK 的。

我如何设置 AlarmManager 的示例:

Intent myIntent = new Intent(context, AlarmActivity.class);
myIntent.putExtras(extras);
PendingIntent pIntent = PendingIntent.getActivity(context, ID, myIntent, PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager alarm = (AlarmManager)
context.getSystemService(Context.ALARM_SERVICE); 
alarm.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + timeInMillis, pIntent);

这是我在 AlarmActivity 的 onCreate 中使用的:

PowerManager pm;
WakeLock wakeLock;
WakeLock cpuWake;

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);

    pm = (PowerManager) getSystemService(POWER_SERVICE);
    cpuWake = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, TAG);
    cpuWake.acquire();
    wakeLock = pm.newWakeLock(PowerManager.ACQUIRE_CAUSES_WAKEUP
            | PowerManager.FULL_WAKE_LOCK, TAG);
    if (!wakeLock.isHeld()) {
        wakeLock.acquire();
    } 

    final Window win = getWindow();
    win.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON
            | WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD
            | WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED
            | WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON
            | WindowManager.LayoutParams.FLAG_FULLSCREEN);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    setContentView(R.layout.layout);
//rest of the code

注意:我在没有 PARTIAL_WAKE_LOCK 的情况下尝试过,结果是相同的行为。

知道我做错了什么吗?

我会很感激任何评论,因为这个问题现在让我头疼。

谢谢你。

4

1 回答 1

0

Well, to speed up the debugging process I decided to use one of my test devices that I was able to use to reproduce this issue. After a lot of tries I realized that even the stock clock was behaving the same way so basically it negated all my tries. Anyway I was able to reproduce it on another device (less likely).

What I ended up using (based on other SO answers) is a BroadcastReciever for my alarms and use wakelock in a static way (acquire on receive of the broadcast and release onstop of the activity). So far it seems to work.

I had another wakelock issue in a service, for that I ended up using wakefulbroadcastreceiver and startwakefulservice (for this its enough to wake the cpu).

于 2014-03-24T19:32:59.767 回答