網(wǎng)站首頁 編程語言 正文
介紹
在前一天我們介紹了Android中有兩種啟動Service的方法。并擅述了startService和bindService的區(qū)別。同時我們著重講了startService。
因此今天我們就來講bindService。bindService大家可以認(rèn)為它是和Android的一個共生體。即這個service所屬的activity如果消亡那么bindService也會消亡。
因此今天我們以一個比較復(fù)雜的例子,activity<->service間互相傳值來講透這個bindService的使用,同時我們在這個例子中故意留下一個坑即:在Service里使用Thread處理大事務(wù)是不是就一定安全呢?也不安全,它也會引起ANR即:Application Not Responding-安卓崩潰。從而以這個坑來引出IntentService的使用。
來看例子
我們設(shè)有三個按鈕:
- 【BIND SERVICE】-點擊后運(yùn)行Service
- 【STOP BINDING】-點擊后結(jié)束Service
- 【GET VALUE FROM BINDER】-通過Activity獲取正在BINDING的Service內(nèi)的值,此處我們留下了一個ANR的坑,即獲取Service內(nèi)的值時我們留了一個Thread.Sleep(30000)的長事務(wù),來觀察ANR;
此處記得按鈕的點擊順序為:先點【BIND SERVICE】->再點【GET VALUE FROM BINDER】->再點【STOP BINDING】不過此處你沒有機(jī)會點這個【STOP BINDING】按鈕,因為在GET時你已經(jīng)ANR(崩潰)了。
來看全代碼展示。
全代碼
Service注冊
<?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類(坑來了)
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)建時調(diào)用
@Override
public void onCreate() {
Log.i(TAG, ">>>>>>onCreate方法被調(diào)用");
super.onCreate();
//創(chuàng)建一個線程動態(tài)地修改count的值
new Thread() {
public void run() {
while (!quit) {
try {
Thread.sleep(1000);
} catch (Exception e) {
e.printStackTrace();
}
count++;
}
}
;
}.start();
}
//Service斷開連接時回調(diào)
@Override
public boolean onUnbind(Intent intent) {
Log.i(TAG, ">>>>>>onUnbind方法被調(diào)用!");
return true;
}
//Service被啟動時調(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;
}
}
}
我們可以看到,這個Service以每秒對著count+1.
然后通過bindService的onBind生命體里以一個CountNumBinder暴露出去,給到外部可以通過一個getCount方法來調(diào)用獲取Service里當(dāng)前count的值,但是這個值在獲取前我們會使用Thread.sleep(30000)-30秒來模擬ANR。
主運(yùn)行類-MainActivity.java
在調(diào)用Service的activity里我們使用bindService(intent, conn, Service.BIND_AUTO_CREATE);來啟動。
這邊這個conn是一個ServiceConnection類,new出一個ServiceConnection類并覆蓋里面的
- onServiceConnected方法,用于接受bindService返回的對象;
- onServiceDisconnected方法,用于在這個bindService被銷毀時作處理;
具體代碼如下:
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斷開連接時回調(diào)該方法
@Override
public void onServiceDisconnected(ComponentName name) {
Log.i(TAG, ">>>>>>Service DisConnected");
}
//Activity與Service連接成功時回調(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)行效果
- 先點【BIND SERVICE】;
- 再點【GET VALUE FROM BINDER】;
看,ANR出現(xiàn)了。
這就是我說的坑,怎么解決這個坑,請聽下回分解。
原文鏈接:https://blog.csdn.net/lifetragedy/article/details/128175329
相關(guān)推薦
- 2022-07-23 詳解Nginx的超時keeplive_timeout配置步驟_nginx
- 2022-06-28 C#二分查找算法_C#教程
- 2022-04-06 一篇文章帶你了解C/C++的回調(diào)函數(shù)_C 語言
- 2022-09-15 C#?彈出窗口show()和showdialog()的兩種方式_C#教程
- 2022-06-28 python反轉(zhuǎn)單鏈表算法題_python
- 2022-09-26 go語言標(biāo)準(zhǔn)庫fmt包的一鍵入門_Golang
- 2024-04-07 mybatis-plus插入數(shù)據(jù)庫值無效(插入不對,沒有插入數(shù)據(jù)庫默認(rèn)值,int的類型的null會變
- 2022-05-23 如何在Python?中使用?join()?函數(shù)把列表拼接成一個字符串_python
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運(yùn)行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運(yùn)算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡單動態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支