Android入門(mén)之bindService的用法詳解
介紹
在前一天我們介紹了Android中有兩種啟動(dòng)Service的方法。并擅述了startService和bindService的區(qū)別。同時(shí)我們著重講了startService。
因此今天我們就來(lái)講bindService。bindService大家可以認(rèn)為它是和Android的一個(gè)共生體。即這個(gè)service所屬的activity如果消亡那么bindService也會(huì)消亡。
因此今天我們以一個(gè)比較復(fù)雜的例子,activity<->service間互相傳值來(lái)講透這個(gè)bindService的使用,同時(shí)我們?cè)谶@個(gè)例子中故意留下一個(gè)坑即:在Service里使用Thread處理大事務(wù)是不是就一定安全呢?也不安全,它也會(huì)引起ANR即:Application Not Responding-安卓崩潰。從而以這個(gè)坑來(lái)引出IntentService的使用。
來(lái)看例子
我們?cè)O(shè)有三個(gè)按鈕:
- 【BIND SERVICE】-點(diǎn)擊后運(yùn)行Service
- 【STOP BINDING】-點(diǎn)擊后結(jié)束Service
- 【GET VALUE FROM BINDER】-通過(guò)Activity獲取正在BINDING的Service內(nèi)的值,此處我們留下了一個(gè)ANR的坑,即獲取Service內(nèi)的值時(shí)我們留了一個(gè)Thread.Sleep(30000)的長(zhǎng)事務(wù),來(lái)觀察ANR;
此處記得按鈕的點(diǎn)擊順序?yàn)椋合赛c(diǎn)【BIND SERVICE】->再點(diǎn)【GET VALUE FROM BINDER】->再點(diǎn)【STOP BINDING】不過(guò)此處你沒(méi)有機(jī)會(huì)點(diǎn)這個(gè)【STOP BINDING】按鈕,因?yàn)樵贕ET時(shí)你已經(jīng)ANR(崩潰)了。
來(lái)看全代碼展示。
全代碼
Service注冊(cè)
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools"> <application android:allowBackup="true" android:dataExtractionRules="@xml/data_extraction_rules" android:fullBackupContent="@xml/backup_rules" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true" android:theme="@style/Theme.DemoBindService" tools:targetApi="31"> <service android:name=".SampleBindService" android:enabled="true" android:exported="true"> <intent-filter> <action android:name="org.mk.android.demo.SampleBindService"/> </intent-filter> </service> <activity android:name=".MainActivity" android:exported="true"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> <meta-data android:name="android.app.lib_name" android:value="" /> </activity> </application> </manifest>
Service類(lèi)(坑來(lái)了)
package org.mk.android.demo; import android.app.Service; import android.content.Intent; import android.os.Binder; import android.os.IBinder; import android.util.Log; public class SampleBindService extends Service { private final String TAG = "SimpleBindService"; private int count; private boolean quit; private CountNumBinder countNumBinder = new CountNumBinder(); public SampleBindService() { } @Override public IBinder onBind(Intent intent) { // TODO: Return the communication channel to the service. Log.i(TAG, ">>>>>>onBind方法被調(diào)用"); return countNumBinder; } //Service被關(guān)閉前回調(diào) @Override public void onDestroy() { super.onDestroy(); this.quit = true; Log.i(TAG, ">>>>>>onDestroyed方法被調(diào)用!"); } @Override public void onRebind(Intent intent) { Log.i(TAG, ">>>>>>onRebind方法被調(diào)用!"); super.onRebind(intent); } //Service被創(chuàng)建時(shí)調(diào)用 @Override public void onCreate() { Log.i(TAG, ">>>>>>onCreate方法被調(diào)用"); super.onCreate(); //創(chuàng)建一個(gè)線程動(dòng)態(tài)地修改count的值 new Thread() { public void run() { while (!quit) { try { Thread.sleep(1000); } catch (Exception e) { e.printStackTrace(); } count++; } } ; }.start(); } //Service斷開(kāi)連接時(shí)回調(diào) @Override public boolean onUnbind(Intent intent) { Log.i(TAG, ">>>>>>onUnbind方法被調(diào)用!"); return true; } //Service被啟動(dòng)時(shí)調(diào)用 @Override public int onStartCommand(Intent intent, int flags, int startId) { Log.i(TAG, ">>>>>>onStartCommand方法被調(diào)用"); return super.onStartCommand(intent, flags, startId); } public class CountNumBinder extends Binder { public int getCount() { Log.i(TAG, ">>>>>>into long waiting"); try { Thread.sleep(300000); } catch (Exception e) { } return -1; } } }
我們可以看到,這個(gè)Service以每秒對(duì)著count+1.
然后通過(guò)bindService的onBind生命體里以一個(gè)CountNumBinder暴露出去,給到外部可以通過(guò)一個(gè)getCount方法來(lái)調(diào)用獲取Service里當(dāng)前count的值,但是這個(gè)值在獲取前我們會(huì)使用Thread.sleep(30000)-30秒來(lái)模擬ANR。
主運(yùn)行類(lèi)-MainActivity.java
在調(diào)用Service的activity里我們使用bindService(intent, conn, Service.BIND_AUTO_CREATE);來(lái)啟動(dòng)。
這邊這個(gè)conn是一個(gè)ServiceConnection類(lèi),new出一個(gè)ServiceConnection類(lèi)并覆蓋里面的
- onServiceConnected方法,用于接受bindService返回的對(duì)象;
- onServiceDisconnected方法,用于在這個(gè)bindService被銷(xiāo)毀時(shí)作處理;
具體代碼如下:
package org.mk.android.demo; import androidx.appcompat.app.AppCompatActivity; import android.app.Service; import android.content.ComponentName; import android.content.Context; 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.Button; import android.widget.Toast; public class MainActivity extends AppCompatActivity { private final String TAG = "SimpleBindService"; private Button buttonBindService; private Button buttonStopBinding; private Button buttonGetValueFromBinder; private Context ctx; private Intent intent; private SampleBindService.CountNumBinder countNumBinder; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); buttonBindService = (Button) findViewById(R.id.buttonBindService); buttonStopBinding = (Button) findViewById(R.id.buttonStopBinding); buttonGetValueFromBinder = (Button) findViewById(R.id.buttonGetValueFromBinder); ctx = MainActivity.this; intent = new Intent(ctx, SampleBindService.class); buttonBindService.setOnClickListener(new OnClickListener()); buttonStopBinding.setOnClickListener(new OnClickListener()); buttonGetValueFromBinder.setOnClickListener(new OnClickListener()); } private ServiceConnection conn = new ServiceConnection() { //Activity與Service斷開(kāi)連接時(shí)回調(diào)該方法 @Override public void onServiceDisconnected(ComponentName name) { Log.i(TAG, ">>>>>>Service DisConnected"); } //Activity與Service連接成功時(shí)回調(diào)該方法 @Override public void onServiceConnected(ComponentName name, IBinder service) { Log.i(TAG, ">>>>>>Service Connected"); countNumBinder = (SampleBindService.CountNumBinder) service; } }; class OnClickListener implements View.OnClickListener { @Override public void onClick(View view) { Intent eIntent; switch (view.getId()) { case R.id.buttonBindService: bindService(intent, conn, Service.BIND_AUTO_CREATE); break; case R.id.buttonStopBinding: unbindService(conn); break; case R.id.buttonGetValueFromBinder: Toast.makeText(getApplicationContext(), "Service的count" + "的值為:" + countNumBinder.getCount(), Toast.LENGTH_LONG).show(); break; } } } }
運(yùn)行效果
- 先點(diǎn)【BIND SERVICE】;
- 再點(diǎn)【GET VALUE FROM BINDER】;
看,ANR出現(xiàn)了。
這就是我說(shuō)的坑,怎么解決這個(gè)坑,請(qǐng)聽(tīng)下回分解。
到此這篇關(guān)于Android入門(mén)之bindService的用法詳解的文章就介紹到這了,更多相關(guān)Android bindService內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Android 啟動(dòng)第三方程序的方法總結(jié)
這篇文章主要介紹了Android 啟動(dòng)第三方程序的方法總結(jié)的相關(guān)資料,需要的朋友可以參考下2017-04-04條件數(shù)據(jù)庫(kù)Android:sqllite的簡(jiǎn)單使用
條件數(shù)據(jù)庫(kù)Android:sqllite的簡(jiǎn)單使用,需要的朋友可以參考一下2013-05-05Android給自定義按鍵添加廣播和通過(guò)廣播給當(dāng)前焦點(diǎn)輸入框賦值
這篇文章主要介紹了Android給自定義按鍵添加廣播和通過(guò)廣播給當(dāng)前焦點(diǎn)輸入框賦值的相關(guān)資料,需要的朋友可以參考下2016-10-10Android自定義view實(shí)現(xiàn)水波紋進(jìn)度球效果
在我們的日常開(kāi)發(fā)中自定義控件還是用的挺多的,設(shè)計(jì)師或者產(chǎn)品為了更好的漂亮,美觀,交互都會(huì)做一些牛逼的ui效果圖,但是最后實(shí)現(xiàn)的還是我們程序員啊。所以說(shuō) 自定義view你還是得會(huì)的。2016-08-08Android自定義ScrollView使用自定義監(jiān)聽(tīng)
這篇文章主要介紹了Android自定義ScrollView使用自定義監(jiān)聽(tīng) ,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-12-12Kotlin高階函數(shù)reduce與fold使用實(shí)例
Kotlin的高階函數(shù)reduce和fold可以用來(lái)對(duì)集合進(jìn)行聚合操作。reduce函數(shù)將集合元素逐個(gè)累加,而fold函數(shù)則可以指定一個(gè)初始值進(jìn)行累加。這兩個(gè)函數(shù)在處理大數(shù)據(jù)集時(shí)非常有用2023-04-04Android點(diǎn)擊WebView實(shí)現(xiàn)圖片縮放及滑動(dòng)瀏覽效果
這篇文章主要為大家詳細(xì)介紹了Android點(diǎn)擊WebView實(shí)現(xiàn)圖片縮放及滑動(dòng)瀏覽效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-12-12Android實(shí)現(xiàn)仿微軟系統(tǒng)加載動(dòng)畫(huà)效果
這篇文章主要介紹了Android實(shí)現(xiàn)仿微軟系統(tǒng)加載動(dòng)畫(huà)效果的方法,幫助大家更好的理解和學(xué)習(xí)使用Android,感興趣的朋友可以了解下2021-04-04創(chuàng)建子線程對(duì)Android進(jìn)行網(wǎng)絡(luò)訪問(wèn)
這篇文章介紹了Android中創(chuàng)建子線程進(jìn)行網(wǎng)絡(luò)訪問(wèn)的方法,文中通過(guò)示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考2021-11-11