網站首頁 編程語言 正文
Service 是 Android 系統的服務組件,適用于開發沒有用戶界面且長時間在后臺運行的功能。通過本次試驗了解后臺服務的基本原理,掌握本地服務的使用方法。
1、創建一個Service服務用來完成簡單的求和和比較大小的數學運算。
2、創建Activity并調用該數學Service
activity_main.xml
<?xml version="1.0" encoding="utf-8"?> <androidx.constraintlayout.widget.ConstraintLayout 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" ? ? tools:context=".MainActivity"> <LinearLayout ? ? android:layout_width="match_parent" ? ? android:layout_height="match_parent" ? ? android:orientation="vertical"> ? ? ? <LinearLayout ? ? ? ? android:layout_width="wrap_content" ? ? ? ? android:layout_height="wrap_content" ? ? ? ? android:orientation="horizontal"> ? ? ? ? ? <TextView ? ? ? ? ? ? android:layout_width="wrap_content" ? ? ? ? ? ? android:layout_height="wrap_content" ? ? ? ? ? ? android:text="第一個數:"> ? ? ? ? ? </TextView> ? ? ? ? ? <EditText ? ? ? ? ? ? android:layout_width="wrap_content" ? ? ? ? ? ? android:layout_height="wrap_content" ? ? ? ? ? ? android:ems="10" ? ? ? ? ? ? android:id="@+id/firstnum" ? ? ? ? ? ? android:inputType="number" ? ? ? ? ? ? android:digits="1234567890."> ? ? ? ? ? </EditText> ? ? </LinearLayout> ? ? ? <LinearLayout ? ? ? ? android:layout_width="wrap_content" ? ? ? ? android:layout_height="wrap_content" ? ? ? ? android:orientation="horizontal"> ? ? ? ? <TextView ? ? ? ? ? ? android:layout_width="wrap_content" ? ? ? ? ? ? android:layout_height="wrap_content" ? ? ? ? ? ? android:text="第二個數"/> ? ? ? ? <EditText ? ? ? ? ? ? android:layout_width="wrap_content" ? ? ? ? ? ? android:layout_height="wrap_content" ? ? ? ? ? ? android:ems="10" ? ? ? ? ? ? android:id="@+id/second" ? ? ? ? ? ? android:inputType="number" ? ? ? ? ? ? android:digits="1234567890."/> ? ? </LinearLayout> ? ? ? <Button ? ? ? ? android:layout_width="wrap_content" ? ? ? ? android:layout_height="wrap_content" ? ? ? ? android:id="@+id/bind" ? ? ? ? android:text="綁定"/> ? ? ? <Button ? ? ? ? android:layout_width="wrap_content" ? ? ? ? android:layout_height="wrap_content" ? ? ? ? android:id="@+id/add" ? ? ? ? android:text="求和"/> ? ? ? <Button ? ? ? ? android:layout_width="wrap_content" ? ? ? ? android:layout_height="wrap_content" ? ? ? ? android:id="@+id/compare" ? ? ? ? android:text="比較大小"/> ? ? ? <Button ? ? ? ? android:layout_width="wrap_content" ? ? ? ? android:layout_height="wrap_content" ? ? ? ? android:id="@+id/unbind" ? ? ? ? android:text="解除綁定"/> ? ? <TextView ? ? ? ? android:layout_width="wrap_content" ? ? ? ? android:layout_height="wrap_content" ? ? ? ? android:id="@+id/out"/> ? ? </LinearLayout> ? ? </androidx.constraintlayout.widget.ConstraintLayout>
MathService.java
package com.example.serviceexperiment;
?
import android.app.Service;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Binder;
import android.os.IBinder;
import android.widget.Toast;
?
public class MathService extends Service {
? ? //服務綁定
? ? final IBinder mBinder=new LocalBinder();
? ? public class LocalBinder extends Binder {
? ? ? ? MathService getService() {
? ? ? ? ? ? return MathService.this;
? ? ? ? }
? ? }
? ? public MathService() {
? ? }
?
? ? @Override
? ? public IBinder onBind(Intent intent) {
? ? ? ? // TODO: Return the communication channel to the service.
? ? ? ? return mBinder;
? ? }
? ? public boolean onUnbind(Intent intent){
? ? ? ? Toast.makeText(this,"取消本地綁定",Toast.LENGTH_SHORT).show();
? ? ? ? return false;
? ? }
? ? public Double Add(Double a,Double b){
? ? ? ? return a+b;
? ? }
? ? public boolean Compare(Double a,Double b){
? ? ? ? if(a>b){
? ? ? ? ? ? return true;
? ? ? ? };
? ? ? ? return false;
? ? }
}
MainActicity.java
package com.example.serviceexperiment;
?
import androidx.appcompat.app.AppCompatActivity;
?
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.text.Editable;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
?
public class MainActivity extends AppCompatActivity {
? ? private MathService mathService;
? ? private boolean isBound=false;
? ? TextView labelView;
? ? EditText firstnum;
? ? EditText secondnum;
? ? @Override
? ? protected void onCreate(Bundle savedInstanceState) {
? ? ? ? super.onCreate(savedInstanceState);
? ? ? ? setContentView(R.layout.activity_main);
? ? ? ? labelView=(TextView)findViewById(R.id.out);
? ? ? ? labelView.setText("兩個數默認值都為0");
? ? ? ? firstnum=(EditText)findViewById(R.id.firstnum);
? ? ? ? secondnum=(EditText)findViewById(R.id.second);
? ? ? ? Button bindButton=(Button)findViewById(R.id.bind);
? ? ? ? Button unbindButton=(Button)findViewById(R.id.unbind);
? ? ? ? Button addButton=(Button)findViewById(R.id.add);
? ? ? ? Button compareButton=(Button)findViewById(R.id.compare);
? ? ? ? bindButton.setOnClickListener(new View.OnClickListener() {//綁定按鈕
? ? ? ? ? ? @Override
? ? ? ? ? ? public void onClick(View view) {
? ? ? ? ? ? ? ? if(!isBound){
? ? ? ? ? ? ? ? ? ? final Intent serviceIntent=new Intent(MainActivity.this,MathService.class);
? ? ? ? ? ? ? ? ? ? bindService(serviceIntent,mConnection,Context.BIND_AUTO_CREATE);
? ? ? ? ? ? ? ? ? ? isBound=true;
? ? ? ? ? ? ? ? ? ? Toast.makeText(MainActivity.this,"本地綁定:MathService",Toast.LENGTH_SHORT).show();
? ? ? ? ? ? ? ? }
? ? ? ? ? ? }
? ? ? ? });
? ? ? ? unbindButton.setOnClickListener(new View.OnClickListener() {//解綁按鈕
? ? ? ? ? ? @Override
? ? ? ? ? ? public void onClick(View view) {
? ? ? ? ? ? ? ? if(isBound){
? ? ? ? ? ? ? ? ? ? isBound=false;
? ? ? ? ? ? ? ? ? ? unbindService(mConnection);
? ? ? ? ? ? ? ? ? ? mathService=null;
?
? ? ? ? ? ? ? ? ? ? Toast.makeText(MainActivity.this,"取消本地綁定:MathService",Toast.LENGTH_SHORT).show();
? ? ? ? ? ? ? ? }
? ? ? ? ? ? }
? ? ? ? });
? ? ? ? addButton.setOnClickListener(new View.OnClickListener() {//調用服務加法
? ? ? ? ? ? @Override
? ? ? ? ? ? public void onClick(View view) {
?
? ? ? ? ? ? ? ? if(mathService==null){
? ? ? ? ? ? ? ? ? ? Toast.makeText(MainActivity.this,"未綁定服務:MathService",Toast.LENGTH_SHORT).show();
? ? ? ? ? ? ? ? ? ? return;
? ? ? ? ? ? ? ? }
?
? ? ? ? ? ? ? ? String firsttext=firstnum.getText().toString();
? ? ? ? ? ? ? ? Double a= 0.0;
? ? ? ? ? ? ? ? if(firsttext.length()!=0){
? ? ? ? ? ? ? ? ? ? a=Double.parseDouble(firsttext);
? ? ? ? ? ? ? ? }
?
? ? ? ? ? ? ? ? String secondtext=secondnum.getText().toString();
? ? ? ? ? ? ? ? Double b= 0.0;
? ? ? ? ? ? ? ? if(secondtext.length()!=0){
? ? ? ? ? ? ? ? ? ? b=Double.parseDouble(secondtext);
? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? Double result=mathService.Add(a,b);
? ? ? ? ? ? ? ? String msg=String.valueOf(a)+"+"+String.valueOf(b)+"="+String.valueOf(result);
? ? ? ? ? ? ? ? labelView.setText(msg);
?
? ? ? ? ? ? }
? ? ? ? });
? ? ? ? compareButton.setOnClickListener(new View.OnClickListener() {
? ? ? ? ? ? @Override
? ? ? ? ? ? public void onClick(View view) {
? ? ? ? ? ? ? ? if(mathService==null){
? ? ? ? ? ? ? ? ? ? Toast.makeText(MainActivity.this,"未綁定服務:MathService",Toast.LENGTH_SHORT).show();
? ? ? ? ? ? ? ? ? ? return;
? ? ? ? ? ? ? ? }
?
? ? ? ? ? ? ? ? String firsttext=firstnum.getText().toString();
? ? ? ? ? ? ? ? Double a= 0.0;
? ? ? ? ? ? ? ? if(firsttext.length()!=0){
? ? ? ? ? ? ? ? ? ? a=Double.parseDouble(firsttext);
? ? ? ? ? ? ? ? }
?
? ? ? ? ? ? ? ? String secondtext=secondnum.getText().toString();
? ? ? ? ? ? ? ? Double b= 0.0;
? ? ? ? ? ? ? ? if(secondtext.length()!=0){
? ? ? ? ? ? ? ? ? ? b=Double.parseDouble(secondtext);
? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? boolean result=mathService.Compare(a,b);
? ? ? ? ? ? ? ? String msg;
? ? ? ? ? ? ? ? if(result){
?
? ? ? ? ? ? ? ? ? ? msg=String.valueOf(a)+"和"+String.valueOf(b)+"中最大的數是"+String.valueOf(a);
? ? ? ? ? ? ? ? }else{
? ? ? ? ? ? ? ? ? ? msg=String.valueOf(a)+"和"+String.valueOf(b)+"中最大的數是"+String.valueOf(b);
? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? labelView.setText(msg);
? ? ? ? ? ? }
? ? ? ? });
? ? }
? ? private ServiceConnection mConnection=new ServiceConnection() {//綁定
? ? ? ? @Override
? ? ? ? public void onServiceConnected(ComponentName componentName, IBinder iBinder) {
? ? ? ? ? ? mathService=((MathService.LocalBinder)iBinder).getService();
? ? ? ? }
?
? ? ? ? @Override
? ? ? ? public void onServiceDisconnected(ComponentName componentName) {
? ? ? ? ? ? mathService=null;
? ? ? ? }
? ? };
}
AndroidMainfest.xml中加入
<service android:name=".MathService" android:enabled="true" android:exported="true"></service>
原文鏈接:https://blog.csdn.net/qq_57171795/article/details/125195393
相關推薦
- 2023-03-19 匯編語言LDR指令和LDR偽指令詳解_匯編語言
- 2022-08-04 Python并發編程之IO模型_python
- 2022-07-15 Python打印數據類型的全過程_python
- 2022-06-26 C++中declspec(dllexport)和declspec(dllimport)?的用法介紹_
- 2022-03-30 用Python判斷奇偶數示例_python
- 2022-09-26 C++實現頁面的緩沖區管理器_C 語言
- 2022-11-03 Python日期與時間模塊(datetime+time+Calendar+dateuil?)相關使用
- 2022-05-13 三分鐘搞懂react-hooks及實例代碼_React
- 最近更新
-
- 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同步修改后的遠程分支