`
bcyy
  • 浏览: 1823678 次
文章分类
社区版块
存档分类
最新评论

android service 服务学习笔记

 
阅读更多

1 Service 概述

Service 是没有用户界面,但它是在后台一只一直运行的,每个Service都继承类Service。与Activitvy及其他组件一样,Service同样运行在应用程序主线程中,所以不能阻塞其他进程的运行,通常需用对服务创建一个线程执行耗时的服务

2 Service生命周期

使用Service有两种方式:

一是使用Content上的StartService()的方法来启动一个线程;

另一种是使用bindservice()方法来绑定并启动一个线程。

和Activity相比 ,Service的生命周期会简单很多,使用stopservice()来关闭服务:其生命周期中相关的方法有以下三个:

public void onCreate() {
		// TODO Auto-generated method stub
		super.onCreate();


	public void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();


public void onStart(Intent intent, int startId) {
		// TODO Auto-generated method stub
		super.onStart(intent, startId);
	}

同样和Activity一样来验证Service的启动和关闭服务的回调函数的调用情况,这里就不再验证了!比较简单!

在启动服务是首先调用onCreate(),再调用onStart(),服务开启;stopservice()时,调用ondestory()完成服务的关闭;

下面是简单的例子:

具体的代码如下:
main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
    <Button 
    android:id="@+id/button1" 
    android:textColor="#ff0000"
    android:layout_height="wrap_content" 
    android:text="启动服务" 
    android:onClick="start"
    android:layout_width="wrap_content"></Button>
    <Button 
    android:text="停止服务" 
    android:onClick="stop"
    android:textColor="#00ff00"
    android:id="@+id/button2" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content"></Button>
</LinearLayout>

ServiceActivity.java
public class ServiceActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }
    public void start(View view){
    	Intent intent = new Intent(this,service.class);
    	startService(intent);//开启服务
    }
    public void stop(View view){
    	Intent intent = new Intent(this,service.class);
    	startService(intent);//关闭服务
    }
}

Service。java
/*在ononCreate()创建一个线程,开启线程
 * 线程每隔一秒打印数值,可以再LOGCAT中看到
 */
public class service extends Service{
	Boolean isRuning = true;
	@Override
	public IBinder onBind(Intent intent) {
		//默认的都会有的不用管,可以看到返回值为null的
		return null;
	}

	@Override
	public void onCreate() {
		Thread thread = new Thread(new mythread());
		thread.start();
//		thread.stop();目前是不建议使用的,使用可能会产生意想不到的后果
//		现在使用标志位isRuning控制
		super.onCreate();
	}	
	@Override
	public void onDestroy() {
		// TODO Auto-generated method stub	
		isRuning =false;
		super.onDestroy();
	}

	class mythread implements Runnable{

		@Override
		public void run() {
			// TODO Auto-generated method stub
			int i = 0;
			while(isRuning){
				System.out.println(i++);
				try {
					Thread.sleep(1000);//延迟
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}		
		}	
	}
}

注意:必须在AndroidManifest.xml 注册服务<service android:name=".service"></service>;


分享到:
评论

相关推荐

    android service 学习笔记(上)

    android service 学习笔记(上、下)android service 学习笔记(上、下)

    android service 学习笔记(下)

    android service 学习笔记(上、下)

    Android开发学习笔记(整理)

    Android开发学习笔记(整理),整理论坛android学习笔记,较完整的讲解了android的内容。包括:view、activity、service、intent、广播机制、http连接、数据sqllite存储、后台线程、各种layout、偏好、本地文件操作、...

    Pro Android学习:Android service小例子

    这是Pro Android学习系列中Android Service部分的例子源代码。相关学习笔记见:http://blog.csdn.net/flowingflying/article/details/6212512

    Android学习笔记之Service

    自己学习安卓的时候的一些学习笔记,很详细奥,代码和解说都有。

    Android学习笔记-service[参考].pdf

    Android学习笔记-service[参考].pdf

    Android学习笔记.pdf

    包括环境搭建教程、创建模拟器、新建helloworld程序、电话拨号器、软件部署到模拟器、短信发送器、线性布局、相对布局、日志、activity、Android进程线程及优先级、Android UI线程阻塞及优化、广播接受者、Service...

    android学习笔记(html完整版)目录

    目录,整理论坛android学习笔记,较完整的讲解了android的内容。包括:view、activity、service、intent、广播机制、http连接、数据sqllite存储、后台线程、各种layout、偏好、本地文件操作、apdapter等几乎全部内容...

    android学习笔记(html完整版)

    整理论坛android学习笔记,较完整的讲解了android的内容。包括:view、activity、service、intent、广播机制、http连接、数据sqllite存储、后台线程、各种layout、偏好、本地文件操作、apdapter等几乎全部内容。非常...

    ANDROID 学习笔记 中文版

    ANDROID 学习笔记 中文版 来自ACC:http://androidos.cc/dev 内容比较适合初学者,不过也适合中级开发者,从HelloWorld讲述到Service,还是比较经典的笔记

    xamarin学习笔记A18(安卓Service)

    Service简介,服务就是没有任何界面,不需要和用户直接打交道且需要长时间在后台运行的程序。例如手机中的音乐播放服务、天气更新服务和下载服务等。 服务运行在主线程中的,为了不让主线程阻塞,所以要在服务内部...

    Android学习笔记

    Android学习笔记,主要讲解了intent,service,Activity,生命周期以及几个项目的实例

    ZQYServiceDemo:Android Service学习笔记

    ZQYServiceDemo

    Android学习笔记之Started Service

    Started Service就是启动之后可以在后台无限期的运行,比如通过Activity组件的startService方法启动之后,如果不用指定方法关闭,这个Service就会一直运行。 下面说一下怎么来实现这种Service: (在主界面设置两个...

    Pro Android学习:了解Handler小例子

    这是Pro Android学习系列中Android Service部分的例子源代码。相关学习笔记见:http://blog.csdn.net/flowingflying/article/details/6212512

    java8集合源码分析-AndroidLearn:Android学习笔记

    Android UI 生命周期 启动模式 ActivityManagerService PMS Art/Dalvik Http Http WebSocket Udp Kotlin 算法 快速排序 链表 重新排版 RxJava2 Glide GreenDao 其他 反编译 Hook 动态加载 编译构建 设计模式 代理...

    Android学习笔记–Activity中使用Intent传值示例代码

    Intent,又称为意图,是一种运行时绑定机制,它能在程序运行的过程中链接两个不同的组件(Activity、Service、BroadcastReceiver)。通过Intent,程序可以向Android表达某种请求或意愿,Android会根据意愿的内容选择...

Global site tag (gtag.js) - Google Analytics