Android中bindService的使用方法

2021-07-09 08:27:38 字數 1047 閱讀 7411

bindservice用於繫結乙個服務。這樣當bindservice(intent,conn,flags)後,就會繫結乙個服務。這樣做可以獲得這個服務物件本身,而用startservice(intent)的方法只能啟動服務。

bindservice方式的一般過程:

①新建service類bindservice。在bindservice類裡新建內部類mybinder,繼承自binder(binder實現ibinder介面)。mybinder提供方法返回bindservice例項。

public

class

mybinder 

extends

binder}

例項化mybinder得到mybinder物件;

重寫onbind()方法:

@override

public ibinder onbind(intent intent)

②在activity裡,例項化serviceconnection介面的實現類,重寫onserviceconnected()和onservicedisconnected()方法

serviceconnection conn=new serviceconnection()

@override

public void onservicedisconnected(componentname name)

};③在activity的oncreate()方法裡,新建intent,並繫結服務

intent intent=new intent(mainactivity.this,bindservice.class); 

bindservice(intent, conn,bind_auto_create);

④在activity的ondestroy裡面,新增

unbindservice(conn);

bindservice()的執行過程如下:

bindservice(intent,conn,flag)->service:oncreate()->service:onbind()->activity:onserviceconnected()

android 中layout weight的作用

layout weight 用於給乙個線性布局中的諸多檢視的重要度賦值。所有的檢視都有乙個layout weight值,預設為零,意思是需要顯示多大的檢視就佔據多大的螢幕空 間。若賦乙個高於零的值,則將父檢視中的可 用空間分割,分割大小具體取決於每乙個檢視layout weight 值以及該值在當前...

Android中LayoutParams的用法

寧願做過了後悔,也不要錯過了後悔。本講內容 layoutparams的用法 一 layoutparams的認識 從官方文件可知layoutparams繼承於android.view.viewgroup.layoutparams.layoutparams相當於乙個layout的資訊包,它封裝了layo...

android中layout weight的理解

例如 xmlns android android layout width fill parent android layout height wrap content android background 00ff00 android weightsum 0 android orientation...