peekService()返回null即使该服务正在运行,如果服务是由系统OOM杀死后重新开始 [英] peekService() returns null even though the service is running, if the service is restarted by the system after OOM kill

查看:198
本文介绍了peekService()返回null即使该服务正在运行,如果服务是由系统OOM杀死后重新开始的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的是启动服务

startService(new Intent(this, RelayService.class));

和再利用服务启动一个报警

and then the service starts an alarm using

AlarmManager am = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(this, SyncAlarmReceiver.class);
PendingIntent pi = PendingIntent.getBroadcast(this, 0, intent,
    PendingIntent.FLAG_CANCEL_CURRENT);
am.setInexactRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, 
    SystemClock.elapsedRealtime() + SYNC_EVERY_MS, SYNC_EVERY_MS, pi);

然后广播接收器 SyncAlarmReciver 正在尝试使用来获得服务

and then BroadcastReceiver SyncAlarmReciver is trying to get the service using

RelayServiceBinder relay = (RelayServiceBinder) 
    peekService(context, new Intent(context, RelayService.class));

直到该服务是由系统重新启动后,当其他应用程序需要更多的内存的应用程序就会被杀死这一切工作正常。重新启动后,重新推出的服务使用相同的code报警,但 peekService()返回null。通过调试消息,我看到背景在服务和广播接收器是一样的,以及RelayService对象,即这些对象都是在内存中的同一对象。我怎样才能解决这个问题?

It all works fine until the service is restarted by the system after application gets killed when other applications need more memory. After restart, the service relaunches the alarm using the same code, but peekService() returns null. Through debugging messages, I see that context in the service and in the broadcast receiver is the same, as well as RelayService object, i.e. these objects are the same objects in memory. How can I solve this problem?

如果有帮助,这里有链接到三块提到的code的:<一href=\"https://github.com/oakkitten/weechat-android/blob/sync/weechat-android/src/main/java/com/ubergeek42/WeechatAndroid/WeechatActivity.java#L107\"相对=nofollow>主要活动,<一个href=\"https://github.com/oakkitten/weechat-android/blob/sync/weechat-android/src/main/java/com/ubergeek42/WeechatAndroid/service/RelayService.java#L110-L113\"相对=nofollow>服务,<一个href=\"https://github.com/oakkitten/weechat-android/blob/sync/weechat-android/src/main/java/com/ubergeek42/WeechatAndroid/service/SyncAlarmReceiver.java#L12\"相对=nofollow>广播接收器。

If it helps, here are links to the three pieces of the code mentioned: main activity, service, broadcast receiver.

P.S。我知道,服务正在运行时间可持续,因为我正在密切关注这个过程,看看它运行良好,并没有重新启动以任何方式,也因为我看到这方面,启动报警前打开不无谓的干扰。

P.S. I know that the service is running continuosly because I'm watching the process closely and see it running fine and not restarting in any way, and also because I see that connection it opens before launching the alarm does not get interrupted.

推荐答案

作为建议的JesusFreke,问题是, peekService()只能返回的现有的IBinder 对象。由于该服务是由系统重新启动,并没有活动将绑定到该服务在这一点上,<​​code>的IBinder 对象不存在。因此,一个被返回。这是一个耻辱,完全的文档错过了这个事实。

As suggested by JesusFreke, the problem is that peekService() can only return an existing IBinder object. As the service is restarted by the system and there's no Activity that would bind to the service at this point, the IBinder object does not exist yet. So a null is returned. It's a shame that documentation misses this fact entirely.

由于我绝对不想启动该服务,如果它没有运行,使用 startService()似乎是一个坏主意,所以我使出查询一个静态该服务的属性(在那里反正)。

As I definitely didn't want to start the service if it wasn't running, using startService() seemed a bad idea, so I resorted to quering a static attribute of the service (which was there anyway).

这篇关于peekService()返回null即使该服务正在运行,如果服务是由系统OOM杀死后重新开始的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆