Android 之Service
service是运行在后台的服务,你可以启动一个服务Service来播放音乐,或者记录你地理信息位置的改变,或者启动一个服务来运行并一直监听某种动作。
接下来分析一下service 的生命周期:
1:actiivty_main.xml

<RelativeLayout 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"> <Button
android:id="@+id/btn_bind_service"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Bind Service"/> <Button
android:id="@+id/btn_stop_bind"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/btn_bind_service"
android:text="Stop Bind"/> <Button
android:id="@+id/btn_start_service"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/btn_stop_bind"
android:text="Start Service"/> <Button
android:id="@+id/btn_stop_service"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/btn_start_service"
android:text="Stop Service"/>
</RelativeLayout>
2:ServiceDemo.java
public class ServiceDemo extends Service{
private static final String TAG="ServiceDemo";
public static final String ACTION="com.yan.service.ServiceDemo";
@Override
public IBinder onBind(Intent arg0) {
Log.i(TAG, "onBind");
return null;
}
@Override
public void onCreate() {
Log.i(TAG,"onCreate");
super.onCreate();
}
@Override
@Deprecated
public void onStart(Intent intent, int startId) {
Log.i(TAG, "onStart");
super.onStart(intent, startId);
}
@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();
}
@Override
public boolean onUnbind(Intent intent) {
Log.i(TAG, "onUnbind");
return super.onUnbind(intent);
}
}
3:MainActivity.java
public class MainActivity extends Activity {
private Button btnBindService=null;
private Button btnStopBind=null;
private Button btnStartService=null;
private Button btnStopService=null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnBindService=(Button)findViewById(R.id.btn_bind_service);
btnStopBind=(Button)findViewById(R.id.btn_stop_bind);
btnStartService=(Button)findViewById(R.id.btn_start_service);
btnStopService=(Button)findViewById(R.id.btn_stop_service);
btnBindService.setOnClickListener(new OnClickListener(){
public void onClick(View view){
Intent intent=new Intent(ServiceDemo.ACTION);
bindService(intent,sc,BIND_AUTO_CREATE);
}
});
btnStopBind.setOnClickListener(new OnClickListener(){
public void onClick(View view){
unbindService(sc);
}
});
btnStartService.setOnClickListener(new OnClickListener(){
public void onClick(View view){
Intent intent=new Intent(ServiceDemo.ACTION);
startService(intent);
}
});
btnStopService.setOnClickListener(new OnClickListener(){
public void onClick(View view){
Intent intent=new Intent(ServiceDemo.ACTION);
stopService(intent);
}
});
}
ServiceConnection sc=new ServiceConnection(){
@Override
public void onServiceConnected(ComponentName arg0, IBinder arg1) {
Log.i("SC", "onServiceConnected");
}
@Override
public void onServiceDisconnected(ComponentName arg0) {
Log.i("SC", "onServiceDisconnected");
}
};
}
4:AndroidManifest.xml
<service
android:name="com.yan.service.ServiceDemo">
<intent-filter>
<action android:name="com.yan.service.ServiceDemo"/>
</intent-filter>
</service>
5:运行结果分析。
(1)点击StartService:
执行 onCreate()->onStartCommand()->onStart()
(2)点击StopService:
执行 onDestroy()
(3)点击BindService
执行 onCreate()->onBind()
(4)点击StopBind
执行 onUnbind()->onDestory()
Android 之Service的更多相关文章
- Android服务(Service)研究
Service是android四大组件之一,没有用户界面,一直在后台运行. 为什么使用Service启动新线程执行耗时任务,而不直接在Activity中启动一个子线程处理? 1.Activity会被用 ...
- Android中Service 使用详解(LocalService + RemoteService)
Service 简介: Service分为本地服务(LocalService)和远程服务(RemoteService): 1.本地服务依附在主进程上而不是独立的进程,这样在一定程度上节约了资源,另外L ...
- android 入门-Service实时向Activity通过BroadcastReceiver传递数据
引文: http://www.cnblogs.com/linjiqin/p/3147764.html <RelativeLayout xmlns:android="http://sch ...
- android 入门-Service
sdk 1.7 package com.example.hellowrold; import java.util.Random; import com.example.hellowrold.R.id; ...
- Android中Service(服务)详解
http://blog.csdn.net/ryantang03/article/details/7770939 Android中Service(服务)详解 标签: serviceandroidappl ...
- Android中Service的使用
我个人的理解是:我们平时使用的android系统的app的后台应用,就是这个原理 可以利用Service实现程序在后台运行,依照这个原理,可以通过Service来实现关键代码的运行与实现. <一 ...
- 【Android 】Service 全面总结
1.Service的种类 按运行地点分类: 类别 区别 优点 缺点 应用 本地服务(Local) 该服务依附在主进程上, 服务依附在主进程上而不是独立的进程,这样在一定程度上节约了资源,另外L ...
- Android Activity/Service/Broadcaster三大组件之间互相调用
我们研究两个问题,1.Service如何通过Broadcaster更改activity的一个TextView.(研究这个问题,考虑到Service从服务器端获得消息之后,将msg返回给activity ...
- Android 保持Service不被Kill掉的方法--双Service守护 && Android实现双进程守护
本文分为两个部分,第一部分为双Service守护,第二部分为双进程守护 第一部分: 一.Service简介:Java.lang.Object ↳Android.content.Context ↳an ...
- Android 利用Service BroadcastReceiver实现小例子
Activity: package com.example.test; import android.app.Activity; import android.content.Context; imp ...
随机推荐
- 高性能web系统的架构和系统优化
07年毕业一直都在软件公司,14年来到一个互联网公司,给我的感受,区别主要在于: 软件公司需求相对稳定,能够按照计划按部就班的去实施,互联网公司需求相对来说不稳定,上线比较着急,大部分都是小迭代更新, ...
- Java容器详解
线性表,链表,哈希表是常用的数据结构,在进行Java开发时,JDK已经为我们提供了一系列相应的类来实现基本的数据结构.这些类均在java.util包中.在Java中,容器的类型主要有:List.Set ...
- HDU 2485 Destroying the bus stations (IDA*+ BFS)
传送门:http://acm.hdu.edu.cn/showproblem.php?pid=2485 题意:给你n个点,m条相连的边,问你最少去掉几个点使从1到n最小路径>=k,其中不能去掉1, ...
- java中基于TaskEngine类封装实现定时任务
主要包括如下几个类: 文章标题:java中基于TaskEngine类封装实现定时任务 文章地址: http://blog.csdn.net/5iasp/article/details/10950529 ...
- shell 中条件判断
if 中的 -z 到 -d 的意思 2011-09-05 10:30 [ -a FILE ] 如果 FILE 存在则为真. [ -b FILE ] 如果 FILE 存在且是一个块特殊文件则为真. [ ...
- unity3d触屏操作对象运动
using UnityEngine; using System.Collections; public class robot : MonoBehaviour { private GameObject ...
- Meth | ubuntu下安装与卸载软件方法
1.通过deb包安装的情况: 安装.deb包: 代码:sudo dpkg -i package_file.deb反安装.deb包:代码:sudo dpkg -r package_name 2.通过ap ...
- 3_Linux_文件搜索指令
.3文件搜索命令 1)which 查找一个命令所在的路径 whereis 提供命令的帮助文件的信息 whatis 显示命令的概要信息whatis ls which提供命令的别名信息 2)find,基本 ...
- CentOS 7 安装教程
参考资料: http://www.cnblogs.com/bobbylinux/articles/centos7.html
- 在MacOs上配置Hadoop和Spark环境
在MacOs上配置hadoop和spark环境 Setting up Hadoop with Spark on MacOs Instructions 准备环境 如果没有brew,先google怎样安装 ...