作者Azarc (风中行者时空旅人)
看板AndroidDev
标题Re: [问题] Service直接 call Service onDestroy()方
时间Wed Jul 25 14:45:51 2012
※ 引述《s86097john (锅子)》之铭言:
: 这个标题可能有点耸动
: 因为翻过API是说不要直接这样CALL
: 但是心里对於Service的Lifecycle还是有点好奇
: 例如:
: 如果我今天在这边这样写(这边是Service的onDestroy)
: @Override
: public void onDestroy()
: {
: Log.d("MyService", "onDestroy");
: Toast.makeText(this, "跑到onDestroy", Toast.LENGTH_LONG).show();
: super.onDestroy();
: }
: 然後我在在Service另外的地方先後使用
: onDestroy();
: stopSelf();
: 则去log看的结果是两个"onDestroy",也就是说直接使用的onDestroy()是有跑进去的,
: 然後使用stopSelf()的时候因为把整个Service停掉了所以也会跑进去一次(这边的理解
: 可能有问题,希望有人可以指正)。
: 那如果照onDestroy()api的定义:
: Called by the system to notify a Service that it is no longer used
: and is being removed. The service should clean up any resources
: it holds (threads, registered receivers, etc) at this point.
: Upon return, there will be no more calls in to this Service object
: and it is effectively dead. Do not call this method directly.
: 为什麽直接使用onDestroy()後还可以使用stopSelf()呢?(因为东西不是都已经
: 被清空了?)还是说关键在 Called bt the system 这句话?因为好奇既然都有使用
: 为什麽不会发生上述问题?或是我对於继承还有甚麽地方没有搞清楚的呢?
"当一个 service component 要被杀掉的时候系统会 call Service.onDestroy()"
而不是
"call Service.onDestroy() 会把 service component 杀掉"
此外
"call Service.stopSelf() 会把 service component 杀掉"
所以说你的 log 里面会有两个 onDestroy
希望这样有回答到你的问题
顺便提一下
你的 code 里面 call 到的 super.onDestroy() 内容其实是空的
系统 runtime 也不会去检查他有没有被 call 到所以拔掉也没差
--
╔═══╗╔══╮╗╔═══╗╭═══╮╔═══╮
║ ║║ ║║║ ║║ ║║ ║
╚═╗╔╝║ ║║║ ═╣║ ╭╮║║ ═ ║
╔═╝╚╗║ ║ ║║ ╔═╝║ ╰╯║║ ╔╮╯
╚═══╝╚═╰═╝╚═╝ ╰═══╯╚═╝╚╝
--
※ 发信站: 批踢踢实业坊(ptt.cc)
◆ From: 140.112.29.235
※ 编辑: Azarc 来自: 140.112.29.235 (07/25 15:28)
1F:推 s86097john:感谢Az大实在是太威了~ 07/25 21:38