zoukankan      html  css  js  c++  java
  • Android Service

    Service的生命周期

    Service对象不能自己启动,需要通过某个Activity、Service或者其他Context对象来启动。启动的方法有两种,Context.startService和Context.bindService()。
    两种方式的生命周期是不同的,具体如下所示。

    Context.startService方式的生命周期:
    启动时,startService –> onCreate() –> onStart()
    停止时,stopService –> onDestroy()

    Context.bindService方式的生命周期:
    绑定时,bindService  -> onCreate() –> onBind()
    解绑定时,unbindService –>onUnbind() –> onDestory()

    判断service是否已运行

    pubic boolean isServiceRun(Context context){
         ActivityManager am = (ActivityManager)context.getSystemService(context.ACTIVITY_SERVICE);
         List<RunningServiceInfo> list = am.getRunningServices(30);
         for(RunningServiceInfo info : list){
             if(info.service.getClassName.equals("service的全称(一般为包名+service类的名称)")){
                      return true;
             }
        }
        return false;
    }
  • 相关阅读:
    HDU 5794
    HDU 5794
    3070 Fibonacci 矩阵快速幂
    数论基础
    hdu 1061 Rightmost Digit 快速幂
    poj 2305 Basic remains java
    poj 1001 Exponentiation
    hdu 2054 A == B ? (java)
    java大数练习
    hdu3018 Ant Trip 欧拉回路
  • 原文地址:https://www.cnblogs.com/rfheh/p/4164827.html
Copyright © 2011-2022 走看看