網站首頁 編程語言 正文
開篇
在前一篇中我們講了bindService的使用。并且我們留下了一個念想,即在bindService取值時故意阻塞30秒,引起了一次ANR并引出了今天的章節-IntentService。
IntentService的生命周期中有一個非常好的方法-onHandleIntent方法,它是一個abstract方法,開發者在實現IntentService時可以覆蓋它來處理“長事務”。
IntentService
Android開發者官網說過:
- Service不是一個單獨的進程,它和它的應用程序在同一個進程中
- Service不是一個線程,這樣就意味著我們應該避免在Service中進行耗時操作
于是乎,Android給我們提供了解決上述問題的替代品,就是下面要講的IntentService; IntentService是繼承與Service并處理異步請求的一個類,在IntentService中有 一個工作線程來處理耗時操作,請求的Intent記錄會加入隊列。
這么神奇?
我們來看演示,如何來驗證這個IntentService里的onHandleIntent處理這種長事務。
課程目標
設Service里有一個字符型數組:
private String[] stdNames = new String[]{"小王", "小明", "小張"};
在Activity里輸入數組下標后、等待30秒、然后把相對應的數組下標所對應的StudentName顯示在Toast里,看看是不是會發生ANR。
該點擊動作可以反復點擊,因為每次點擊后都會執行unbindService。
代碼核心設計
IntentService沒什么特殊的,它只是extends 自 IntentService,同時它擁有一個可以被覆蓋的:onHandleIntent方法。
- 我們這次使用CallBack模式來實現Service里長事務結束后回調activity里的handler實現數值傳遞;
- 我們使用intent.putExtra來實現activity里的數值傳遞到service中去;
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.DemoRealIntentService" tools:targetApi="31"> <service android:name=".LongWaitingService" android:exported="false"></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類-LongWaitingService
package org.mk.android.demorealintentservice;
import android.app.IntentService;
import android.content.Intent;
import android.content.Context;
import android.os.IBinder;
import android.util.Log;
public class LongWaitingService extends IntentService {
private final String TAG = "LongWaitingService";
private String[] stdNames = new String[]{"小王", "小明", "小張"};
private Callback callback;
private int stdNo;
public class StudentBinder extends android.os.Binder {
public LongWaitingService getService() {
return LongWaitingService.this;
}
}
public void setCallback(Callback callback) {
this.callback = callback;
}
public static interface Callback {
void onDataChange(String data);
}
public LongWaitingService() {
super("LongWaitingService");
}
@Override
public void onStart(Intent intent, int startId) {
Log.i(TAG, ">>>>>>onStart");
}
@Override
protected void onHandleIntent(Intent intent) {
if (intent != null) {
Log.i(TAG, ">>>>>>onHandleIntent");
Log.i(TAG, ">>>>>>into a long waiting");
new Thread() {
public void run() {
try {
Thread.sleep(30000);
if (callback != null) {
String stdName = stdNames[stdNo];
callback.onDataChange(stdName);
}
} catch (Exception e) {
}
}
}.start();
}
}
@Override
public IBinder onBind(Intent intent) {
// TODO: Return the communication channel to the service.
Log.i(TAG, ">>>>>>onBind方法被調用");
this.stdNo = intent.getIntExtra("stdNo", -1);
onHandleIntent(intent);
return new StudentBinder();
}
//Service被關閉前回調
@Override
public void onDestroy() {
super.onDestroy();
Log.i(TAG, ">>>>>>onDestroyed方法被調用!");
}
@Override
public boolean onUnbind(Intent intent) {
Log.i(TAG,">>>>>>onUnbind");
return false;
}
}
主類-MainActivity.java
package org.mk.android.demorealintentservice;
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.Handler;
import android.os.IBinder;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
private final String TAG = "LongWaitingService";
private Button buttonGetValueFromBinder;
private Button buttonClose;
private Context ctx;
private Intent intent;
private LongWaitingService.StudentBinder stdBinder;
private EditText etStdNo;
Handler stdHandler = new StudentHandler();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
buttonGetValueFromBinder = (Button) findViewById(R.id.buttonGetValueFromBinder);
etStdNo = (EditText) findViewById(R.id.etStdNo);
ctx = MainActivity.this;
intent = new Intent(ctx, LongWaitingService.class);
buttonGetValueFromBinder.setOnClickListener(new OnClickListener());
}
private ServiceConnection conn = new ServiceConnection() {
//Activity與Service斷開連接時回調該方法
@Override
public void onServiceDisconnected(ComponentName name) {
Log.i(TAG, ">>>>>>Service DisConnected");
}
//Activity與Service連接成功時回調該方法
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
Log.i(TAG, ">>>>>>Service Connected");
stdBinder = (LongWaitingService.StudentBinder) service;
LongWaitingService stdService = stdBinder.getService();
stdService.setCallback(new LongWaitingService.Callback() {
@Override
public void onDataChange(String data) {
Message msg = new Message();
msg.obj = data;
stdHandler.sendMessage(msg);
}
});
}
};
class OnClickListener implements View.OnClickListener {
@Override
public void onClick(View view) {
Intent eIntent;
switch (view.getId()) {
case R.id.buttonGetValueFromBinder:
int stdNo = Integer.valueOf(etStdNo.getText().toString());
intent.putExtra("stdNo", stdNo);
bindService(intent, conn, Service.BIND_AUTO_CREATE);
break;
}
}
}
class StudentHandler extends Handler {
@Override
public void handleMessage(Message msg) {
Log.i(TAG,">>>>>>Service的count" + "的值為:" + msg.obj.toString());
Toast.makeText(getApplicationContext(), "Service的count" + "的值為:" + msg.obj.toString(), Toast.LENGTH_LONG).show();
unbindService(conn);
}
}
}
運行效果
來看看運行效果吧
看,再也沒有ANR了,結果成功通過CALL BACK回傳Activity。
因此我們一般都會這么使用IntentService來實現一些資源異步加載、第三方API回調。
原文鏈接:https://blog.csdn.net/lifetragedy/article/details/128175889
相關推薦
- 2023-01-01 matplotlib基本圖形繪制操作實例_python
- 2022-12-09 python中為main方法傳參問題_python
- 2022-07-08 C#中的圖像Image類與打印Printing類用法_C#教程
- 2022-08-14 在WPF中使用多線程更新UI_C#教程
- 2022-09-27 python常見運算符及用法小結_python
- 2022-05-01 淺談Redis哨兵模式高可用解決方案_Redis
- 2022-06-15 C#實現歸并排序_C#教程
- 2022-10-19 為什么不要在?Flutter?中使用全局變量_Android
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支