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开发那些事儿(二)热补丁
一.热补丁作用:修复导致崩溃的错误.替换/增加方法.替换原来的界面等等 二.实现手段:JSPatch (使用Objective-C Objective-C和JavaScript jspatch桥.你可 ...
- OpenCV学习(1) RGB颜色空间
1.1彩色空间 颜色是外来的光刺激作用于人的视觉器官而产生的主观感觉,它具有色调.饱和度和亮度三个特性.物体的颜色不仅取决于物体本身,还与光源.周围环境的颜色,以及观察者的视觉系统有关 1.1.1颜色 ...
- 加密传输SSL协议2_传统加密
原本到了睡觉的时间,但是做完了SSL的笔记还有GCC和Oracle等的好多的笔记,所以刻苦一点. The Priciple of Encryption/Decryption --conventiona ...
- js创建对象的几种常用方式小结
第一种模式:工厂方式 var lev=function(){ return "666"; }; function Parent(){ var Child = new Object ...
- struts2笔记07-action扩展名
1.action扩展名 默认扩展名可以去/org/apache/struts2/default.properties中查看 struts.action.extension=action,, defau ...
- 转 ——eclipse下进行Python开发 环境配置
python for eclipse插件安装1.下载python for eclipsepython for eclipse下载地址,如:org.python.pydev.feature-1.6.3. ...
- Red Hat Enterprise Linux Release Dates
Red Hat Enterprise Linux Release Dates UpdatedMay 10 2016 at 10:57 PM - English The tables below lis ...
- Oracle EBS-SQL (SYS-20):职责使用菜单2.sql
select frt.responsibility_name, fr.menu_id, fm.menu_name, fr.request_group_i ...
- Oracle EBS-SQL (SYS-17):查询一张报表在哪个职责下面.sql
1.查询该报表的 concurrent_program_id SQL如下: SELECT t.application_id, t.concurrent_program_id, t.* FROM FND ...
- 调bug时候应该提高思维深度(多问二十个为什么)
版权声明:本文为博主原创文章,未经博主允许不得转载. (一)关于思维深度 读书时 有的人做一份卷子有一份卷子的收获 有的人做100张卷子只有一份卷子的收获 写代码时 有的人调一个Bug可以收获多方面的 ...