BgService代码

  1. public class BgService extends Service {
  2. public static final String TAG = "BgService";
  3. private BgBinder binder;
  4.  
  5. @Override
  6. public void onCreate() {
  7. super.onCreate();
  8. binder = new BgBinder();
  9. Log.d(TAG, "onCreate() executed");
  10. }
  11.  
  12. @Override
  13. public void onDestroy() {
  14. super.onDestroy();
  15. Log.d(TAG, "onDestroy() executed");
  16. }
  17.  
  18. @Override
  19. public int onStartCommand(Intent intent, int flags, int startId) {
  20. Log.d(TAG, "onStartCommand() executed");
  21. return super.onStartCommand(intent, flags, startId);
  22. }
  23.  
  24. @Nullable
  25. @Override
  26. public IBinder onBind(Intent intent) {
  27. Log.d(TAG, "onBind() executed");
  28. return binder;
  29. }
  30.  
  31. class BgBinder extends Binder {
  32. public void doWork() {
  33. Log.d(TAG, "doWork() executed");
  34. }
  35. }
  36. }

Activity 代码

  1. public class MyActivity extends AppCompatActivity {
  2. private ServiceConnection bgServiceConnection;
  3. private BgService.BgBinder bgBinder;
  4.  
  5. @Override
  6. protected void onCreate(Bundle savedInstanceState) {
  7. ...
  8. bgServiceConnection = new ServiceConnection() {
  9. @Override
  10. public void onServiceConnected(ComponentName name, IBinder service) {
  11. bgBinder = (BgService.BgBinder)service;
  12. bgBinder.doWork(); // 异步回调必须在这里调用
  13. Log.d("MyActivity", "onServiceConnected");
  14. }
  15.  
  16. @Override
  17. public void onServiceDisconnected(ComponentName name) {
  18. bgBinder = null;
  19. Log.d("MyActivity", "onServiceDisconnected");
  20. }
  21. };
  22. ...
  23. }
  24.  
  25. public void startBg(View view) {
  26. Intent intent = new Intent(this, BgService.class);
  27. startService(intent);
  28. }
  29.  
  30. public void stopBg(View view) {
  31. Intent intent = new Intent(this, BgService.class);
  32. stopService(intent);
  33. }
  34.  
  35. public void bindBg(View view) {
  36. Intent intent = new Intent(this, BgService.class);
  37. bindService(intent, bgServiceConnection, BIND_AUTO_CREATE);
  38. }
  39.  
  40. public void unbindBg(View view) {
  41. unbindService(bgServiceConnection);
  42. }
  43. }

其中
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 之间的消息传递的更多相关文章

  1. Activity与Service之间交互并播放歌曲的实现代码

    Activity与Service之间交互并播放歌曲,为了方便,我把要播放的歌曲定死了,大家可以灵活改进 MService: 复制代码代码如下: package com.tiantian.test;im ...

  2. activity与service之间的通信方式

    Activity之间的通信 1.activity与activity的通信可以通过Intent来封装数据,startActivityForResult()来实现,当跳转的activity调用finish ...

  3. activity 与 service 之间的通信

    activity和service通信:通过binder 举个我实际项目中的例子:在service中下载更新应用 首先是下载更新apk的service: public class UpdateVersi ...

  4. Android中Activity、Service和线程之间的通信

    Activity.Service和线程应该是Android编程中最常见的几种类了,几乎大多数应用程序都会涉及到这几个类的编程,自然而然的,也就会涉及到三者之间的相互通信,本文就试图简单地介绍一下这三者 ...

  5. 201709012工作日记--activity与service的通信机制

    service生命周期 Service主要包含本地类和远程类. Service不是Thread,Service 是android的一种机制,当它运行的时候如果是Local Service,那么对应的 ...

  6. Android四大组件应用系列——Activity与Service交互实现APK下载

    Servic与Activity相比它没有界面,主要是在后台执行一些任务,Service有两种启动方法startService()和bindService(),startService方式Service ...

  7. Activity与Service通信(不同进程之间)

    使用Messenger 上面的方法只能在同一个进程里才能用,如果要与另外一个进程的Service进行通信,则可以用Messenger. 其实实现IPC(Inter-Process Communicat ...

  8. Android开发 ---ContentProvider数据提供者,Activity和Service就是上下文对象,短信监听器,内容观察者

    1.activity_main.xml <?xml version="1.0" encoding="utf-8"?> <LinearLayou ...

  9. Android 四大组件(Activity、Service、BroadCastReceiver、ContentProvider)

    转载于:http://blog.csdn.net/byxdaz/article/details/9708491 http://blog.csdn.net/q876266464/article/deta ...

随机推荐

  1. 【CoreData】分页查询和模糊查询

    在CoreData实际使用中,分页查询和模糊查询是必不可少的,接下来演示一下: 首先 // 1.创建模型文件 (相当于一个数据库里的表) // New File ———— CoreData ———— ...

  2. 与TableView插入、删除、移动、多选,刷新控件

    一.插入.删除.移动.多选 方法一: Cell的插入.删除.移动都有一个通用的方法,就是更新tableView的数据源,再reloadData,这样做实现上是简单一点,但是reloadData是刷新整 ...

  3. node.js学习之路

    (非原创) 目录 Nodejs的介绍 15个Nodejs应用场景 Nodejs学习路线图 1. Nodejs的介绍 Node.js的是建立在Chrome的JavaScript的运行时,可方便地构建快速 ...

  4. List 简单升\降序实现

    public class User { public int Id { get; set; } public string Code { get; set; } } //示例 //升序 list.So ...

  5. Java NIO 同步非阻塞

    同步非阻塞IO (NIO) NIO是基于事件驱动思想的,实现上通常采用Reactor(http://en.wikipedia.org/wiki/Reactor_pattern)模式,从程序角度而言,当 ...

  6. .NET领域驱动设计—实践(穿过迷雾走向光明)

    阅读目录 开篇介绍 1.1示例介绍 (OnlineExamination在线考试系统介绍) 1.2分析.建模 (对真实业务进行分析.模型化) 1.2.1 用例分析 (提取系统的所有功能需求) 1.3系 ...

  7. android RelativeLayout 动态设置高度

    定义: private RelativeLayout mrlay; 调高度: mrlay = (RelativeLayout) findViewById(R.id.rlay_1); android.v ...

  8. Kali Linux 秘籍/Web渗透秘籍/无线渗透入门

    Kali Linux 秘籍 原书:Kali Linux Cookbook 译者:飞龙 在线阅读 PDF格式 EPUB格式 MOBI格式 Github Git@OSC 目录: 第一章 安装和启动Kali ...

  9. Altium Desiner 警告 adding hidden net

    这是因为 一些元件 隐藏了 vcc GND 或者没有使用vcc GND ,用不着它也报警告了. 这里可以将 vcc GND删掉这个管脚.

  10. iOS 内存错误调试(EXC_BAD_ACCESS)

    内存错误crash现场: Thread堆栈: 有可能是访问被释放对象造成,根据现场并不能找到具体哪个对象出现内存错误. 1.开启僵尸对象调试 Edit Scheme->Debug->Dia ...