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 ...
随机推荐
- Java线程Dump分析工具--jstack【转载】
jstack用于打印出给定的java进程ID或core file或远程调试服务的Java堆栈信息,如果是在64位机器上,需要指定选项"-J-d64",Windows的jstack使 ...
- mount 远程挂载Nfs
服务器:192.168.20.204 客户端:192.168.20.203 1. 在服务器配置/etc/export 添加可以共享的文件夹和允许的客户端地址 /home/dir 192.168.20. ...
- 【Python排序搜索基本算法】之Dijkstra算法
Dijkstra算法和前一篇的Prim算法非常像,区别就在于Dijkstra算法向最短路径树(SPT)中添加顶点的时候,是按照ta与源点的距离顺序进行的.OSPF动态路由协议就是用的Dijkstra算 ...
- solr + tomcat 搭建
1.准备jdk7和tomcat72.拷贝solr目录下example/webapps/solr.war,到tomcat下的webapps目录中.3.启动tomcat74.编辑tomcat7中的weba ...
- 【开源项目】Android 手写记事 App(半成品)
该项目已上传到 CSDN 的 Git 平台中 项目地址:https://code.csdn.net/gd920129/whiteboard GIT SSH:git@code.csdn.net:gd92 ...
- [Javascript] What is JavaScript Function Currying?
Currying is a core concept of functional programming and a useful tool for any developer's toolbelt. ...
- Android中Application类用法
Application类 Application和Activity,Service一样是Android框架的一个系统组件,当Android程序启动时系统会创建一个Application对象,用来存储系 ...
- php开启ssl支持
1.首先在php的安装文件下找到三个文件 并copy到系统目标下的 system32文件夹下: ssleay32.dll.libeay32.dll,php_openssl.dll. 2.打开php.i ...
- TinyXml快速入门(二)
在<TinyXml快速入门(一)>中我介绍了使用TinyXml库如何创建和打印xml文件,下面我介绍使用tinyxml库对xml文件进行一系列的操作,包括获取xml文件声明,查询指定节点. ...
- uploadify上传大文件时出现404错误
出现这个错误的话一般是IIs限制了文件大小.IIS7下的默认设置限制了上传大小.这个时候Web.Config中的大小设置也就失效了.具体步骤:1.打开IIS管理器,找到Default Web Site ...