Activity 与 Service 之间的消息传递
BgService代码
- public class BgService extends Service {
- public static final String TAG = "BgService";
- private BgBinder binder;
- @Override
- public void onCreate() {
- super.onCreate();
- binder = new BgBinder();
- Log.d(TAG, "onCreate() executed");
- }
- @Override
- public void onDestroy() {
- super.onDestroy();
- Log.d(TAG, "onDestroy() executed");
- }
- @Override
- public int onStartCommand(Intent intent, int flags, int startId) {
- Log.d(TAG, "onStartCommand() executed");
- return super.onStartCommand(intent, flags, startId);
- }
- @Nullable
- @Override
- public IBinder onBind(Intent intent) {
- Log.d(TAG, "onBind() executed");
- return binder;
- }
- class BgBinder extends Binder {
- public void doWork() {
- Log.d(TAG, "doWork() executed");
- }
- }
- }
Activity 代码
- public class MyActivity extends AppCompatActivity {
- private ServiceConnection bgServiceConnection;
- private BgService.BgBinder bgBinder;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- ...
- bgServiceConnection = new ServiceConnection() {
- @Override
- public void onServiceConnected(ComponentName name, IBinder service) {
- bgBinder = (BgService.BgBinder)service;
- bgBinder.doWork(); // 异步回调必须在这里调用
- Log.d("MyActivity", "onServiceConnected");
- }
- @Override
- public void onServiceDisconnected(ComponentName name) {
- bgBinder = null;
- Log.d("MyActivity", "onServiceDisconnected");
- }
- };
- ...
- }
- public void startBg(View view) {
- Intent intent = new Intent(this, BgService.class);
- startService(intent);
- }
- public void stopBg(View view) {
- Intent intent = new Intent(this, BgService.class);
- stopService(intent);
- }
- public void bindBg(View view) {
- Intent intent = new Intent(this, BgService.class);
- bindService(intent, bgServiceConnection, BIND_AUTO_CREATE);
- }
- public void unbindBg(View view) {
- unbindService(bgServiceConnection);
- }
- }
其中
startService 调用onStartCommand, 如果service未create, 会在调用之前, 调用onCreate
stopService 会检查service被引用的情况, 如果没有其他引用, 则调用onDestroy
bindService 会调用onBind(之后在异步返回中执行onServiceConnected), 如果service未create, 会在调用之前, 调用onCreate. 注意, 不会调用onStartCommand
unbindService 会检查service被引用的情况, 如果没有其他引用, 则调用onDestroy,
unbindService与stopService的区别: stopService可多次调用, 不会抛异常, unbindService在执行后如果再调用会抛异常, 不处理则应用崩溃
onServiceDisconnected为什么没被调用? 这个只有在service因异常而断开连接的时候,这个方法才会被调用 This is called when the connection with the service has been unexpectedly disconnected -- that is, its process crashed. Because it is running in our same process, we should never see this happen.
Activity 与 Service 之间的消息传递的更多相关文章
- Activity与Service之间交互并播放歌曲的实现代码
Activity与Service之间交互并播放歌曲,为了方便,我把要播放的歌曲定死了,大家可以灵活改进 MService: 复制代码代码如下: package com.tiantian.test;im ...
- activity与service之间的通信方式
Activity之间的通信 1.activity与activity的通信可以通过Intent来封装数据,startActivityForResult()来实现,当跳转的activity调用finish ...
- activity 与 service 之间的通信
activity和service通信:通过binder 举个我实际项目中的例子:在service中下载更新应用 首先是下载更新apk的service: public class UpdateVersi ...
- Android中Activity、Service和线程之间的通信
Activity.Service和线程应该是Android编程中最常见的几种类了,几乎大多数应用程序都会涉及到这几个类的编程,自然而然的,也就会涉及到三者之间的相互通信,本文就试图简单地介绍一下这三者 ...
- 201709012工作日记--activity与service的通信机制
service生命周期 Service主要包含本地类和远程类. Service不是Thread,Service 是android的一种机制,当它运行的时候如果是Local Service,那么对应的 ...
- Android四大组件应用系列——Activity与Service交互实现APK下载
Servic与Activity相比它没有界面,主要是在后台执行一些任务,Service有两种启动方法startService()和bindService(),startService方式Service ...
- Activity与Service通信(不同进程之间)
使用Messenger 上面的方法只能在同一个进程里才能用,如果要与另外一个进程的Service进行通信,则可以用Messenger. 其实实现IPC(Inter-Process Communicat ...
- Android开发 ---ContentProvider数据提供者,Activity和Service就是上下文对象,短信监听器,内容观察者
1.activity_main.xml <?xml version="1.0" encoding="utf-8"?> <LinearLayou ...
- Android 四大组件(Activity、Service、BroadCastReceiver、ContentProvider)
转载于:http://blog.csdn.net/byxdaz/article/details/9708491 http://blog.csdn.net/q876266464/article/deta ...
随机推荐
- 【CoreData】分页查询和模糊查询
在CoreData实际使用中,分页查询和模糊查询是必不可少的,接下来演示一下: 首先 // 1.创建模型文件 (相当于一个数据库里的表) // New File ———— CoreData ———— ...
- 与TableView插入、删除、移动、多选,刷新控件
一.插入.删除.移动.多选 方法一: Cell的插入.删除.移动都有一个通用的方法,就是更新tableView的数据源,再reloadData,这样做实现上是简单一点,但是reloadData是刷新整 ...
- node.js学习之路
(非原创) 目录 Nodejs的介绍 15个Nodejs应用场景 Nodejs学习路线图 1. Nodejs的介绍 Node.js的是建立在Chrome的JavaScript的运行时,可方便地构建快速 ...
- List 简单升\降序实现
public class User { public int Id { get; set; } public string Code { get; set; } } //示例 //升序 list.So ...
- Java NIO 同步非阻塞
同步非阻塞IO (NIO) NIO是基于事件驱动思想的,实现上通常采用Reactor(http://en.wikipedia.org/wiki/Reactor_pattern)模式,从程序角度而言,当 ...
- .NET领域驱动设计—实践(穿过迷雾走向光明)
阅读目录 开篇介绍 1.1示例介绍 (OnlineExamination在线考试系统介绍) 1.2分析.建模 (对真实业务进行分析.模型化) 1.2.1 用例分析 (提取系统的所有功能需求) 1.3系 ...
- android RelativeLayout 动态设置高度
定义: private RelativeLayout mrlay; 调高度: mrlay = (RelativeLayout) findViewById(R.id.rlay_1); android.v ...
- Kali Linux 秘籍/Web渗透秘籍/无线渗透入门
Kali Linux 秘籍 原书:Kali Linux Cookbook 译者:飞龙 在线阅读 PDF格式 EPUB格式 MOBI格式 Github Git@OSC 目录: 第一章 安装和启动Kali ...
- Altium Desiner 警告 adding hidden net
这是因为 一些元件 隐藏了 vcc GND 或者没有使用vcc GND ,用不着它也报警告了. 这里可以将 vcc GND删掉这个管脚.
- iOS 内存错误调试(EXC_BAD_ACCESS)
内存错误crash现场: Thread堆栈: 有可能是访问被释放对象造成,根据现场并不能找到具体哪个对象出现内存错误. 1.开启僵尸对象调试 Edit Scheme->Debug->Dia ...