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 ...
随机推荐
- android gps定位LocationManager
android location provider有: * LocationManager.GPS_PROVIDER:GPS,精度比较高,但是慢而且消耗电力,而且可能因为天气原因或者障碍物而无法获取卫 ...
- Android 采用post方式提交数据到服务器
接着上篇<Android 采用get方式提交数据到服务器>,本文来实现采用post方式提交数据到服务器 首先对比一下get方式和post方式: 修改布局: <LinearLayout ...
- 【代码笔记】iOS-两个时间字符串的比较
一,效果图. 二,代码. - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the ...
- linux下安装jdk+tomcat+eclipse+mysql
我的环境:主机是win7的,虚拟机是VWare Workstation 6.0 ,linux系统为Red Hat Enterprise Linux 5 64位 各软件版本:jdk是jdk-6u ...
- DOM属性操作
HTML attribute --> DOM property 每个html属性都对应一个DOM对象属性,举个栗子: <div> <label for="userN ...
- 字符输入流Reader简要概括
字符输入流Reader组成结构 本篇将对JAVA I/O流中的字符输入流Reader做个简单的概括: 总得来说,每个字符输入流类都有一个对应的用途,如下: 字符流基类:Reader 字节流转字符流:I ...
- 一秒钟看懂SaaS、CRM、OA、ERP、HR、进销存
自2014年以来,SaaS.CRM.OA.ERP.HR.APM.进销存.财务系统等,这些名词大量出现在微信朋友圈.电视楼宇广告和千百万融资资讯中.它们到底是什么意思?相互之间又有什么区别?在这个飞速发 ...
- ADO.Net(一)——增、删、改、查
数据访问 对应命名空间:System.Data.SqlClient; SqlConnection:连接对象 SqlCommand:命令对象 SqlDataReader:读取器对象 CommandTex ...
- 编写BinIoDemo.java的Java应用程序,程序完成的功能是:完成1.doc文件的复制,复制以后的文件的名称为自己的学号姓名.doc。
package zuoye; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; ...
- MySQL客户端工具 SQLyog
我最喜欢它的History功能,把执行过的操作对应语句都输出出来,一些麻烦的语句用GUI操作后,还能保存对应的sql语句.不错! 官网:http://www.webyog.com 下面是官方的介绍:S ...