日积月累 | Android面试:最简单的Android Service 你真的能对答如流吗?
发布日期:2021-04-30 21:11:22 浏览次数:91 分类:精选文章

本文共 2780 字,大约阅读时间需要 9 分钟。

Service ?????

??????????????????????????????????????????? Service ???????????


??????????????????????? Activity ????????? Activity ??????????????? Activity ???????????????????????

?? Service ???????????????

?????????????????????

  • ?????????????? Activity ???????????????????????????????????????????????????????????????

?????? android:configChanges ??????? Activity ???????

  • ?????????????????? ContentView ???????????????

?????? Service

  • ???? Service???????????????????

Service ?????

Service ?????????????????????????? UI ??????????????

1. startService() ??

  • ?????? startService() ???? Service??????Service ??????????????? Service ?????????????? start ? Service ???????????????????????????????? Service ???? stopSelf() ???????? stopService() ?????

2. bindService() ??

  • ???????? bindService() ????? Service??????? Service ??????????????????????????Service ?????? unBind ?????? Service ???????????????????????? unBind ????? Service ??????

Service ?????

Service ??????????????????????

Service
??? onCreate()
??? onStartCommand() ? onBind()
? ??? doWork()
? ??? ...
??? onDestroy()

???

  • Service ????????????????????????
  • ???????entire lifetime??? onCreate() ????? onDestroy() ???
  • ???????activity lifetime??? onStartCommand() ? onBind() ????????? startService() ? bindService() ???start ?????????????????????????? bind ???? onUnbind() ????Service ??????????

?????

  • ??? startService() ?? bindService() ?? Service?onCreate() ? onDestroy() ??????

Service ? onCreate() ?????????

Service ???????????????????????????????????????


? Service ???????

???? Service ????????????????

  • ?? Thread???????????????????????????????
  • ?? AsyncTask ? HandlerThread??? Thread ?????
  • ?????????????????????

    ??IntentService ??????????


    ????? IntentService

    IntentService ??? Service?? Service ???????????????? IntentService ????????????? onHandleIntent() ????????????? Intent ????????????????????????????????????????????? stopSelf() ???onHandleIntent() ??????????

    ?? IntentService ????

  • ???????????? onStartCommand() ???? Intent?
  • ???? onHandleIntent() ??????????? Intent ?????????????multi-threading???????
  • ??????????????? Service??????? stopSelf() ???
  • ????? onBind() ?????? null?
  • ????? onStartCommand() ???????? Intent ????????? onHandleIntent()???????????? Intent ???

  • ??

    ?????? Service ????????? Service ?????????????????????????????? Service??? Service ????????????????????????????Service ? UI ????????????????????????????????????????????????????????????? Thread??? AsyncTask, ThreadHandler???????


    ??????

    ???????????????????????????? Android ?????????????????????????????????????????????+?????????? + ??????????????????????????????????????


    ??

    ?????????????????????????????????????


    Android ????????????????????

    ???????????????????????????~


    ?????????????????????58????360????????????????????????????????????????????????????

    上一篇:你努力工作会让老板感动吗?
    下一篇:我为何从测试转测试开发,并坚持了10年?

    发表评论

    最新留言

    不错!
    [***.144.177.141]2026年06月14日 19时10分10秒

    关于作者

        喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
    -- 愿君每日到此一游!

    推荐文章