绑定服务时什么时候调用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 ...
随机推荐
- netstat -a 显示出你的计算机当前所开放的所有端口
netstat -s -e 比较详细的显示你的网络资料,包括TCP.UDP.ICMP 和 IP的统计等
- ascii - 在八进制,十进制,十六进制中的 ASCII 字符集编码
描述 ASCII 是美国对于信息交换的标准代码,它是7位码,许多8位码(比如 ISO 8859-1, Linux 的默认字符集)容纳 ASCII 作为它们的下半部分.对应的国际 ASSII 是 ISO ...
- NET VBCSCompiler.exe占用100%,造成项目卡顿的的解决方法
1)服务器环境 最低配 的window server 2008 r2, 配置低容易发现问题‘ 2)事件描述 :项目打开缓慢,查询列表卡顿 3)问题分析:排除代码问题, ->打开服务器任务管理器 ...
- iOS sandbox
iOS的沙盒机制,应用只能访问自己应用目录下的文件.iOS不像android,没有SD卡概念,不能直接访问图像.视频等内容.iOS应用产生的内容,如图像.文件.缓存内容等都必须存储在自己的沙盒内.默认 ...
- Java 文件操作大集合
package com.sess.hny.sys.fileserver; import java.io.BufferedInputStream;import java.io.BufferedOutpu ...
- Spider-Python爬虫之使用Selenium模拟浏览器行为
分析 他的代码比较简单,主要有以下的步骤:使用BeautifulSoup库,打开百度贴吧的首页地址,再解析得到id为new_list标签底下的img标签,最后将img标签的图片保存下来. header ...
- os系统下安装Python2和Python3
一.下载Xcode工具 1.在App Store 里面下载并安装Xcode 2.安装好Xcode后就打开它,首次进入会有一些LicenceAgreement,点同意就是了,然后就进入到 这个界面: 3 ...
- c++学习第一课--输入/输出
1,程序: #include<iostream> int main() { std::cout<<"Enter two numbers:"<& ...
- 32道常见的Java基础面试题
1. 什么是 Java 虚拟机(JVM)?为什么 Java 被称作是“平台无关的编程语言”? Java 虚拟机是一个可以执行 Java 字节码的虚拟机进程.Java 源文件被编译成能被 Java 虚拟 ...
- HDU-1163Eddy's digital Roots,九余定理的另一种写法!
下午做了NYOJ-424Eddy's digital Roots后才正式接触了九余定理,不过这题可不是用的九余定理做的.网上的博客千篇一律,所以本篇就不发篇幅过多介绍九余定理了: 但还是要知道什么是九 ...