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 ...
随机推荐
- Double 数据保留两位小数二:直接截取小数后面两位,不进行四舍五入
package com; public class T2 { public static void main(String[] args) { System.out.println(calculate ...
- iOS中使用 Reachability 检测网络
iOS中使用 Reachability 检测网络 内容提示:下提供离线模式(Evernote).那么你会使用到Reachability来实现网络检测. 写本文的目的 了解Reachability都 ...
- iOS 用webView加载后台返回的HTML数据
返回数据: <p><b>\U516c\U53f8\U7b80\U4ecb</b></p><p> \U5584\U76c8\U7ba1\ ...
- iOS-点击推送消息跳转处理
当用户通过点击通知消息进入应用时 - (BOOL)application:(UIApplication*)application didFinishLaunchingWithOptions:(NSDi ...
- iOS之 Xcode7下 bitcode的工作流程及安全评估
文章参考来自http://www.freebuf.com/articles/others-articles/89806.html 很多朋友在升级Xcode7以后原有正常运行的工程在Xcode7下编译会 ...
- go的环境变量设置
GOROOT go的安装路劲 如:D:\Program Files\Go GOPATH go的工作路径 GOPATH可以设置多个.存放包文件.比如你引入 "xxx"包.那么go会去 ...
- IIS和tomcat共用80端口
IIS和tomcat共用80端口 很多机器都需要同时使用tomcat和iis两个服务器以部署不同的网站,而解决共用80端口的问题也经常遇到,今天实际操作了一回,以下是具体步骤: 实现tomcat和ii ...
- XML语言基础1
这学期选修了XML技术这门课,没有发课本,于是参考了W3school教程,整理一下上课的内容. 1.XML简介 XML是一种标记语言,很类似HTML,它不是对HTML的替代,而是对HTML的补充.在大 ...
- 【转发】揭秘Facebook 的系统架构
揭底Facebook 的系统架构 www.MyException.Cn 发布于:2012-08-28 12:37:01 浏览:0次 0 揭秘Facebook 的系统架构 www.MyExcep ...
- 深入解析Windows操作系统笔记——CH1概念和术语
1.概念和工具 本章主要介绍Windows操作系统的关键概念和术语 1.概念和工具 1.1操作系统版本 1.2基础概念和术语 1.2.1Windows API 1.2.2 服务.函数和例程 1.2.3 ...