绑定服务时什么时候调用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 ...
随机推荐
- Websocket 关闭浏览器报错
这个报错,是因为你关闭之后,websocket 自动连接失败造成的 只要在你的websocket 运行的类里面加上: @OnError public void onError(Throwable e, ...
- CSS中的趣事之float浮动
浮动float一般跟left或是right: 特性: 1,包裹性:浮动文本类型时,需要指定宽度width,如果不指定,就会折叠到最小宽度: 2,浮动会影响别的元素: 3,子级浮动,会导致父级高度 ...
- 编写图形界面下的Java Swing程序,接受用户输入的两个数据为上下限,然后输出上、下限之间的所有素数。(结果部分每行显示10个数据)
这个代码我整体写的比较简单,读者可以简单参考一下,对人家题目要求略微修改了一下,多加了一个“置空”按钮.下图为我设计的界面 运行程序后的截图请看我后面的截图: package com.wangshil ...
- date - 打印或设置系统日期和时间
总览 date [选项]... [+格式] date [选项] [MMDDhhmm[[CC]YY][.ss]] 描述 根据指定格式显示当前时间或设置系统时间. -d, --date=STRING 显示 ...
- 用npm来部署快速一个httpweb服务器
https://blog.csdn.net/u012182627/article/details/55060594 http-server的安装######注意事项 安装http-server的时候 ...
- CAD参数绘制多段线(网页版)
多段线又被称为多义线,表示一起画的都是连在一起的一个复合对象,可以是直线也可以是圆弧并且它们还可以加不同的宽度. 主要用到函数说明: _DMxDrawX::PathLineTo 把路径下一个点移到指定 ...
- java_String类练习
public class StringTest { //1.模拟trim方法,去除字符串两端的空格 public static void main(String[] args) { String st ...
- node.js 的介绍
1.node.js是什么? (1)node.js不是一门编程语言, 是一个开发平台,就像Java开发平台,Net平台,PHP开发平台,Apple开发平台.(何为开发平台?有对应的编程语言,有语言运行时 ...
- Chrome插件:浏览器后台与页面间通信
content.js 与 background.js和popup.js 通信和 background.js与popup.js 这些通信都用 chrome.runtime.sendMessage 这个 ...
- 零基础入门学习Python(4)--改进我们的小游戏
前言 在以前的博客中有做个一个小游戏,但是太简单了,所以这次就来对我们做的小游戏进行改进,改善从以下四个方面进行: 程序猜错的时候要给出提示,例如告诉用户输入的值是大了还是小了. 以前程序每运行一次只 ...