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的更多相关文章

  1. Service官方教程(9)绑定服务时的注意事项

    Binding to a Service Application components (clients) can bind to a service by calling bindService() ...

  2. Android应用中创建绑定服务使得用户可以与服务交互

    原文:http://android.eoe.cn/topic/android_sdk 一个绑定的服务是客户服务器接口上的一个服务器.一个绑定的服务允许组件(如:活动)来绑定一个服务,传送请求,接收响应 ...

  3. 实现在GET请求下调用WCF服务时传递对象(复合类型)参数

    WCF实现RESETFUL架构很容易,说白了,就是使WCF能够响应HTTP请求并返回所需的资源,如果有人不知道如何实现WCF支持HTTP请求的,可参见我之前的文章<实现jquery.ajax及原 ...

  4. Android(java)学习笔记229:服务(service)之绑定服务调用服务里面的方法 (采用接口隐藏代码内部实现)

    1.接口 接口可以隐藏代码内部的细节,只暴露程序员想暴露的方法 2.利用上面的思想优化之前的案例:服务(service)之绑定服务调用服务里面的方法,如下: (1)这里MainActivity.jav ...

  5. Android(java)学习笔记228:服务(service)之绑定服务调用服务里面的方法

    1.绑定服务调用服务里面的方法,图解: 步骤: (1)在Activity代码里面绑定 bindService(),以bind的方式开启服务 :                     bindServ ...

  6. Android(java)学习笔记172:服务(service)之绑定服务调用服务里面的方法 (采用接口隐藏代码内部实现)

    1. 接口 接口可以隐藏代码内部的细节,只暴露程序员想暴露的方法 2. 利用上面的思想优化之前的案例:服务(service)之绑定服务调用服务里面的方法,如下: (1)这里MainActivity.j ...

  7. Android(java)学习笔记171:服务(service)之绑定服务调用服务里面的方法

    1.绑定服务调用服务里面的方法,图解: 步骤: (1)在Activity代码里面绑定 bindService(),以bind的方式开启服务 :                     bindServ ...

  8. Xamarin.Android广播接收器与绑定服务

    一.前言 学习了前面的活动与服务后,你会发现服务对于活动而言似乎就是透明的,相反活动对于服务也是透明的,所以我们还需要一中机制能够将服务和活动之间架起一座桥梁,通过本节的学习,你将会学到广播与绑定服务 ...

  9. Android--Service之绑定服务交互

    前言 开篇名义,这篇博客介绍一下Android下使用绑定服务进行时数据交互的几种方法.关于Android下Service的内容,前面两篇博客已经介绍了,不清楚的可以移步过去先看看:Android--S ...

随机推荐

  1. HTML标签的分类

    html中的标签元素大体被分为三种不同的类型:块状元素.内联元素和内联块状元素.常用的块状元素有:<div>.<p>.<h1>...<h6>.<o ...

  2. python strip() 函数探究

    strip()方法语法:str.strip([chars]); 声明:str为字符串,rm为要删除的字符序列 str.strip(rm) 删除字符串中开头.结尾处,位于rm删除序列的字符 eg1: # ...

  3. cookie设置和读取以及获取超链接参数

    function setCookie(c_name, value, expiredays) { var exdate = new Date() exdate.setDate(exdate.getDat ...

  4. 模拟Java-Sping,实现其IOC和AOP核心

    简易的IOC框图 注解的方式实现了左边的分支,那么就剩下右边的XML分支: XmlContext:这个类是也是AbstractApplicationContext的子类,和AnnotationCont ...

  5. Lodash数组方法中文总结

    LodashAPI总结 Lodash是一个特别特别好用的工具,感觉有了Lodash就没有解决不了的问题了~~~~ 使用初开始 官网 https://www.lodashjs.com/docs/4.17 ...

  6. C#override与new修饰隐藏的区别(转载)

    C#比java多一个new隐藏的功能.C# override重写相当于java中没有关键字的方法重写.所以java中方法是没有隐藏功能的. C# override重写,是指对父类中的虚方法(标记vir ...

  7. new Buffer 生成二进制数据

    node编辑环境下: > new Buffer("admin")<Buffer 61 64 6d 69 6e> 通过post请求,服务端接收到是流数据,必须把流数 ...

  8. CAD使用SetxDataLong写数据(com接口)

    主要用到函数说明: MxDrawEntity::SetxDataLong 写一个long扩展数据,详细说明如下: 参数 说明 [in] BSTR val 字符串值 szAppName 扩展数据名称 n ...

  9. No-2.注释

    01. 注释的作用 使用用自己熟悉的语言,在程序中对某些代码进行标注说明,增强程序的可读性 02. 单行注释(行注释) 以 # 开头,# 右边的所有东西都被当做说明文字,而不是真正要执行的程序,只起到 ...

  10. C++链表STL

    #include <iostream> #include <list> #include <algorithm> #include <stdlib.h> ...