android-服务Service
服务是在后台运行,负责更新内容提供器、发出意图、触发通知,它们是执行持续或定时处理的方式。
多线程一般捆绑服务执行任务,因为在activity中开辟多线程执行任务的话,子线程的生命周期得不到保障,可能在应用进入后台时(进入后台后子线程仍会继续执行),activity被释放时同时被释放,而service在进入后台后仍有优先级存在,所以让子线程捆绑它执行,来保障子线程能执行完全。而在servcie的onStartCommand中,不会将一个在主线程运行且消耗长时间的任务放在onStartCommand(虽然进入后台后onStartCommand中的主线程代码仍会继续执行),而子线程的开辟一般就放到onStartCommand。这样onStartCommand就可以快速完成,并返回一个重启行为标识:
创建服务
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的更多相关文章
- Android服务Service具体解释(作用,生命周期,AIDL)系列文章-为什么须要服务呢?
Android服务Service具体解释(作用,生命周期,AIDL) 近期沉迷于上班,没有时间写博客了.解衣入睡,未眠.随起床写一篇博客压压惊! ##我们android系统为什么须要服务Service ...
- Android服务(Service)研究
Service是android四大组件之一,没有用户界面,一直在后台运行. 为什么使用Service启动新线程执行耗时任务,而不直接在Activity中启动一个子线程处理? 1.Activity会被用 ...
- Android服务Service总结
转自 http://blog.csdn.net/liuhe688/article/details/6874378 富貴必從勤苦得,男兒須讀五車書.唐.杜甫<柏學士茅屋> 作为程序员的我们, ...
- android服务Service(上)- IntentService
Android学习笔记(五一):服务Service(上)- IntentService 对于需要长期运行,例如播放音乐.长期和服务器的连接,即使已不是屏幕当前的activity仍需要运行的情况,采用服 ...
- Android服务——Service
服务 Service 是一个可以在后台执行长时间运行操作而不使用用户界面的应用组件.服务可由其他应用组件启动,而且即使用户切换到其他应用,服务仍将在后台继续运行. 此外,组件可以绑定到服务,以与之进行 ...
- Android服务Service
安卓Service服务 一 Service简介 Service是运行在后台的,没有界面的,用来处理耗时比较长的.Service不是一个单独的进程,也不是一个单独的线程. Service有两种类型 ...
- android 服务service开启和关闭
startService()方法开启一个服务. 服务只会开启一次,如果服务已经创建,并且没有销毁,多次调用startService方法只会执行onStartCommand方法和onStart方法. 服 ...
- Android中Service(服务)详解
http://blog.csdn.net/ryantang03/article/details/7770939 Android中Service(服务)详解 标签: serviceandroidappl ...
- Android服务之Service(其一)
android中服务是运行在后台的东西,级别与activity差不多.既然说service是运行在后台的服务,那么它就是不可见的,没有界面的东西.你可以启动一个服务Service来播放音乐,或者记录你 ...
- Android 服务类Service 的详细学习
http://blog.csdn.net/vipzjyno1/article/details/26004831 Android服务类Service学习四大组建 目录(?)[+] 什么是服务 服务有 ...
随机推荐
- iOS常用的封装方法
做开发也有一段时间了,看了好多大神的代码,总体感觉他们写的代码简洁,好看,然而在对比下我写的代码,混乱,无序,简直不堪入目啊! 总体来说大神们的代码封装的都比较好,对一个项目要重复用到的代码他们都会封 ...
- React和Angular
React和Angular 你若装逼,请带我飞! 从前,从前,听说React只负责UI,话说写Angular代码就像写后端,现在看来,React赢在情怀上了: 我认为没必要老是拿React和Angul ...
- Xampp Linux应用
一.基本操作: 1.Xampp安装包下载: https://www.apachefriends.org/index.html 2.安装与配置: 将xampp-linux-x64-5.6.3 ...
- Linux(CentOS6.5) 开放端口,配置防火墙
打开配置文件 [root@localhost ~]# vi /etc/sysconfig/iptables 正确的配置文件 # Firewall configuration written by sy ...
- android select选择器 checkbox改外观,button按下状态
android 可以用选择器.来加载视图.选择器里的选项也很多针对实际使用中用的几个进行描述. 1.button 的按下弹起改外观.选择器属性用 android:state_pressed 2.C ...
- Unix/Linux环境C编程入门教程(17) Gentoo LinuxCCPP开发环境搭建
1. Gentoo Linux是一套通用的.快捷的.完全免费的Linux发行,它面向开发人员和网络职业人员.与其他发行不同的是,Gentoo Linux拥有一套先进的包管理系统叫作Portage.在B ...
- 限制div高度当内容多了溢出时显示滚动条
<!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type"content= ...
- Oracle 11g新特性虚拟列分区
如今有个需求:一个单据表要依照月份来分区.假设是在Oracle 10g上,仅仅能再加一个字段. 在Oracle 11g以后就不一样了.能够用虚拟列处理. SQL> select * from v ...
- android 内存优化
OOM 内存泄漏引起很多问题: 1:节目卡顿.反应慢(高内存使用情况JVM 虚拟机的频繁离职GC) 2:消失 3:直接崩溃 ANDROID 内存面临的问题 1: 有限的堆内存,原始仅仅有16M 2:内 ...
- ovs 实用案例
建立gre,xvlan:http://networkstatic.net/configuring-vxlan-and-gre-tunnels-on-openvswitch/ vm之间通过gre通信:h ...