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。
- Android--Service生命周期(一)
- Android的服务(Service)(一)生命周期
- Android的服务(Service)(一)生命周期
- Android的服务(Service)(一)生命周期
- Service的生命周期(一)
- Android--Service生命周期(二)
- 四大组件---Service的生命周期(一)
- Service简介与生命周期讲解(一)
- Android 中Service简单理解(生命周期)
- Android Service生命周期(api使用介绍)
- android service 生命周期
- 【Android】Service 生命周期
- android service生命周期
- android service生命周期
- Android 中Service生命周期
- Android Service生命周期
- android service生命周期
- android service 生命周期
- C语言中可变参数的用法
- 开发报表月连续出月份
- 正则表达式简介
- c/c++ extern的使用
- lstrcpy function
- Android--Service生命周期(一)
- SQL优化学习(一)
- INSERT INTO 创建新表
- 恶搞西游
- Top 10 jQuery Mobile Code Snippets that you need to know
- awk命令详解
- C#:StringBuilder和Char[]作为参数类型在平台调用中的差异
- 工作上的几点建议
- on line 44 Warning: session_write_close() [function.session-write-close]:..\index.php on line 44