android 开发中service的使用

2025-06-30 16:42:18

1、点击service类,在as或者idea中使用ctrl+H查看类继承关系,则看到service继承与context,和activity属于同类

android 开发中service的使用

2、新建service,在idea(as)中有直接创建service的按钮 ,自动继承与service,并在清单文件中自动注册,我们不要关心其他地方忘记配置

android 开发中service的使用
android 开发中service的使用

3、service有两种启动模式,一种start ,一种bind,在使用start方式时,可以对其内部抽象方法返回null,在使用bind时则需要进行实现

android 开发中service的使用
android 开发中service的使用

4、start启动方式使用startService 启动 会以此调用onCreate (一次) onDestroy对应onStartCommand(每次调用)

android 开发中service的使用

5、点击测试按钮,则调用service方法,在启动service,如果不销毁,则一直在程序中运行

android 开发中service的使用
android 开发中service的使用

6、bind方法则需要进行重写onBind 一般使用内部类实现,继承Binder即可,bindService进行绑定服务

android 开发中service的使用
android 开发中service的使用
声明:本网站引用、摘录或转载内容仅供网站访问者交流或参考,不代表本站立场,如存在版权或非法内容,请联系站长删除,联系邮箱:site.kefu@qq.com。
猜你喜欢