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

  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. CoreGraphics相关方法

    // 将view转为image(不经常用到的功能)(摘自SCCatWaitingHUD) - (UIImage *)convertViewToImage { CGSize s = self.bound ...

  2. iOS UITableView , UITableViewController ,UITableViewCell实现全国各省市遍历,选择相应的地区

    我们先看一下效果                   代码如下 首先是第一个页面 rootTableViewController.h #import <UIKit/UIKit.h> #im ...

  3. TFS中向源代码方案中添加文件

    一些情况下,不能使用VS提供的菜单直接将文件添加到源代码项目,例如该文件是使用TT生成的,或者依赖于其它文件     此时可以在此文件的父级依赖文件上右击,即可添加未受托管的文件     另一种方法, ...

  4. MyEclipse、Eclipse优化设置

    第一步: 取消自动validation validation有一堆,什么xml.jsp.jsf.js等等,我们没有必要全部都去自动校验一下,只是需要的时候才会手工校验一下! 取消方法: windows ...

  5. nodejs 小爬虫

    编写爬虫示例: var http = require('http'); var cheerio = require('cheerio'); var url = 'http://www.cnblogs. ...

  6. SQL server 2014安装以及解决连接数据库失败问题

    安装教程:http://jingyan.baidu.com/article/3a2f7c2e653d5926afd61197.html 安装好之后打开SQL server 2014 Managemen ...

  7. 关于NodeJS的思考

    对于NodeJS来说传统程序员比较陌生,初看以为是什么前端框架,其实并不是前端框架.传统的Javascript只能跑在浏览器中,但是一位叫Ryan Dahl的开发者灵感一来,为什么Javascript ...

  8. php示例代码之使用MySQLi接口

    1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 3 ...

  9. tar包在linux下 java安装

    总的原则:将jdk-7u10-linux-x64.tar.gz压缩包解压至/usr/lib/jdk,设置jdk环境变量并将其修改为系统默认的jdk 1.将jdk-7u5-linux-x64.tar.g ...

  10. XML语言基础1

    这学期选修了XML技术这门课,没有发课本,于是参考了W3school教程,整理一下上课的内容. 1.XML简介 XML是一种标记语言,很类似HTML,它不是对HTML的替代,而是对HTML的补充.在大 ...