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学习四大组建 目录(?)[+] 什么是服务 服务有 ...
随机推荐
- Sql Server 列转行 Pivot使用
今天正好做 数据展示,用到了列转行,行转列有多种方式,Pivot是其中的一种,Povit 是sql server 2005以后才出现的功能, 下面的业务场景: 每个月,进货渠道的总计数量[Total] ...
- mysql 基本使用
SQL分类 -------------------数据库------------ 创建数据库 create database xxx; 查询所有的数据库 show databases; 查询当前数据 ...
- oracle查看系统资源占用情况
1,连上服务器,使用top命令,可以查看cpu使用率以及内存的使用情况等等,还有当前各用户的使用情况 2,用pl/sql developper,tool里面选sessions,就可以看到当前sessi ...
- JAVA反射系列之Field,java.lang.reflect.Field使用获取方法
JAVA反射系列之Field,java.lang.reflect.Field使用获取方法. 转载https://my.oschina.net/u/1407116/blog/209383 摘要 ja ...
- std::string 字符替换函数
// 替换路径中所有“\”为“/” #include <algorithm> static std::string ConvertSlash(std::string& strUrl ...
- [php]php时间戳当中关于时区的问题
PHP_VERSION = 5.5.11 话说php函数 time() 的起始时间戳是从:GMT 1970-01-01 00:00:00 开始算起的 写了点测试代码: $gmt1 = strtotim ...
- js私有化属性
我们先来看一个例子: var Demo1 = function(val){ this.value = val; this.getValue = function(){ return this.valu ...
- VI 配置文件(略全)
配置 ~/.vimrc文件. root则放到/etc/vimrc 具体详见代码 "====================================================== ...
- 【异常处理】java.lang.NoClassDefFoundError
Exception in thread"main" java.lang.NoClassDefFoundError:org/apache/commons/lang/exception ...
- cocos2d-x -------之笔记篇 环境的安装
cocos2d-x -------之笔记篇 环境的安装 使用到的工具有VS2010 cygwin android-NDK eclipse android SDK 1.首先是android相关环境的安 ...