服务是在后台运行,负责更新内容提供器、发出意图、触发通知,它们是执行持续或定时处理的方式。

多线程一般捆绑服务执行任务,因为在activity中开辟多线程执行任务的话,子线程的生命周期得不到保障,可能在应用进入后台时(进入后台后子线程仍会继续执行),activity被释放时同时被释放,而service在进入后台后仍有优先级存在,所以让子线程捆绑它执行,来保障子线程能执行完全。而在servcie的onStartCommand中,不会将一个在主线程运行且消耗长时间的任务放在onStartCommand(虽然进入后台后onStartCommand中的主线程代码仍会继续执行),而子线程的开辟一般就放到onStartCommand。这样onStartCommand就可以快速完成,并返回一个重启行为标识:

START_STICKY:如果service进程被kill掉,保留service的状态为开始状态,但不保留递送的intent对象。随后系统会尝试重新创建service,由于服务状态为开始状态,所以创建服务后一定会调用onStartCommand(Intent,int,int)方法。如果在此期间没有任何启动命令被传递到service,那么参数Intent将为null。
START_NOT_STICKY:“非粘性的”。使用这个返回值时,如果在执行完onStartCommand后,服务被异常kill掉,系统不会自动重启该服务。
START_REDELIVER_INTENT:重传Intent。使用这个返回值时,如果在执行完onStartCommand后,服务被异常kill掉,系统会自动重启该服务,并将Intent的值传入。
START_STICKY_COMPATIBILITY:START_STICKY的兼容版本,但不保证服务被kill后一定能重启。

创建服务

public class MyService extends Service {

    private final IBinder binder = new MyBinder();

    public class MyBinder extends Binder {
MyService getService() {
return MyService.this;
}
} @Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return null;
} @Override
public void onCreate() {
// TODO Auto-generated method stub
super.onCreate();
} @Override
public int onStartCommand(Intent intent, int flags, int startId) { //启动一个后台线程来进行处理 if ((flags & START_FLAG_RETRY) == 0) { } else { } // return super.onStartCommand(intent, flags, startId);
return Service.START_STICKY;
}
}

绑定服务

public class MainActivity extends ActionBarActivity {

    @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); startService(new Intent(this, MyService.class)); //绑定服务,这样就可以获取服务对象,从而可以使用服务对象的所有公有方法和属性(应用外的交互可以通过广播意图,或意图中启动服务调度extras Bundle,而AIDL是另一种更紧密的连接方式)
Intent bindIntent = new Intent(MainActivity.this, MyService.class);
bindService(bindIntent, mConnection, Context.BIND_AUTO_CREATE);
} private MyService serviceBinder; private ServiceConnection mConnection = new ServiceConnection() {
//处理服务和活动之间的链接
@Override
public void onServiceDisconnected(ComponentName name) {
//当服务以外对开时调用
serviceBinder = null;
} @Override
public void onServiceConnected(ComponentName name, IBinder service) {
//当建立连接时调用
serviceBinder = ((MyService.MyBinder)service).getService();
}
};
}

服务移到前台

public class MyService extends Service {

    public void moveServiceToForeground() {
int NOTIFICATION_ID = 1; Intent intent = new Intent(this, MainActivity.class);
PendingIntent pi = PendingIntent.getActivity(this, 1, intent, 0);
Notification notification = new Notification(R.drawable.ic_launcher, "Running in the Foreground", System.currentTimeMillis());
notification.setLatestEventInfo(this, "title", "Text", pi); notification.flags = notification.flags | Notification.FLAG_ONGOING_EVENT;
startForeground(NOTIFICATION_ID, notification);
}
}

android-服务Service的更多相关文章

  1. Android服务Service具体解释(作用,生命周期,AIDL)系列文章-为什么须要服务呢?

    Android服务Service具体解释(作用,生命周期,AIDL) 近期沉迷于上班,没有时间写博客了.解衣入睡,未眠.随起床写一篇博客压压惊! ##我们android系统为什么须要服务Service ...

  2. Android服务(Service)研究

    Service是android四大组件之一,没有用户界面,一直在后台运行. 为什么使用Service启动新线程执行耗时任务,而不直接在Activity中启动一个子线程处理? 1.Activity会被用 ...

  3. Android服务Service总结

    转自 http://blog.csdn.net/liuhe688/article/details/6874378 富貴必從勤苦得,男兒須讀五車書.唐.杜甫<柏學士茅屋> 作为程序员的我们, ...

  4. android服务Service(上)- IntentService

    Android学习笔记(五一):服务Service(上)- IntentService 对于需要长期运行,例如播放音乐.长期和服务器的连接,即使已不是屏幕当前的activity仍需要运行的情况,采用服 ...

  5. Android服务——Service

    服务 Service 是一个可以在后台执行长时间运行操作而不使用用户界面的应用组件.服务可由其他应用组件启动,而且即使用户切换到其他应用,服务仍将在后台继续运行. 此外,组件可以绑定到服务,以与之进行 ...

  6. Android服务Service

    安卓Service服务 一    Service简介 Service是运行在后台的,没有界面的,用来处理耗时比较长的.Service不是一个单独的进程,也不是一个单独的线程. Service有两种类型 ...

  7. android 服务service开启和关闭

    startService()方法开启一个服务. 服务只会开启一次,如果服务已经创建,并且没有销毁,多次调用startService方法只会执行onStartCommand方法和onStart方法. 服 ...

  8. Android中Service(服务)详解

    http://blog.csdn.net/ryantang03/article/details/7770939 Android中Service(服务)详解 标签: serviceandroidappl ...

  9. Android服务之Service(其一)

    android中服务是运行在后台的东西,级别与activity差不多.既然说service是运行在后台的服务,那么它就是不可见的,没有界面的东西.你可以启动一个服务Service来播放音乐,或者记录你 ...

  10. Android 服务类Service 的详细学习

    http://blog.csdn.net/vipzjyno1/article/details/26004831 Android服务类Service学习四大组建   目录(?)[+] 什么是服务 服务有 ...

随机推荐

  1. CentOS6.3下搭建vsftpd(采用虚拟用户设置)

    CentOS6.3如果在安装的时候所有安装选项都打勾的话就含有单间vsftpd必备的软件:vsftpd.pam*.db4* 检查是否安装: [root@centos6 ~]# rpm -qa | gr ...

  2. Android开发问题汇总(持续更新)

    在Android开发中,总会有一些很小的问题.由于我们的不仔细,很容易忽略掉,从而导致在该问题上花费了很多的时间,造成工作进度的延迟. 为此,在这里做一下记录,避免再次浪费许多时间在这些问题上. 1. ...

  3. js——DOM操作(二)

    表格属性: tHead:表格头 tBodies:表格正文 tFoot:表格尾 rows:行 cells:列 表单操作: <form id="form1"> <in ...

  4. poj 1149 pigs ---- 最大流

    题意以及分析:http://ycool.com/post/zhhrrm6#rule3 主要是建图,简化图,然后在套最大流的模板. #include <iostream> #include& ...

  5. JVM学习之JVM1.6 GC详解

    转自:http://www.cnblogs.com/ggjucheng/p/3977384.html,多谢分享 前言  JVM GC是JVM的内存回收算法,调整JVM GC(Garbage Colle ...

  6. mysql 索引创建规则

    1.表的主键.外键必须有索引:2.数据量超过300的表应该有索引: 3.经常与其他表进行连接的表,在连接字段上应该建立索引: 4.经常出现在Where子句中的字段,特别是大表的字段,应该建立索引: 5 ...

  7. php数组分页类

    <?php class ArrayPage{ public $totalPage;//全部页数 public $lists;//每页显示数目 public $arr = array();//分页 ...

  8. 用“U盘”重新安装(MSDN)原版Windows XP sp3操作系统(图文)

    安装微软(MSDN)原版Windows XP sp3系统的方法不少,可以说是很多,但是我就用“U盘”安装.用“U盘”装XP系统也不是什么稀罕事,不会的,就按照下面我常用的“U盘”装原版Windows ...

  9. c++多线程编程之互斥对象(锁)的使用之----死锁

    一.死锁会在什么情况发生 1.假设有如下代码 mutex;   //代表一个全局互斥对象 void  A() { mutex.lock(); //这里操作共享数据 B();  //这里调用B方法 mu ...

  10. poj1552---枚举

    #include <stdio.h> #include <stdlib.h> int main() { ],th=,i,j; while(scanf("%d" ...