zoukankan      html  css  js  c++  java
  • Service简介

    创建时需要

    1.)定义一个继承Service的子类

    2.)在AndroidManifest.xml文件中配置Service

    生命周期及主要回调方法

    abstract IBinder onBind(Intent intent)

      该方法是Service子类必须实现的方法,该方法返回一个IBinder对象,应用程序通过它来跟Service组件通信

    void onCreate():当gai Service第一次被创建后将立即回调该方法

    void onDestroy() :当该Service被关闭之前将会回调该方法

    void onStartCommand(Intent intent,int flags,int startId):该方法早期的版本是void onStart(Intent intent,int startId),

              每次客户端调用StartService(Intent)方法启动该Service时都会回调该方法

    boolean onUnbind(Intent intent):当该service上绑定的所有客户端都断开连接时将会回调该方法

    AndroidManifest.xml中中配置:

    <service android:name = ".serviceName">

      <intent-filter>

        <!--为该service组件的intent-filter配置action-->

        <action android:name="org.crazyit.service.SERVICE_NAME"/>

      </intent-filter>

    </service>

    service启动方式:

    1.)通过context的 startService()->访问者与service之间没有关联,即使访问者退出,service仍然正常运行

        ----对应结束service的方法为stopService();

    2.)通过context的 bindService()->访问者与service绑定在一起,访问者一旦退出service也终止

        ----对应结束service的方法为unbindService

  • 相关阅读:
    尚筹网11阿里云OSS对象存储
    阿里云的OSS对象存储
    尚筹网10用户登录
    尚筹网09用户注册
    尚筹网08环境搭建
    实体类的进一步划分
    尚筹网07分布式架构
    临时弹出一个QQ对话窗口
    Input框改placeholder中字体的颜色
    判断银行卡号的正则
  • 原文地址:https://www.cnblogs.com/liu666bin/p/2918494.html
Copyright © 2011-2022 走看看