Android学习总结(二)——Service基本概念和生命周期
好了,前面我们已经学习了Activity的知识,相信大家也有一定的理解,但是还是不能放松,Android四大组件,我们才学习了一个而已,接下我们继续学习Service。计划总结如下内容:

一.Service的基本概念
service是Android中实现程序后台运行的解决方案,它非常适合执行那些不需要和用户交互而且还要求长期运行的任务。服务的运行不依赖于任何用户界面,即使当程序被切换到后台,或者用户打开了另一个应用程序,服务仍然能够保持正常运行。服务并不是运行在一个独立的进程当中,而是依赖创建服务时所在的应用程序进程。当某一个应用程序进程被杀掉时,所有依赖于该进程的服务也会停止运行。但是我们千万不要被服务端的后台概念所迷住咯,实际上服务并不会自动开启线程,所有的代码都是默认运行在主线程当中的,也就是说,我们需要在服务的内部手动创建子线程,并在这里执行具体的任务,否则就有出现主线程被阻塞住的情况。
二.Service生命周期
我们之前学习过Activity的生命周期,当然Service也有Service的生命周期,前面我们使用了onCreate()、onStart()、onResume()、onPause()、onStop()、onDestroy()和onRestart()方法都是在服务的生命周期内可能回调的方法。
2.1 Service生命周期的图片
图片的来源(http://www.runoob.com/w3cnote/android-tutorial-service-1.html)

三、体验Service生命周期
哈哈哈~说了那么多,我们还是用代码来验证service生命周期吧,代码才是最好的老师,首先自定义一个ServiceTest,重写相关的方法,用户在用Log打印验证,代码如下:
package com.nyl.servicesummary; import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.util.Log; public class ServiceTest extends Service { private static final String TAG = ServiceTest.class.getSimpleName(); //onBind()这个方法是service中唯一的一个抽象方法,所以必须在子类里实现
@Override
public IBinder onBind(Intent intent) {
Log.i(TAG,"onBind方法被调用!");
return null;
} //Service被创建时调用
@Override
public void onCreate() {
Log.i(TAG,"onCreate方法被调用");
super.onCreate();
} //Service被启动时调用
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.i(TAG,"onStartCommand方法被调用");
return super.onStartCommand(intent, flags, startId);
} //服务销毁的时候调用
@Override
public void onDestroy() {
Log.i(TAG,"onDestroy方法被调用");
super.onDestroy();
}
}
需要注意,每一个服务都需要在AndroidManifest.xml文件中注册才能生效,代码如下:
<!--配置Service组件,同时配置一个action-->
<service android:name=".ServiceTest">
<intent-filter>
<action android:name="android.intent.action.RESPOND_VIA_MESSAGE"/>
</intent-filter>
</service>
接着定义两个简单的启动服务器和停止服务器的按钮,在MainActivity的按钮点击事件中分别调用startService()和stopService()这两个方法,代码如下:
package com.nyl.servicesummary; import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button; /**
* 开启服务和停止服务
*/
public class MainActivity extends Activity implements View.OnClickListener { private Button btnStartService;
private Button btnStopService; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); //初始化布局控件
btnStartService = (Button) findViewById(R.id.btnStartService);
btnStopService = (Button) findViewById(R.id.btnStopService); btnStartService.setOnClickListener(this);
btnStopService.setOnClickListener(this);
} @Override
public void onClick(View view) {
if (view.getId() == R.id.btnStartService){
//创建一个Intent对象,然后调用startService()方法来启动ServiceTest这个服务
Intent intent = new Intent(this,ServiceTest.class);
//开启服务
startService(intent);
return;
}
if (view.getId() == R.id.btnStopService){
//创建一个Intent对象,然后调用stopService()方法来停止ServiceTest这个服务
Intent intent = new Intent(this,ServiceTest.class);
//停止服务
stopService(intent);
return;
}
}
}
最后的运行截图
点击开启服务,如下图:

点击停止服务,如下图:

结果分析:
从上面的运行结果我们可以验证我们生命周期图中解释的内容: 我们发现onBind()方法并没有被调用,另外多次点击启动Service,只会重复地调用onCreate和onStartCommand 方法!无论我们启动多少次Service,一个stopService就会停止Service!
Android学习总结(二)——Service基本概念和生命周期的更多相关文章
- Android学习笔记(五)——活动的生命周期
//此系列博文是<第一行Android代码>的学习笔记,如有错漏,欢迎指正! 为了能写出流畅连贯的程序,我们需要了解一下活动的生命周期. 一.返回栈 Android 中的活动是可以层叠的. ...
- android 学习随笔十五(Activity的生命周期与摧毁时返回数据 )
1.Activity的生命周期 onCreate:创建时调用 onStart:在屏幕上可见,但是还没有获得焦点 onResume:可见并且获得焦点 onPause:可见,但是失去焦点 onStop:不 ...
- Android学习笔记(六)Fragment的生命周期
在上一篇博文中对Fragment做了简单的介绍,现在再来探讨一下Fragment的生命周期. 一.Fragment的几种状态: 与Activity类似,Fragment也有一下几种状态: · 活动状态 ...
- 张高兴的 Xamarin.Android 学习笔记:(三)活动生命周期
本文将直接解释我写的一个示例.示例目的在于展示 Android 活动在 Xamarin 中的用法.如果有朋友对基础知识不太了解建议先学 Android . 新建一个 Xamarin.Android 项 ...
- Android学习路线(十四)Activity生命周期——停止和重新启动(Stopping and Restarting)一个Activity
正确地停止和重新启动你的activity在activity的生命周期中是一个非常重要的过程.这样可以确保你的用户感觉到你的应用一直都活着而且没有丢失进度.你的activity的停止和重新启动时有几个重 ...
- Android学习笔记(十) Activity的生命周期
一.如何在一个应用程序中定义多个Activity -定义一个类,继承Activity -复写onCreate() setContentView(R.layout.secondLayout):设定该Ac ...
- android 学习随笔二十二(小结)
ADB进程 * adb指令 * adb install xxx.apk * adb uninstall 包名 * adb devices * adb start-server * adb kill-s ...
- android学习笔记56——Service
Service四大组件之一,需要在AndroidMainfest.xml中添加相关配置,运行于后台,不与用户进行交换,没有UI... 配置时可通过<intent-filter.../>元素 ...
- Android基础_1 四大基本组件介绍与生命周期
Android四大基本组件分别是Activity,Service(服务),Content Provider(内容提供者),BroadcastReceiver(广播接收器). 一.四大基本组件 Acti ...
随机推荐
- bzoj3676
后缀自动机+manacher 听说本质不同的回文串只有O(n)个 那么用manacher求出所有回文串,然后在sam上查找出现了几次就行了 sam的性质又忘了... manacher也忘了... #i ...
- Kafka入门之生产者消费者测试
目录: kafka启动脚本以及关闭脚本 1. 同一个生产者同一个Topic,两个相同的消费者相同的Group 2. 同一个生产者同一个Topic,两个消费者不同Group 3. 两个生产者同一个Top ...
- 标准C++中的string
转自http://www.cnblogs.com/xFreedom/archive/2011/05/16/2048037.html 要想使用标准C++中string类,必须要包含 #include & ...
- 【eclipse插件开发实战】Eclipse插件开发2——SWT
Eclipse插件开发实战2--SWT 一.SWT简介 SWT(StandardWidget Toolkit) 标准小窗口工具箱,一开源的GUI编程框架,与AWT/Swing有相似的用处,eclips ...
- Flutter实战视频-移动电商-42.详细页_UI主页面架构搭建
42.详细页_UI主页面架构搭建 详细分成六大部分拆分开 body里面用FutureBuilder异步加载. FutureBuilder里面的furure属性这里用一个方法,必须返回的也是future ...
- HDU - 4535 ZZULI 1867: 礼上往来【错位排序】
1867: 礼上往来 Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 216 Solved: 65 SubmitStatusWeb Board Desc ...
- LeetCode: 496 Next Greater Element I(easy)
题目: You are given two arrays (without duplicates) nums1 and nums2 where nums1’s elements are subset ...
- 454. 4Sum II
Given four lists A, B, C, D of integer values, compute how many tuples (i, j, k, l) there are such t ...
- POJ3697【BFS】
题意: n个点的完全图,删掉m条边以后,求与1联通的点的个数. 思路: 直接判断 遍历图,n(n+1)/2=5e7 复杂度n^2......,哦,这样也行... //#include<bits/ ...
- hdoj1253
一题简直模板的 BFS,只是三维遍历而已. #include <stdio.h> #include <iostream> #include <sstream> #i ...