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

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

回到頂部

色偷偷偷亚洲综合网另类,亚洲欧美另类在线观看,欧美午夜激情在线,久久久精品一区
主站蜘蛛池模板: 高清欧美性猛交xxxx| 国产欧美精品日韩| 欧美日韩一区二区在线播放| 美女999久久久精品视频| 欧美尺度大的性做爰视频| 欧美日韩亚洲国产一区| 性亚洲最疯狂xxxx高清| 日本免费久久高清视频| 国产精品亚洲欧美导航| 成人写真视频福利网| 亚洲精品视频在线观看视频| 中文字幕在线日韩| 黑丝美女久久久| 日本精品性网站在线观看| 国产美女精品视频| 亚洲乱码国产乱码精品精| 精品国内自产拍在线观看| 欧美日韩亚洲视频| 国产成人综合精品在线| 日韩av有码在线| 久久午夜a级毛片| 日韩美女视频在线观看| 日韩av在线网| 欧美xxxx综合视频| 日韩暖暖在线视频| 亚洲天堂第二页| 日韩欧美国产骚| 91麻豆国产精品| 久久久91精品国产| 日本精品性网站在线观看| 精品一区二区三区三区| 欧美日韩亚洲网| 亚洲精品欧美日韩专区| 久久伊人精品视频| 国产精品欧美日韩久久| 中文字幕日韩欧美在线| 国产97色在线| 中文字幕欧美日韩在线| 日韩美女毛茸茸| 日韩三级成人av网| 国产精品爽爽爽|