绑定服务时什么时候调用onRebind
Serivce中onRebind被调用的时机非常特别,想知道什么时候onRebind被调用,能够接以下的次序来学习。最后自然就明确了!
1. 首先要知道。同一个服务既可能被启动也能够被绑定;
2. Service中onRebind方法被调用。仅仅要符合两个必要条件即可
(1)服务中onUnBind方法返回值为true
(2)服务对象被解绑后没有被销毁。之后再次被绑定
。以下举例说明:
例1:同一个Activity对象
先自启动服务(onCreate, onStartCommand);再绑定服务(onBind); 再解除绑定服务(onUnBind)(因为服务被启动过,所以Service中onDestroy不会被调用);再绑定服务, 这次绑定的服务对象是之前已经创建好的,所以这次绑定服务时就会调用onReBind方法了。而且本次不会调用onBind方法。
例2:不是同一个Activity对象
打开项目,启动MainActivity, 在Activity中启动服务(onCreate, onStartCommand),再绑定服务(onBind); 再解除绑定服务(onUnBind); 再接返回键销毁MainActivity对象(onUnBind);再打开项目启动MainActivity;再绑定服务,这次绑定服务时会调用onReBind方法
代码演示样例:
activity_main.xml文件
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.qf.act.MainActivity"
tools:ignore="MergeRootFrame,Orientation" > <TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView1" /> <Button
android:id="@+id/bind"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="work"
android:text="bind" /> <Button
android:id="@+id/unbind"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="work"
android:text="unbind" /> <Button
android:id="@+id/call"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="work"
android:text="call" /> </LinearLayout>
watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvd2FuZ2d1YW5ncm9uZw==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="">
LocalService.java文件
package com.qf.act; import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;
import android.util.Log; public class LocalService extends Service {
private static String LOG = "LocalService";
private int count = 0;
private IBinder binder = new MyIbinder(); @Override
public IBinder onBind(Intent intent) {
Log.e(LOG, "onBind");
return this.binder;
} @Override
public boolean onUnbind(Intent intent) {
Log.e(LOG, "onUnBind");
return true;
} @Override
public void onRebind(Intent intent) {
super.onRebind(intent);
Log.e(LOG, "onRebind");
} @Override
public void onCreate() {
new Thread() {
public void run() {
Log.e(LOG, "onCreate");
for (int i = 0; i < 100; i++) {
count++;
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}.start();
super.onCreate();
} public class MyIbinder extends Binder {
public int getCount() {
return LocalService.this.getCount();
}
} public int getCount() {
return this.count;
} @Override
public void onDestroy() {
Log.e(LOG, "onDestroy");
super.onDestroy();
}
}
MainActivity.java文件
package com.qf.act; import com.qf.act.LocalService.MyIbinder; import android.app.Activity;
import android.app.Service;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.util.Log;
import android.view.View;
import android.widget.Toast;
public class MainActivity extends Activity {
private boolean isBind = false;
private LocalService.MyIbinder binder = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void work(View v) {
Intent intent = new Intent();
intent.setClass(this, LocalService.class);
switch (v.getId()) {
case R.id.start:
this.startService(intent);
break;
case R.id.stop:
this.stopService(intent);
break;
case R.id.bind:
this.bindService(intent, conn, Service.BIND_AUTO_CREATE);
break;
case R.id.unbind:
if(isBind == true)
{
unbindService(conn);
isBind = false;
}
break;
case R.id.call:
if(this.binder == null)
return;
int count = this.binder.getCount();
Toast.makeText(this, ""+count, 1).show();
break;
}
}
private ServiceConnection conn = new ServiceConnection() {
@Override
public void onServiceDisconnected(ComponentName name) {
Log.e("", "onServiceDisconnected");
}
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
binder = (MyIbinder) service;
isBind = true;
}
};
}
操作演示样例:
1.点击button 启动服务
日志信息: onCreate
2. 点击button bind
日志信息: onBind
3.点击button unbind
日志信息: onUnBind
4.点击button bind
日志信息: onReBind
绑定服务时什么时候调用onRebind的更多相关文章
- Service官方教程(9)绑定服务时的注意事项
Binding to a Service Application components (clients) can bind to a service by calling bindService() ...
- Android应用中创建绑定服务使得用户可以与服务交互
原文:http://android.eoe.cn/topic/android_sdk 一个绑定的服务是客户服务器接口上的一个服务器.一个绑定的服务允许组件(如:活动)来绑定一个服务,传送请求,接收响应 ...
- 实现在GET请求下调用WCF服务时传递对象(复合类型)参数
WCF实现RESETFUL架构很容易,说白了,就是使WCF能够响应HTTP请求并返回所需的资源,如果有人不知道如何实现WCF支持HTTP请求的,可参见我之前的文章<实现jquery.ajax及原 ...
- Android(java)学习笔记229:服务(service)之绑定服务调用服务里面的方法 (采用接口隐藏代码内部实现)
1.接口 接口可以隐藏代码内部的细节,只暴露程序员想暴露的方法 2.利用上面的思想优化之前的案例:服务(service)之绑定服务调用服务里面的方法,如下: (1)这里MainActivity.jav ...
- Android(java)学习笔记228:服务(service)之绑定服务调用服务里面的方法
1.绑定服务调用服务里面的方法,图解: 步骤: (1)在Activity代码里面绑定 bindService(),以bind的方式开启服务 : bindServ ...
- Android(java)学习笔记172:服务(service)之绑定服务调用服务里面的方法 (采用接口隐藏代码内部实现)
1. 接口 接口可以隐藏代码内部的细节,只暴露程序员想暴露的方法 2. 利用上面的思想优化之前的案例:服务(service)之绑定服务调用服务里面的方法,如下: (1)这里MainActivity.j ...
- Android(java)学习笔记171:服务(service)之绑定服务调用服务里面的方法
1.绑定服务调用服务里面的方法,图解: 步骤: (1)在Activity代码里面绑定 bindService(),以bind的方式开启服务 : bindServ ...
- Xamarin.Android广播接收器与绑定服务
一.前言 学习了前面的活动与服务后,你会发现服务对于活动而言似乎就是透明的,相反活动对于服务也是透明的,所以我们还需要一中机制能够将服务和活动之间架起一座桥梁,通过本节的学习,你将会学到广播与绑定服务 ...
- Android--Service之绑定服务交互
前言 开篇名义,这篇博客介绍一下Android下使用绑定服务进行时数据交互的几种方法.关于Android下Service的内容,前面两篇博客已经介绍了,不清楚的可以移步过去先看看:Android--S ...
随机推荐
- ALTER OPERATOR CLASS - 修改一个操作符表的定义
SYNOPSIS ALTER OPERATOR CLASS name USING index_method RENAME TO newname DESCRIPTION 描述 ALTER OPERATO ...
- 1434:【例题2】Best Cow Fences
1434:[例题2]Best Cow Fences 时间限制: 1000 ms 内存限制: 65536 KB提交数: 263 通过数: 146 [题目描述] 给定一个长度为n的 ...
- QT5:先导篇 正则表达式
一.简介 使用正则表达式可以快速完成处理字符串的一些操作,如验证 查找 替换和分割 Qt的QRegExp类是正则表达式的表示类,它基于Perl的正则表达式语言 正则表达式由表达式(expression ...
- Handler和内部类的正确用法
PS:本文摘抄自<Android高级进阶>,仅供学习使用 Android代码中涉及线程间通信的地方经常会使用Handler,典型的代码结构如下. public class HandlerA ...
- python __future__ 使用
在开头加上from __future__ import print_function这句之后,即使在python2.X,使用print就得像python3.X那样加括号使用.python2.X中pri ...
- 【C语言】控制台窗口图形界面编程(一)句柄和文本属性
目录 00. 目录 01. 句柄 02. GetStdHandle函数 03. CloseHandle函数 04. SetConsoleTextAttribute函数 05. 十进制颜色对照表 06. ...
- pycharm connect to github
这位同学写得非常详细,推荐 http://www.cnblogs.com/feixuelove1009/p/5955332.html#undefined
- vue全选与反选以及通过使用如何filter删除数据
在vue学习经常遇到的一些基本问题,下面是购物车里面的部分功能,分享给初学者,直接上源码: <!DOCTYPE html><html> <head> <met ...
- h5 中MP3 播放暂停 jq
<!--音乐--> <div id="music"> <img src="../img/music.gif" class=&quo ...
- BZOJ2212【POI2011】ROT:Tree Rotation 线段树合并
题意: 给一棵n(1≤n≤200000个叶子的二叉树,可以交换每个点的左右子树,要求叶子遍历序的逆序对最少. 分析: 求逆序对我们可以想到权值线段树,所以我们对每个点建一颗线段树(为了避免空间爆炸,采 ...