V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
gamexg
V2EX  ›  Android

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

  •  
  •   gamexg · 2015-03-11 22:01:25 +08:00 · 7734 次点击
    这是一个创建于 3326 天前的主题,其中的信息可能已经有所发展或是发生改变。

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

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

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

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

    5 条回复    2015-03-12 19:47:01 +08:00
    zgxltxz
        1
    zgxltxz  
       2015-03-12 09:17:35 +08:00
    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
       2015-03-12 14:28:30 +08:00
    @zgxltxz setExact android 4.4以上版本才有。

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

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

    另一个待机唤醒的方式是网络数据到达,也会唤醒 CPU 处理,这需要由服务端发起,但是如果由于意外状况导致长连接断开,这个唤醒方式会失效。
    zgxltxz
        5
    zgxltxz  
       2015-03-12 19:47:01 +08:00
    @gamexg 这样,用判断目标机器API Level,低于4.4用老办法,高于等于用新方法。
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   2843 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 20ms · UTC 13:32 · PVG 21:32 · LAX 06:32 · JFK 09:32
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.