色yeye在线视频观看_亚洲人亚洲精品成人网站_一级毛片免费播放_91精品一区二区中文字幕_一区二区三区日本视频_成人性生交大免费看

當前位置:首頁 > 嵌入式培訓 > 嵌入式學習 > 講師博文 > 綁定服務時什么時候調用onRebind

綁定服務時什么時候調用onRebind 時間:2018-09-25      來源:未知

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="//schemas.android.com/apk/res/android"

xmlns:tools="//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>

布局文件運行的界面

//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.點擊按鈕 啟動服務

日志信息: onCreate

2. 點擊按鈕 bind

日志信息: onBind

3.點擊按鈕 unbind

日志信息: onUnBind

4.點擊按鈕 bind

日志信息: onReBind

上一篇:Shell函數

下一篇:從AlphaGo大戰李世乭,看人工智能的現在與未來

熱點文章推薦
華清學員就業榜單
高薪學員經驗分享
熱點新聞推薦
前臺專線:010-82525158 企業培訓洽談專線:010-82525379 院校合作洽談專線:010-82525379 Copyright © 2004-2022 北京華清遠見科技集團有限公司 版權所有 ,京ICP備16055225號-5,京公海網安備11010802025203號

回到頂部

主站蜘蛛池模板: 巨胸喷奶水视频www 特黄熟妇丰满人妻无码 | 精品国内自产拍在线播放观看 | 欧美一区内射最近更新 | 欧美又黄又大又爽A片三年片 | 日韩内射美女人妻一区二区三区 | 河北老熟女hd | 欧美又黄又大又爽A片三年片 | 经典国产乱子伦精品视频 | 四虎国产精品永久免费网址 | 国产无套中出学生姝 | 日韩一中文字无码不卡 | 91精品久久久久久久久久小网站 | 麻豆高清视频 | 日本一卡二卡四卡无卡乱码视频免费 | 女人被爽到呻吟GIF动态图 | 娇软高h哭宫交双性怀孕攻失忆 | 国产超碰人人爽人人做 | 亚洲精品无码永久在线观看你懂的 | 无码做爰视频WWW网站建设 | 亚洲另类欧美综合久久 | H成年动漫在线观看 | 亚洲中文字幕无码爆乳AV | caoporm国产精品视频免费 | 成人午夜视频一区二区无码 | 亚洲精品久久无码AV片俺去也 | 中日韩精品视频一区二区三区 | 精品产区WNW2544 | 亚洲精品一区二区三区无码夜色 | tube性老少配bbwcom | 亚洲AV无码国产综合专区 | 日本护士xxxxhd | 一区二区三区AV高清免费波多 | 日韩人妻无码一区二区三区久久 | 国产精品免费_区二区三区观看 | 麻豆国产成人AV在线播放欲色 | 国产一二在线 | 吃奶呻吟打开双腿做受动态图 | 日韩国产丝袜人妻一二区 | 国色精品卡一卡2卡3卡4卡免费 | 秋霞理论理论福利院久久 | 国产成人AV乱码在线观看 |