Android学习 Service(一)之如何写一个最基本的Service

来源:互联网 发布:淘宝网页 编辑:程序博客网 时间:2024/05/21 17:37
1、定义Service
继承Service类,复写onBind,onCreate,onStartCommand,onDestroy方法
2、在manifest中声明
同activity声明,可以通过在<intent-filter>中添加<action android:name="AAA"></action>标签设置过滤器。
3、启动service
显示启动:intent.set(this,myservice.class)
隐式启动:intent.setAction("AAA");
                    intent.setPackage("程序包名");
通过调用startService(intent),stopService(intent)来启动或停止service。

service
创建时:onCreate,onStartCommand
启动时:onStartCommand
停止时:onDestroy

0 0