Android--Service生命周期(一)

来源:互联网 发布:sql修改字段名 编辑:程序博客网 时间:2024/05/14 23:03
package com.service.lifecycle.demo;import android.app.Activity;import android.content.Intent;import android.os.Bundle;import android.util.Log;import android.view.View;import android.widget.Button;public class ServiceLifeCycleActivity extends Activity {/** Called when the activity is first created. */@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.main);Log.d(AppConstant.TAG, "FirstActivity-->onCreate()");Button startServiceBtn = (Button) findViewById(R.id.startServiceBtn);Button endServiceBtn = (Button) findViewById(R.id.endServiceBtn);startServiceBtn.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {Intent intent = new Intent(ServiceLifeCycleActivity.this, ServiceDemo.class);ServiceLifeCycleActivity.this.startService(intent);}});endServiceBtn.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {Intent intent = new Intent(ServiceLifeCycleActivity.this, ServiceDemo.class);ServiceLifeCycleActivity.this.stopService(intent);}});}@Overrideprotected void onStart() {super.onStart();Log.d(AppConstant.TAG, "FirstActivity-->onStart()");}@Overrideprotected void onRestart() {super.onRestart();Log.d(AppConstant.TAG, "FirstActivity-->onRestart()");}@Overrideprotected void onResume() {super.onResume();Log.d(AppConstant.TAG, "FirstActivity-->onResume()");}@Overrideprotected void onPause() {super.onPause();Log.d(AppConstant.TAG, "FirstActivity-->onPause()");}@Overrideprotected void onStop() {super.onStop();Log.d(AppConstant.TAG, "FirstActivity-->onStop()");}@Overrideprotected void onDestroy() {super.onDestroy();Log.d(AppConstant.TAG, "FirstActivity-->onDestroy()");}}

package com.service.lifecycle.demo;import android.app.Service;import android.content.Intent;import android.os.IBinder;import android.util.Log;public class ServiceDemo extends Service {@Overridepublic void onCreate() {super.onCreate();Log.d(AppConstant.TAG, "ServiceDemo-->onCreate()");}@Overridepublic IBinder onBind(Intent arg0) {Log.d(AppConstant.TAG, "ServiceDemo-->onBind()");return null;}@Overridepublic int onStartCommand(Intent intent, int flags, int startId) {Log.d(AppConstant.TAG, "ServiceDemo-->onStartCommand()");return super.onStartCommand(intent, flags, startId);}@Overridepublic void onDestroy() {super.onDestroy();Log.d(AppConstant.TAG, "ServiceDemo-->onDestroy()");}}

本次采用Context.startService()和Context.stopService()方法启和结束Service。点击“启动Service”按钮,运行ServiceDemo中的onCreate(),onStartCommand()方法,再次点击“启动Service”按钮,会运行onStartCommand(),方法。点击“结束Service”按钮,会执行ServiceDemo中的onDestroy()方法。

修改ServiceDemo中的onStartCommand()方法中的代码如下:

public int onStartCommand(Intent intent, int flags, int startId) {Log.d(AppConstant.TAG, "ServiceDemo-->onStartCommand()");try{Thread.sleep(5000);} catch (Exception e) {Log.d(AppConstant.TAG, "ServiceDemo-->onStartCommand()中Thread.sleep出现异常");} finally {this.stopSelf();Log.d(AppConstant.TAG, "ServiceDemo-->onStartCommand()中执行了stopSelf()");}return super.onStartCommand(intent, flags, startId);}

运行如图

可见,在Service中调用stopSelf()方法,同样可以结束掉本Service。



原创粉丝点击