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

  • 相关阅读:
    Luogu P2633 Count on a tree
    Luogu P4011 孤岛营救问题
    Luogu P3157 [CQOI2011]动态逆序对
    SCOI2015 国旗计划
    AT2165 Median Pyramid Hard
    BZOJ2959 长跑
    SCOI2015 情报传递
    SDOI2011 染色
    SCOI2010 幸运数字
    SHOI2016 黑暗前的幻想乡
  • 原文地址:https://www.cnblogs.com/liu666bin/p/2918494.html
Copyright © 2011-2022 走看看