android Service后台服务的使用

服务开启方式如下,
AndroidManifest.xml中需要配置service

 <service android:name="hk.newsoft.demo.service.YYYService" />

该service类需要继承service

Intent intent = new Intent(XXXActivity.this,YYYService.class);
startService(intent);
在使用后台服务的过程中还需注意,当客户端关闭时,service会重启,如果后台服务需要使用到前端的数据,此时会报一系列的空指针异常,因此可在开启service的同时将service需要的相关数据保存到本地文件中(客户端关闭时内存中的数据也会被清空)。然后service通过I/O流序列化读取对象。


注意点:service属于UI线程,所以相关的耗时工作需要开辟子线程处理。

猜你喜欢

转载自blog.csdn.net/huashanjuji/article/details/46730223