Service的开启和停止以及生命周期
1、清单文件
<service android:name=".TestService"></service>
2、开启Service
Intent intent = new Intent(this, TestService.class);
startService(intent);
3、停止服务
Intent intent = new Intent(this, TestService.class);
stopService(intent);
4、Service的开启和停止所显示的生命周期
package com.example.testservice; import android.app.Service;
import android.content.Intent;
import android.os.IBinder; public class TestService extends Service { @Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return null;
} @Override
public void onCreate() {
// TODO Auto-generated method stub--1
System.out.println("onCreate");
super.onCreate(); } @Override
@Deprecated
public void onStart(Intent intent, int startId) {
// TODO Auto-generated method stub--3
System.out.println("onStart");
super.onStart(intent, startId); } @Override
public int onStartCommand(Intent intent, int flags, int startId) {
// TODO Auto-generated method stub--2
System.out.println("onStartCommand");
return super.onStartCommand(intent, flags, startId); } @Override
public void onDestroy() {
// TODO Auto-generated method stub--4
System.out.println("onDestroy");
super.onDestroy();
} }
注意:--n显示是它们的执行顺序
Service的开启和停止以及生命周期的更多相关文章
- Service的两种用法及其生命周期
先来一点基础知识: Service 是android的四大组件之一,与Activity同属于一个级别,它是运行在后台进行服务的组件(例如在后台播放的音乐,播放音乐的同时并不影响其他操作).Servic ...
- Android服务Service具体解释(作用,生命周期,AIDL)系列文章-为什么须要服务呢?
Android服务Service具体解释(作用,生命周期,AIDL) 近期沉迷于上班,没有时间写博客了.解衣入睡,未眠.随起床写一篇博客压压惊! ##我们android系统为什么须要服务Service ...
- Android -- Service的开启关闭与生命周期
Service是Android 系统中的四大组件之一,是在一段不定的时间运行在后台,不和用户交互应用组件. service可以在很多场合的应用中使用,比如播放多媒体的时候用户启动了其他Activity ...
- Android activity和service的生命周期对比
1Activity生命周期 七个方法 1. void onCreate(Bundle savedInstanceState) 当Activity被第首次加载时执行.我们新启动一个程序的时候其主窗体的o ...
- service的生命周期
Managing the Lifecycle of a Service service的生命周期,从它被创建开始,到它被销毁为止,可以有两条不同的路径: A started service 被开启的s ...
- android基础---->service的生命周期
服务是一个应用程序组件代表应用程序执行一个长时间操作的行为,虽然不与用户交互或供应功能供其它应用程序使用.它和其他的应用对象一样,在他的宿主进程的主线程中运行.今天我们开始android中普通serv ...
- Activity和Service的生命周期(图)
1.Activity的生命周期 情形一.一个单独的Activity的正常的生命过程是这样的:onCreate->onStart->onPause->onStop->onDest ...
- Android 中Service生命周期
使用context.startService() 启动Service 其生命周期为context.startService() ->onCreate()- >onStart()->S ...
- 8.1.1 Service的生命周期
2010-06-21 16:57 李宁 中国水利水电出版社 字号:T | T <Android/OPhone开发完全讲义>第8章Android服务,本章主要介绍了Android系统 中的服 ...
随机推荐
- C函数调用与栈--代码真相
前面详细的说了,C函数调用的过程中,栈的变化情况的原理部分,这里在看一下汇编代码的真正的实现. 有关前面的那一片博客,主要记住的就是函数调用时栈的变化,4+3+2的步骤: (1)设置栈帧边界 (2)开 ...
- Python之路:Python简介
Python前世今生 python的创始人为吉多·范罗苏姆(Guido van Rossum).1989年的圣诞节期间他为了在阿姆斯特丹打发时间,决心开发一个新的脚本解释程序,作为ABC语言的一种继承 ...
- java和C#之间SOCKET通信的问题
转自:http://www.cdtarena.com/javapx/201307/9170.html java和C#之间SOCKET通信的问题 一.服务器端(使用java编写) /** * 监听客户端 ...
- sun.proxy.$Proxy0 cannot be cast to dao.impl.MyAdviceImpl
ApplicationContext ac=new ClassPathXmlApplicationContext("applicationContext.xml"); ...
- IT第二十一天 - Collections、ArrayList集合、LinkedList集合、Set集合、HashMap集合、集合的操作注意【修20130828】
NIIT第二十一天 上午 集合 1. 集合Collection存储数据的形式是单个存储的,而Map存储是按照键值对来存储的,键值对:即键+值同时存储的,类似align="center&quo ...
- EntityFramework经典的left join语法
/* * 常常看到有人问linq语法怎样写left join的查询语句,但网上找到的都是简单的两表连接.參考意义有限. * 今天最终项目里要用到复杂的多表连接,同一时候含有多个左连接, * 恰好 ...
- 你会用swift创建复杂的加载动画吗(1)
时至今日,iOS 应用商店已经拥有超过了140万 应用,让你自己的应用脱颖而出确实是个不小的挑战.不过,在你的应用掉入默默无闻的大黑洞之前,你拥有一个小小的机遇窗,它能帮你吸引用户的注意. AD: 时 ...
- 编写存储过程导出oracle表数据到多个文本文件
1.测试表和数据: create table test(id )); begin .. loop insert into test values(k,'test'||k); end loop; end ...
- pojg487-3279电话号码转换(字符映射)
http://poj.grids.cn/practice/2974 注意输入中连字符可以任意添加和删除. 描述企业喜欢用容易被记住的电话号码.让电话号码容易被记住的一个办法是将它写成一个容易记住的单词 ...
- 说服式设计(persuasive design)的行为模型
转自:http://www.sharetk.com/html/ued/User-Research/1404.html 一 模型简介 BJ Fogg提出了一个新的理解人类行为的模型,他称之为Fogg b ...