android Service : onStartCommand intent为null
参考资料:
http://hold-on.iteye.com/blog/2024926
问题:
service中加入onStartCommand之后,不定期crash。
分析:
经查看onStartCommand中代码,只有intent为空才可能报NullPointException 。
intent的参数是null,原因是这个intent参数是通过startService(Intent)方法所传递过来的,但是如果Service在你的进程退出后有可能被系统自动重启,这个时候intent就会是null.
为什么service会自动重启呢?
onStartCommand里return START_STICKY,系统内存不足杀掉service之后,若内存足够,则会自动重启service。
解决方法:
在使用intent前需要判断一下是否为空。