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 ...
随机推荐
- O - Marriage Match IV - hdu 3416(最短路+最大流)
题目大意:在城市A的男孩想去城市B的女孩,不过他去城市B必须走最短路,并且走过的路不可以再走,问他最多能看这个女孩多少次. 分析:因为这个男孩直走最短路,所以我们必须求出来所有最短路径上的路,怎么 ...
- windows server 2003 64x 读取office数据终极解决办法 The 'Microsoft.Jet.OLEDB.4.0' provider is not registered
微软老子信了你的邪! 试了各种办法没有效果 网友解决办法一: The 'Microsoft.Jet.OLEDB.4.0' provider is not registered on the ...
- SVN和Maven及Jenkins(转)
目前项目组在开发一个项目,由多个子模块构成,构建工具是maven,版本控制工具是svn.本文想对如何结合使用maven和svn提出一点初步的想法 一.只有svn的情况 首先考虑没有maven的情况.这 ...
- ganglia单播配置
背景: 有时,由于当前网络不支持组播等种种原因,使用gmond默认的配置gmetad不能获取到各个客户端的全部数据,http://x.x.x.x/ganglia页面一个cluster组只能展示一 ...
- linux/hpux 添加用户
#添加用户组, 组名dev, 组id为1001groupadd -g 1001 dev #添加用户组dev,不指定组idgroupadd dev #添加用户user1, id为111, 属于dev组, ...
- Android中如何将dp,dip,sp与px相互转化
Android中有很多度量单位:比如常用的dp,dip,sp,px等,有时候需要将他们相互转换,有下面非常方便的方法: 比如sp转换成px: TypedValue.applyDimension(Typ ...
- SKSpriteNode类
继承自 SKNode:UIResponder:NSObject 符合 NSCoding(SKNode)NSCopying(SKNode)NSObject(NSObject) 框架 /System/L ...
- Android SimpleAdapter ListView (锁定手机,解锁手机的列表)
SimpleAdapter是扩展性最好的适配器,可以定义各种你想要的布局. 构造方法: SimpleAdapter(Context context, List<? extends Map< ...
- Ubuntu Server安全Webserver搭建流程
之前整过CentOS.整了Ubuntu才发现,Ubuntu简单多了--不知道性能相比又怎样. 以Ubtuntu 14.04为例.记录一下搭建流程. 一.SSHserver 第一件事当然是ssh,默认安 ...
- [Reduc] React Counter Example
Before you use the React Redux bindings, learn how to create a complete simple application with just ...