作者s86097john (锅子)
看板AndroidDev
标题[问题] Service直接 call Service onDestroy()方
时间Tue Jul 24 23:44:01 2012
这个标题可能有点耸动
因为翻过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 这句话?因为好奇既然都有使用
为什麽不会发生上述问题?或是我对於继承还有甚麽地方没有搞清楚的呢?
--
※ 发信站: 批踢踢实业坊(ptt.cc)
◆ From: 1.175.139.101
※ 编辑: s86097john 来自: 1.175.139.101 (07/25 00:14)