android基础(四)service
Intent service =new Intent(this,ServiceClass.class);
bindService(Intent service,ServiceConnection conn, int flags);
ServiceConnection conn=new ServiceConnection(){
//service与Activity建立连接后调用的两个方法,用于使用service中的服务
@Override
public void onServiceDisconnected(ComponentName arg0) {
// TODO Auto-generated method stub
} @Override
public void onServiceConnected(ComponentName arg0, IBinder arg1) { //arg1为OnBind()方法中返回的Binder对象, 可通过该对象获得service对象本身(在Binder类中有一个getService()方法用于返回service对象本身)
MyBinder binder=(MyBinder)arg1; //调用getService()方法获得service对象本身
BindService bindservice=binder.getService(); //接下来便可调用service中的方法来实现一定功能
String result=bindservice.service();
}
};
class BindService extends Service { // 实现onBind()方法返回一个Binder对象
public IBinder onBind(Intent arg0){ return mybinder; } //内部类Mybinder
class MyBinder extends Binder{ public BindService getService(){ return BindeService.this;
}
} //BindService中的方法,将使用binder.getService().service()调用
public String service(){
// 方法实现
}
}
3、IntentService
由于Service中的代码都是运行在主线程中的,如果在Service中处理一些耗时操作,会容易出现ANR的情况。此时需要在Service中开启一个子线程来处理耗时操作。但是会出现忘记开启线程或线程中操作执行完成后忘记停止服务的情况。 所以Android系统提供了一种更为简便的处理Service中ANR情况的方式——使用IntentService。
IntentService有以下特点:
(1) 它创建了一个独立的工作线程来处理所有的通过onStartCommand()传递给服务的intents。
(2) 创建了一个工作队列,来逐个发送intent给onHandleIntent()。
(3) 不需要主动调用stopSelft()来结束服务。因为,在所有的intent被处理完后,系统会自动关闭服务。
(4) 默认实现的onBind()返回null
(5) 默认实现的onStartCommand()的目的是将intent插入到工作队列中
继承IntentService的类至少要实现两个函数:构造函数和onHandleIntent(Intent intent)函数。要覆盖IntentService的其它函数时,注意要通过super调用父类的对应的函数。
onHandleIntent(Intent intent)中的Intent参数是startService(Intent intent)/bindService(Intent intent)中传进的intent对象,该intent对象可以携带一些参数
在onHandleIntent(Intent intent)方法中可以通过intent携带的参数来区分不同的intent(即如果多次启动同一个service要执行不同操作时可在intent对象中传入不同的参数来区别),接下来便可以执行不同的操作。
//Operation 1
Intent startServiceIntent = new Intent("com.test.intentservice");
Bundle bundle = new Bundle();
bundle.putString("param", "oper1");
startServiceIntent.putExtras(bundle);
startService(startServiceIntent); //Operation 2
Intent startServiceIntent2 = new Intent("com.test.intentservice");
Bundle bundle2 = new Bundle();
bundle2.putString("param", "oper2");
startServiceIntent2.putExtras(bundle2);
startService(startServiceIntent2);
public class IntentServiceDemo extends IntentService { public IntentServiceDemo() {
//必须实现父类的构造方法
super("IntentServiceDemo");
} @Override
public IBinder onBind(Intent intent) {
System.out.println("onBind");
return super.onBind(intent);
} @Override
public void onCreate() {
System.out.println("onCreate");
super.onCreate();
} @Override
public void onStart(Intent intent, int startId) {
System.out.println("onStart");
super.onStart(intent, startId);
} @Override
public int onStartCommand(Intent intent, int flags, int startId) {
System.out.println("onStartCommand");
return super.onStartCommand(intent, flags, startId);
} @Override
protected void onHandleIntent(Intent intent) {
//Intent是从Activity发过来的,携带识别参数,根据参数不同执行不同的任务
String action = intent.getExtras().getString("param");
if (action.equals("oper1")) {
System.out.println("Operation1");
}else if (action.equals("oper2")) {
System.out.println("Operation2");
} try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
} @Override
public void onDestroy() {
System.out.println("onDestroy");
super.onDestroy();
} }
android基础(四)service的更多相关文章
- Android基础(五) Service全解析----看不见的Activity
一.服务的介绍: 作为Android四大组件之中的一个,Service(服务)也常常运用于我们的日常使用中,它与Activity的差别在于:Service一直在后台执行.没实用户界面.所以绝不会到前台 ...
- <Android基础>(四) Fragment Part 1
Fragment 1)Fragment的简单用法 2)动态添加Fragment 3)在Fragment中模拟返回栈 4)Fragment和活动之间通信 第四章 Fragment Fragment是一种 ...
- <Android基础> (四) Fragment Part 2
4.3 Fragment的生命周期 4.3.1 Fragment的状态和回调 1.运行状态 当一个Fragment是可见的,并且它关联的活动正处于运行状态是,该Fragment也处于运行状态 2.暂停 ...
- 安卓Android基础四天
网页源码查看器 HttpURLConnection:用于发送和接受数据 ScrollView只能由一个孩子 消息机制的写法(***) anr Application not response 应用无响 ...
- android基础---->service的生命周期
服务是一个应用程序组件代表应用程序执行一个长时间操作的行为,虽然不与用户交互或供应功能供其它应用程序使用.它和其他的应用对象一样,在他的宿主进程的主线程中运行.今天我们开始android中普通serv ...
- Android基础测试题(四)
看了前两道题大家有没有发现,测试题少了(一),大家猜猜测试题(一)是什么? Android基础测试题(四): 需求: 建一个方法,格式化输出2016-11-14 10:15:26格式的当前时间,然后截 ...
- 实验四实验报告————Android基础开发
实验四实验报告----Android基础开发 任务一 关于R类 关于apk文件 实验成果 任务二 活动声明周期 实验成果 任务三 关于PendingIntent类 实验成果 任务四 关于布局 实验成果 ...
- Android基础夯实--重温动画(四)之属性动画 ValueAnimator详解
宝剑锋从磨砺出,梅花香自苦寒来:千淘万漉虽辛苦,吹尽狂沙始到金: 长风破浪会有时,直挂云帆济沧海 一.摘要 Animator类作为属性动画的基类,它是一个抽象类,它提供了实现动画的基本架构,但是我们不 ...
- 基础4 Android基础
基础4 Android基础 1. Activity与Fragment的生命周期. Activity生命周期 打开应用 onCreate()->onStart()->onResume 按BA ...
- Android基础总结(8)——服务
服务(Service)是Android中实现程序后台运行的解决方案,它非常适合用于去执行哪些不需要和用户交互而且还要长期运行的任务.服务的运行不依赖任何用户界面,即使当程序被切换到后台,或者用户打开了 ...
随机推荐
- 利用javascript实现课程选择
最终实现的效果如下图所示: 代码如下所示: HTML代码部分: <body> <div class="page" style="overflow: hi ...
- [CCF] Z字形扫描
CCF Z字形扫描 感觉和LeetCode中的ZigZag还是有一些不一样的. 题目描述 在图像编码的算法中,需要将一个给定的方形矩阵进行Z字形扫描(Zigzag Scan).给定一个n×n的矩阵,Z ...
- Python对整形数字进行加密和解密
# -*- coding:utf-8 -*- __author__ = 'Ray' class Encryption: """整形数字简单的一个加密/解密算法" ...
- CentOS7 续续
1.配置网络,虚拟机为桥接模式,IP地址为 192.168.100+学号/24,配置完成后可以通过ping物理机192.168.100段,或者ping 192.168.100.140验证2.通过临时与 ...
- Java中request请求之 - 带文件上传的form表单
常用系统开发中总免不了显示图片,保存一些文件资料等操作. 这些操作的背后,就是程序员最熟悉的 enctype="multipart/form-data"类型的表单. 说起file类 ...
- Android:Butter Knife 8.0.1配置
github地址:https://github.com/GarsonZhang/butterknife Butter Knife Field and method binding for Androi ...
- 在mysql数据库原有字段后增加新内容
update table set user=concat(user,$user) where xx=xxx; [注释]这个语法要求原来的字段值不能为null(可以为空字符''):
- [渣译文] 使用 MVC 5 的 EF6 Code First 入门 系列:MVC程序中实体框架的连接恢复和命令拦截
这是微软官方教程Getting Started with Entity Framework 6 Code First using MVC 5 系列的翻译,这里是第四篇:MVC程序中实体框架的连接恢复和 ...
- sql_树形查询
with Subqry(FID,A_TypeName,A_ParentID) as (select FID,A_TypeName,A_ParentID from tb_Appliances where ...
- iOS- iPhone App 如何运营?
在质量过硬的情况下,如何运营才能使APP冲上app store的推荐?如何获得公众认可?获得下载量? 睡前简单分享一下最近从书中.互联网中浏览到的一些信息,和自己的一点理解. 首先这个问题很大.就抛砖 ...