久久婷婷香蕉热狠狠综合,精品无码国产自产拍在线观看蜜,寡妇房东在做爰3,中文字幕日本人妻久久久免费,国产成人精品三上悠亚久久

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

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

Serivce中onRebind被調用(yong)的(de)時(shi)(shi)機很特別,想(xiang)知道(dao)什么時(shi)(shi)候onRebind被調用(yong),可以接(jie)下(xia)面的(de)次序來學(xue)習,后自(zi)然就(jiu)明白了(le)!

 1. 首先(xian)要(yao)知(zhi)道,同一個服務既可(ke)能被啟動也可(ke)以被綁定(ding);

2. Service中onRebind方(fang)法被調用,只要符合兩個必要條(tiao)件就行

<1>服(fu)務中onUnBind方法返回值為true<;2>服(fu)務對象被(bei)(bei)解綁后(hou)沒有被(bei)(bei)銷(xiao)毀(hui),之后(hou)再(zai)次被(bei)(bei)綁定。下(xia)面舉(ju)例(li)說(shuo)明:

例1:同(tong)一個Activity對象

先自啟動服務(onCreate, onStartCommand);再綁定服務(onBind); 再解除綁定服務(onUnBind)(由于服務被啟動過,所以Service中onDestroy不會被調用);再(zai)綁(bang)(bang)定服(fu)務(wu)(wu), 這(zhe)次(ci)綁(bang)(bang)定的(de)服(fu)務(wu)(wu)對(dui)象是之(zhi)前已經創建好(hao)的(de),所以(yi)這(zhe)次(ci)綁(bang)(bang)定服(fu)務(wu)(wu)時就會(hui)調(diao)用onReBind方法了,并(bing)且本次(ci)不會(hui)調(diao)用onBind方法。

例2:不(bu)是同一個(ge)Activity對象(xiang)

打開項目,啟動MainActivity, 在Activity中啟動服務(onCreate, onStartCommand),再綁定服務(onBind); 再解除綁定服務(onUnBind); 再接返回鍵銷毀MainActivity對象(onUnBind);再打開項目啟動MainActivity;再綁定服(fu)務,這次綁定服(fu)務時會調(diao)用(yong)onReBind方法

代碼示例:

//activity_main.xml文件

<LinearLayout xmlns:android="//schemas.android.com/apk/res/android"

xmlns:tools=&quot;//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>

布局文(wen)件(jian)運行(xing)的界面

//LocalService.java文(wen)件

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文件(jian)

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&quot;);

}

@Override

public void onServiceConnected(ComponentName name, IBinder service) {

binder = (MyIbinder) service;

isBind = true;

}

};

}

操作示例:

1.點擊按鈕 啟動服務

日志信息: onCreate

2. 點擊按鈕(niu) bind

日志信息: onBind

3.點擊按鈕 unbind

日志信(xin)息: onUnBind

4.點擊按鈕(niu) bind

日志信息: onReBind

上一篇:Shell函數

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

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

回到頂部