android的service的研究

来源:互联网 发布:酷家乐软件 编辑:程序博客网 时间:2024/05/20 23:06

当在手机设置中主动停掉service时,该service的onDestroy函数会不会被调用?我做了以下实验。

定义MyService类,并在manifest里边做声明。MyService类的定义如下:

public class MyService extends Service {@Overridepublic void onCreate() {super.onCreate();Log.d("MyService", "onCreate");}@Overridepublic void onDestroy() {super.onDestroy();Log.d("MyService", "onDestroy");}@Overridepublic IBinder onBind(Intent arg0) {// TODO Auto-generated method stubreturn null;}}


 

然后添加代码
        Intent intent = new Intent(this, MyService.class);        startService(intent);

启动程序,Logcat显示:

09-13 09:24:52.517: D/MyService(852): onCreate

在设置的正在运行的服务中关闭MyService,Logcat 显示:

09-13 09:27:27.576: D/MyService(852): onDestroy


实验证明,当在手机设置中主动停掉service时,该service的onDestroy函数会被调用。