網站首頁 編程語言 正文
采用Service的方式,將Activity的計算的業務交給Service去做
參考代碼
1.目錄
2.布局文件
采用線性布局的方式
界面中主要包含的內容有EditText兩個
三個Button按鈕
布局界面代碼:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context=".MainActivity"> <EditText android:id="@+id/num" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="輸入計算的值" android:textSize="25dp" android:singleLine="true" android:maxLength="10" /> <Button android:id="@+id/btn1" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="服務綁定" android:textSize="25dp" /> <Button android:id="@+id/btn2" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="解除綁定" android:textSize="25dp" /> <Button android:id="@+id/btn3" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="求 和" android:textSize="25dp" /> <EditText android:focusable="false" android:enabled="false" android:focusableInTouchMode="false" android:singleLine="true" android:id="@+id/result" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="結果" android:textSize="25dp" /> </LinearLayout>
預覽圖:
3.創建MyService類
提供服務的類
需要繼承Service
因為采用的是綁定服務的方式需要重寫create,bind,unbind以及destory生命周期方法
創建內部對象繼承Bind用于再bind生命周期中返回用于提供服務實現交互
/*
* 需要進行靜態注冊
* */
public class MyService extends Service {
//創建內部類對象
MyBinder myBinder=new MyBinder();
@Override
public void onCreate() {
Toast.makeText(this, "創建服務!", Toast.LENGTH_SHORT).show();
}
@Nullable
@Override
public IBinder onBind(Intent intent) {
Toast.makeText(this, "綁定服務!", Toast.LENGTH_SHORT).show();
//返回的對象是內部類對象
return myBinder;
}
@Override
public boolean onUnbind(Intent intent) {
Toast.makeText(this, "解綁服務!", Toast.LENGTH_SHORT).show();
return true;
}
@Override
public void onDestroy() {
Toast.makeText(this, "銷毀服務!", Toast.LENGTH_SHORT).show();
}
//內部類
class MyBinder extends Binder {
//返回Service對象
public MyService getService(){
return MyService.this;
}
//計算
//計算
public int getSum(int n){
int sum=0;
for(int i=1;i<=n;i++){
sum+=i;
}
return sum;
}
}
}
4.Activity
控件的定義
事件監聽器的創建
連接對象的創建,創建ServiceConnection對象,重寫服務創建的方法,獲取binder對象從而獲得Service對象
調用服務的時候和Activity跳轉的時候是類似的
調用Service服務的時候需要調用myBind就是調用了Service的內部類
public class MainActivity extends Activity {
//定義控件
Button btn1 = null;//綁定
Button btn2 = null;//解綁
Button btn3 = null;//求和
EditText num;//計算數據
EditText result;//結果
MyService.MyBinder myBinder = null;
MyService myService = null;
//創建連接對象
private ServiceConnection conn = new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
myBinder = (MyService.MyBinder) service;
myService = myBinder.getService();
}
@Override
public void onServiceDisconnected(ComponentName name) {
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//獲取控件
initView();
//求和
getResultNum();
}
//初始化控件
public void initView() {
//獲取開啟服務按鈕
btn1 = (Button) findViewById(R.id.btn1);
//獲取停止服務按鈕
btn2 = (Button) findViewById(R.id.btn2);
//求和
btn3 = (Button) findViewById(R.id.btn3);
//獲取要計算的數據
num = findViewById(R.id.num);
// 獲取結果值
result = findViewById(R.id.result);
//調用點擊事件
btn1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(getApplicationContext(), MyService.class);
intent.putExtra("num", num.getText().toString());
bindService(intent, conn, Service.BIND_AUTO_CREATE);
}
});
//解綁服務
btn2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
unbindService(conn);
}
});
}
private void getResultNum() {
btn3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (num.getText().toString().length() == 0) {
result.setText("");
Toast.makeText(getApplicationContext(), "要計算的結果不能為空!", Toast.LENGTH_SHORT).show();
}
else if (num.getText().toString().matches("\\d*")==false){
result.setText("");
Toast.makeText(getApplicationContext(), "要計算的數據必須為數字!", Toast.LENGTH_SHORT).show();
}
else {
result.setText(myBinder.getSum(Integer.parseInt(num.getText().toString()))+"");
}
}
});
}
}
效果圖
1.運行界面
2.服務創建服務綁定
首先先進行服務的創建再進行服務的綁定
3.服務解綁和銷毀
解綁服務會調用Unbind生命周期,又因為沒有別的可以解綁的服務,所以就會調用Destory生命周期方法
原文鏈接:https://blog.csdn.net/weixin_41957626/article/details/128929069
相關推薦
- 2023-06-17 詳解Flask數據庫的連接與使用_python
- 2022-11-16 docker-compose安裝及執行命令_docker
- 2023-01-01 C++?Boost.Signals2信號/槽概念_C 語言
- 2023-01-17 python中終止協程和異常處理方式_python
- 2022-04-11 canvas實現畫板功能
- 2022-12-04 React條件渲染實例講解使用_React
- 2022-03-30 圖文詳解nginx日志切割的實現_nginx
- 2023-07-15 react全局scss變量
- 最近更新
-
- 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同步修改后的遠程分支