Service与BoardcastReceive
开发service需要两个步骤:
1.定义一个继承Service的子类
2.在AndroidMainfest。xml文件中配置该Service。
Service与Activity都是从Context派生出来的,都可调用Context里定义的如getResource()、getContentResolver()等方法
生命周期方法:
IBinder onBind(Intent intent):该方法是必须实现的方法。返回一个IBinder的对象,应用程序可以通过该对象与Service通信
Void onCreat():第一次被创建后将立即回调该方法
Void onDestory:被关闭之前调用该方法
void onStartCommand(Intent intent, int flags,int startId):每次客户端调用startService(Intent)方法启动该Service是都会回调该方法。
boolean onUnbind(Intent intent):当该service上绑定的所有客户端都断开连接时将会回调该方法
public class FirstService extends Service
{
// 必须实现的方法
@Override
public IBinder onBind(Intent arg0)
{
return null;
}
// Service被创建时回调该方法
@Override
public void onCreate()
{
super.onCreate();
System.out.println("Service is Created");
}
// Service被启动时回调该方法
@Override
public int onStartCommand(Intent intent, int flags, int startId)
{
System.out.println("Service is Started");
return START_STICKY;
}
// Service被关闭之前回调
@Override
public void onDestroy()
{
super.onDestroy();
System.out.println("Service is Destroyed");
}
}
如果希望Service组件做某些事情,只需要在onCreate()或onStartCommand()方法中定义相关业务代码
在AndroidMainfest.xml中配置:
<!--配置一个Service组建--> <service android:name=".FristService"> </service>
在Android系统中运行Service有如下两种方式:
1.通过Context的startService()方法:访问者与service之间没有关联,即使访问者退出了,Service也仍然运行。
2.通过Context的bindService()方法:访问者与service绑定在一起,访问者一旦退出,Service也就终止了。
第一种方式:
public class MainActivity extends Activity
{
Button start, stop;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// 获取程序界面中的start、stop两个按钮
start = (Button) findViewById(R.id.start);
stop = (Button) findViewById(R.id.stop);
// 创建启动Service的Intent
final Intent intent = new Intent(this , FirstService.class);
start.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View arg0)
{
// 启动指定Service
startService(intent);
}
});
stop.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View arg0)
{
// 停止指定Service
stopService(intent);
}
});
}
}
如果Service与访问者之间需要进行方法调用或交换数据,则应该使用bindService()和unbindService()方法启动、关闭Service。
Context的bindService()方法的完整方法签名为:bindService(Intent service,ServiceConnection conn,int flags):
service:该参数通过Intent指定要启动的Service。
conn:该对象用于监听访问者与Service之间的连接情况。当成功连接时回调onServiceConnected(ComponentName name,IBinder service)方法:断开时回调:onServiceDisconnected(ComponentName name)方法、
注:当调用者主动通过unBindService()方法断开与Service的连接时,ServiceConnection对象的 onServiceDisconnected(ComponentName name)方法不会被调用。
flags:指定绑定时是否自动创建Service(如果Service还未创建)。该参数可指定为0(不自动创建)或BIND_AUTO_CREATE(自动创建)。
当开发Service类时,该Service类必须提供一个IBinder onBind(Intent intent)方法,在绑定本地service的情况下,onBind(Intent intent)方法返回的IBinder对象将会传给ServiceConnection对象里onServiceConnected(ComponentName name,IBinder service)方法的service参数,这样实现通信。
Service与BoardcastReceive的更多相关文章
- 通过AngularJS实现前端与后台的数据对接(二)——服务(service,$http)篇
什么是服务? 服务提供了一种能在应用的整个生命周期内保持数据的方法,它能够在控制器之间进行通信,并且能保证数据的一致性. 服务是一个单例对象,在每个应用中只会被实例化一次(被$injector实例化) ...
- Azure Service Fabric 开发环境搭建
微服务体系结构是一种将服务器应用程序构建为一组小型服务的方法,每个服务都按自己的进程运行,并通过 HTTP 和 WebSocket 等协议相互通信.每个微服务都在特定的界定上下文(每服务)中实现特定的 ...
- 无法向会话状态服务器发出会话状态请求。请确保 ASP.NET State Service (ASP.NET 状态服务)已启动,并且客户端端口与服务器端口相同。如果服务器位于远程计算机上,请检查。。。
异常处理汇总-服 务 器 http://www.cnblogs.com/dunitian/p/4522983.html 无法向会话状态服务器发出会话状态请求.请确保 ASP.NET State Ser ...
- C#创建、安装、卸载、调试Windows Service(Windows 服务)的简单教程
前言:Microsoft Windows 服务能够创建在它们自己的 Windows 会话中可长时间运行的可执行应用程序.这些服务可以在计算机启动时自动启动,可以暂停和重新启动而且不显示任何用户界面.这 ...
- java中Action层、Service层和Dao层的功能区分
Action/Service/DAO简介: Action是管理业务(Service)调度和管理跳转的. Service是管理具体的功能的. Action只负责管理,而Service负责实施. DAO只 ...
- org.jboss.deployment.DeploymentException: Trying to install an already registered mbean: jboss.jca:service=LocalTxCM,name=egmasDS
17:34:37,235 INFO [Http11Protocol] Starting Coyote HTTP/1.1 on http-0.0.0.0-8080 17:34:37,281 INFO [ ...
- Android—Service与Activity的交互
service-Android的四大组件之一.人称"后台服务"指其本身的运行并不依赖于用户可视的UI界面 实际开发中我们经常需要service和activity之间可以相互传递数据 ...
- angularjs 1 开发简单案例(包含common.js,service.js,controller.js,page)
common.js var app = angular.module('app', ['ngFileUpload']) .factory('SV_Common', function ($http) { ...
- IIS启动失败,启动Windows Process Activation Service时,出现错误13:数据无效 ;HTTP 错误 401.2 - Unauthorized 由于身份验证头无效,您无权查看此页
因为修改过管理员账号的密码后重启服务器导致IIS无法启动,出现已下异常 1.解决:"启动Windows Process Activation Service时,出现错误13:数据无效&quo ...
随机推荐
- Linux 系统配置
centos 7 防火墙和端口配置开启redis端口,修改防火墙配置文件 vi /etc/sysconfig/iptables 加入端口配置 -A RH-Firewall-1-INPUT -m sta ...
- [C++] Pen questions & linux cmd
1.宏替换,完全展开替换,注意带来副作用 #include <stdio.h>#define 打印语句 printf(“hello”); Void main(void) { If (1) ...
- BAdi:BOM_UPDATE - Check update for BOM Save
需求:BOM中替代项目组要求同一组比率之和必须是100. 实现:BAdi:BOM_UPDATE METHOD if_ex_bom_update~change_at_save. DATA: ls_wa ...
- DeepLearning 学习资料
1 sotfmax 函数: stanford UFLDL: http://deeplearning.stanford.edu/wiki/index.php/Softmax%E5%9B%9E%E5%BD ...
- [GO]goexit的使用
package main import "fmt" func test() { defer fmt.Println("cccccccccccc")//在函数退出 ...
- perl读取excel
因为工作当中遇到要处理大数据的excel的玩意,最多的有几十万行.用perl的方式试试,看看效果如何. ppm install OLE::Storage_Lite #如果不安装这个,后面两个安装不了 ...
- JAVA&JS debug
JAVA Chrome Sources 如果当前代码所在文件页面为一界面 → →| -------------------------- ↓↑ ...
- 第08章 ElasticSearch Java API
本章内容 使用客户端对象(client object)连接到本地或远程ElasticSearch集群. 逐条或批量索引文档. 更新文档内容. 使用各种ElasticSearch支持的查询方式. 处理E ...
- [LeetCode 题解]:Combinations
Given two integers n and k, return all possible combinations of k numbers out of 1 ... n. For exampl ...
- ubuntu 中安装 Redis
1.下载安装root@21ebdf03a086:/# apt-cache search redisroot@21ebdf03a086:/# apt-get install redis-server a ...