gamexg
V2EX  ›  Android

android 后台推送,通过 alarmmanager WAKEUP 每分钟发 1 个心跳, adb shell dumpsys alarm 显示被系统改成 15 分钟一个了...

  •  
  •   gamexg · Mar 11, 2015 · 16129 views
    This topic created in 4082 days ago, the information mentioned may be changed or developed.

    后台推送心跳包应该怎么做?尝试了2天,最好的成绩是通过

    manager.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, SystemClock.elapsedRealtime()+t,
                    pendingIntent);
    

    正常工作了1个小时,然后就被系统把每分钟一个的延时改成了15分钟一次...

    实在是想不出来好办法了...

    5 replies    2015-03-12 19:47:01 +08:00
    zgxltxz
        1
    zgxltxz  
       Mar 12, 2015
    If your application has strong ordering requirements there are other APIs that you can use to get the necessary behavior; see setWindow(int, long, long, PendingIntent) and setExact(int, long, PendingIntent).

    https://developer.android.com/reference/android/app/AlarmManager.html#set(int
    gamexg
        2
    gamexg  
    OP
       Mar 12, 2015
    @zgxltxz setExact android 4.4以上版本才有。

    我试一下改成服务端发心跳试试。
    zgxltxz
        3
    zgxltxz  
       Mar 12, 2015
    @gamexg 可以试试Service加定时器的方式,然后用Alarm定时检查Service是否正常运行
    gamexg
        4
    gamexg  
    OP
       Mar 12, 2015
    @zgxltxz android alarmmanager WAKEUP 是我知道的的唯一一个可以在CPU关闭后还能唤醒手机执行程序的方式。

    除此之外就只有持有唤醒锁强制 CPU 不关闭了。

    另一个待机唤醒的方式是网络数据到达,也会唤醒 CPU 处理,这需要由服务端发起,但是如果由于意外状况导致长连接断开,这个唤醒方式会失效。
    zgxltxz
        5
    zgxltxz  
       Mar 12, 2015
    @gamexg 这样,用判断目标机器API Level,低于4.4用老办法,高于等于用新方法。
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   4666 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 44ms · UTC 01:03 · PVG 09:03 · LAX 18:03 · JFK 21:03
    ♥ Do have faith in what you're doing.