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. 防止IOS6与IOS7图标不一致

    点击AppIcon在属性栏内找到iOS icon is pre-rendered打上勾. 如果之前已经安装过,需要先把APP卸载掉再安装.(因为模拟器有缓存) xcode4版本的话需要在INFO内增加 ...

  2. run() 和 start() 的区别

    1) start: 用start方法来启动线程,真正实现了多线程运行,这时无需等待run方法体代码执行完毕而直接继续执行下面(指主线程下面)的代码.通过调用Thread类的start()方法来启动一个 ...

  3. 【原】ios打包ipa的四种实用方法(.app转.ipa)

    总结一下,目前.app包转为.ipa包的方法有以下几种: 1.Apple推荐的方式,即实用xcode的archive功能 Xcode菜单栏->Product->Archive->三选 ...

  4. iOS KVC详细讲解

    iOS KVC详细讲解 什么是KVC? KVC即NSKeyValueCoding,就是键-值编码的意思.一个非正式的 Protocol,是一种间接访问对象的属性使用字符串来标识属性,而不是通过调用存取 ...

  5. TFS2012 自动生成与部署

    思路: 每日构建,自动生成,然后从TFS提交日志中提取版本修订说明,调用打包脚本混淆并生成安装包(系统自带的太锉),最后将相关文件复制到指定网站供浏览下载.自动向测试网站发布,自动生成数据库并初始化. ...

  6. vmware克隆虚拟机eth0网卡无法启动

    概述: 通过vmware克隆安装好的虚拟机之后,出现了网卡未启动的问题. vmware安装虚拟机请看:<vmware快速安装linux虚拟机>. 定位过程: 1.通过ifocnfig命令只 ...

  7. Maven基础配置--nexus私服配置

    登录nexus私服后台,按照下图1-3的顺序进行添加仓库: 其中步骤3有三种仓库类型(Type)进行选择 1. Hosted Repository:本地仓库,在私服服务器上存放用户自行上传的jar包: ...

  8. JavaScript Patterns 6.4 Prototypal Inheritance

    No classes involved; Objects inherit from other objects. Use an empty temporary constructor function ...

  9. Spring AOP 动态代理 缓存

    Spring AOP应用:xml配置及注解实现. 动态代理:jdk.cglib.javassist 缓存应用:高速缓存提供程序ehcache,页面缓存,session缓存 项目地址:https://g ...

  10. Mac下开启FTPserver

    开启命令 sudo -s launchctl load -w /System/Library/LaunchDaemons/ftp.plist   关闭命令 sudo -s launchctl unlo ...