[Android学习笔记4]四大应用组件之一:Service 上
一、什么是Service
一个Service就是一个能够在后台执行长时操作的应用程序组件,并且不提供用户界面。一个应用程序组件能够启动一个Service,即使用户切换到另一个应用程序,这个Service也会继续在后台运行。另外,一个组件能够绑定一个跟它交互的Service,甚至是进程间通信(IPC)。例如,一个Service可以处理网络事务、播放音乐、执行文件I/O、或者跟CotentProvider交互,所有这些都是在后台完成的。
二、Service的形式
一个Service基本上有两种形式:
1. 被启动(Started):
当应用程序组件(如一个Activity)通过调用startService()方法启动的Service是“被启动(started)”的。Service一旦启动,它就能够无限期的在后台运行,即使启动它的组件被销毁。通常,一个被启动的Service执行一个单一操作,并且不给调用者返回结果。例如,这个Service可能在网络上下载或上传文件。当操作完成的时候,Service应该自己终止。
2. 被绑定(Bound):
当一个应用程序组件通过调用bindService()方法绑定的Service是“被绑定(bound)”的。一个被绑定的Service会提供一个允许组件跟Service交互的客户端接口,用于发送请求、获取结果、甚至是跨进程的进程间通信(IPC)。一个被绑定的Service的运行时间跟绑定它的应用程序组件一样长。多个组件能够绑定一个Service,但是只有所有这些绑定解绑,这个Service才被销毁。
Service能够被启动,也能够被绑定,看程序是否实现了运行组件启动的onStartCommand()方法或者允许绑定的onBind()方法。
三、Service的生命周期
1.以被启动方式产生的Sevice独立于启动该Service的组件的生命周期而存在,直到组件调用stopService()方法或直到其自己调用stopSelf()来终止自己。
2. 如果一个组件调用bindService()方法来创建这个Service(并且不调用onStartCommand()方法),那么这个Service只跟绑定的组件运行同样长的时间。一旦这个从所有的客户端解绑,系统就会销毁它。
四、StartService方式实例
StartService有两种方式:1.继承Service类;2.继承IntentService类;
先看第一种方式,继承Service类。
在清单文件中加入<Service></>标签:
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.luoye.servicelearn.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity> <service android:name=".ServiceShow"></service> </application>
布局文件,声明两个Button,一个用于启动Service,一个用于停止Service
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
> <Button
android:id="@+id/start_service1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="StartService1"
android:onClick="buttonOnClickListener"
/> <Button
android:id="@+id/stop_service1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="StopService1"
android:onClick="buttonOnClickListener"
/> </LinearLayout>
MainActivity.java
package com.luoye.servicelearn; import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.View; public class MainActivity extends Activity { @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
} @Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
} public void buttonOnClickListener(View v)
{
switch(v.getId())
{
case R.id.start_service1:
Intent intent_start = new Intent(this, ServiceShow.class);
startService(intent_start); //启动Service
break;
case R.id.stop_service1:
Intent intent_stop = new Intent(this, ServiceShow.class);
stopService(intent_stop); //停止Service
break;
} } }
ServiceShow.java
package com.luoye.servicelearn; import android.app.Service;
import android.content.Intent;
import android.os.IBinder; public class ServiceShow extends Service{ @Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return null;
} @Override
public void onCreate() { // Service创建时调用,在onStartCommand()之前调用,若Service存在,则不调用
// TODO Auto-generated method stub
super.onCreate();
System.out.println("Service Create");
} @Override
public int onStartCommand(Intent intent, int flags, int startId) { //每次StartService都会调用
// TODO Auto-generated method stub
System.out.println("Service Start Command");
return super.onStartCommand(intent, flags, startId);
} @Override
public void onDestroy() { //stopService()方法调用时,会调用该销毁函数
// TODO Auto-generated method stub
System.out.println("Service Stop");
super.onDestroy();
}
}
效果:
1 点击Start按钮后,查看LogCat信息:
调用了OnCreate()方法和onStartCommand()方法
2 再次点击Start按钮后,查看LogCat信息:
并没有重复调用OnCreate方法,而是调用了onStartCommand方法
3 点击Stop按钮后,查看Logcat信息:
调用了Destroy方法
4 再次点击Start按钮后,查看LogCat信息:
调用了OnCreate()方法和onStartCommand()方法,说明先前的Service已被销毁,此次Service为重新创建。
调用网上达人的一幅图作个总结:
第二种方式:继承IntentService类
IntentService执行以下操作:
IntentService类执行以下操作:
1. 创建一个独立与应用程序主线程的默认工作线程,执行所有的给onStartCommand()方法Intent的处理;
2. 创建一个工作队列,以便每次只给你的onHandleIntent()方法实现传递一个Intent,因此你不必担心多线程的问题;
3. 所有的启动请求都被处理之后终止这个服务,因此你不需要自己去调用stopSelf()方法;
4. 提供返回null的onBind()方法的默认实现;
5. 提供一个给工作队列发送Intent对象的onStartCommand()方法的默认实现和onHandleIntent()方法的实现。
所以这些加起来实际上只需要实现onHandleIntent()方法,来完成由客户提供的工作。当然别忘了构造函数,其中需调用父类构造函数。
实例这里不再赘述,也用一个图作个总结:
[Android学习笔记4]四大应用组件之一:Service 上的更多相关文章
- [Android学习笔记5]四大应用组件之一:Service 下
绑定方式的Service使用 在实现绑定服务时,最重要的是定义onBind()回调方法返回的接口,有三种方式: 1. 继承Binder类 2. 使用Messenger 3. 使用AIDL 这里对1,2 ...
- Android学习笔记(五一):服务Service(上)- IntentService
转自 http://blog.csdn.net/flowingflying/article/details/7616333 对于需要长期运行,例如播放音乐.长期和服务器的连接,即使已不是屏幕当前的ac ...
- Android学习笔记:使用ViewPager组件实现图片切换
在很多App中,尤其是第一次安装启动后,都会出现几个图片进行一些app的介绍和说明,图片可以随着滑动而切换. 我们这里利用 ViewPager组件来演示如何实现这一点. 1.创建一个app工程,默认创 ...
- Android学习笔记 ImageSwitcher图片切换组件的使用
activity_main.xml <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android&qu ...
- Android学习笔记 Toast屏幕提示组件的使用方法
activity_main.xml <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android&qu ...
- Android学习笔记(24):进度条组件ProgressBar及其子类
ProgressBar作为进度条组件使用,它还派生了SeekBar(拖动条)和RatingBar(星级评分条). ProgressBar支持的XML属性: Attribute Name Related ...
- Android学习笔记 TextSwitcher文本切换组件的使用
activity_main.xml <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android&qu ...
- 【转】 Pro Android学习笔记(七六):服务(1):local和remote
文章转载只能用于非商业性质,且不能带有虚拟货币.积分.注册等附加条件.转载须注明出处:http://blog.csdn.net/flowingflying/ Android提供服务,服务是运行在后台的 ...
- Android学习笔记之Activity详解
1 理解Activity Activity就是一个包含应用程序界面的窗口,是Android四大组件之一.一个应用程序可以包含零个或多个Activity.一个Activity的生命周期是指从屏幕上显示那 ...
随机推荐
- 使用C语言编写windows服务一般框架
原文:使用C语言编写windows服务一般框架 编写windows服务和编写windows应用程序一样,有一些回调函数必须填写且向windows 服务管理器(service manager)进行注册, ...
- aspnet-webapi-2-contrib
https://github.com/rdingwall/protobuf-net-data https://github.com/mgravell/protobuf-net https://gith ...
- SSM 使用方法
System Safety Monitor(以下简称为SSM),它是一款俄罗斯出品的系统监控软件,通过监视系统特定的文件(如注册表等)及应用程序,达到保护系统安全的目的.在某些功能上比Winpatro ...
- Windows Azure VM两shut down 道路
今天调查Azure当价格,找到下面的语句,来自http://azure.microsoft.com/en-us/pricing/details/virtual-machines/ * If my de ...
- Cacti+Nagios监控平台完美整合
Cacti+Nagios监控平台完美整合 本博文出自51CTO博客吴光科博主,有任何问题请进入博主页面互动讨论!博文地址:http://wgkgood.blog.51cto.com/1192594/1 ...
- leetcode第32题--Search in Rotated Sorted Array
Suppose a sorted array is rotated at some pivot unknown to you beforehand. (i.e., 0 1 2 4 5 6 7 migh ...
- jquery中的一点工作小记
在做一个页面的时候,网页中由许多块相同的地方 ,例如页面中会有多个地方用到TAB 效果,在使用each()遍历以后 不一定会正常显示,这是因为写的html静态页面中只有当前板块中有这个的类,若是用J ...
- 【转】【Android应用开发详解】第01期:第三方授权认证(一)实现第三方授权登录、分享以及获取用户资料
转载请注明出处:http://blog.csdn.net/yangyu20121224/article/details/9057257 由于公司项目的需要,要实现在项目中使用第三方授权登录以及分享文字 ...
- 【转】Android 网络通信框架Volley简介(Google IO 2013)
Volley主页 https://android.googlesource.com/platform/frameworks/volley http://www.youtube.com/watch?v= ...
- vs2010下载链接中国简体(中国含msdn)
昨天一个朋友说vs2010中国版可下载,我开始不相信.只是周末.所以,我下载一试 果然,安装了中国版,原本msdn订户才能够下载,感谢朋友们上传. 文件名 cn_visual_studio_2010_ ...