色偷偷偷亚洲综合网另类,亚洲欧美另类在线观看,欧美午夜激情在线,久久久精品一区

當前位置:首頁 > 嵌入式培訓 > 嵌入式學習 > 講師博文 > 綁定服務時什么時候調用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號

回到頂部

色偷偷偷亚洲综合网另类,亚洲欧美另类在线观看,欧美午夜激情在线,久久久精品一区
主站蜘蛛池模板: 日韩暖暖在线视频| 欧美日韩国产91| 亚洲一区二区三区xxx视频| 日韩视频中文字幕| 国产成人综合久久| 精品成人在线视频| 欧美三级免费观看| 久久久亚洲成人| 奇米4444一区二区三区| 国产精品无av码在线观看| 亚洲成人免费在线视频| 亚洲精品美女网站| 日韩中文字幕精品视频| 欧美性猛交xxxx乱大交3| 欧洲亚洲女同hd| 92裸体在线视频网站| 亚洲午夜色婷婷在线| 久久亚洲综合国产精品99麻豆精品福利| 精品免费在线视频| 欧美伊久线香蕉线新在线| 91精品视频在线免费观看| 在线看国产精品| 狠狠久久五月精品中文字幕| 亚洲电影免费观看高清完整版| 亚洲人成在线一二| 欧美激情亚洲自拍| 国产精品成人品| 亚洲人午夜色婷婷| 欧美夫妻性生活视频| 国产精品直播网红| 日韩中文字幕国产| 人体精品一二三区| 亚洲欧美日韩中文在线| 欧美精品videos| 亚洲第一区第二区| 久久久黄色av| 国产精品老女人视频| 啊v视频在线一区二区三区| 欧亚精品在线观看| 主播福利视频一区| 日韩免费在线观看视频|