android?studio后臺(tái)服務(wù)使用詳解
Service 是 Android 系統(tǒng)的服務(wù)組件,適用于開發(fā)沒(méi)有用戶界面且長(zhǎng)時(shí)間在后臺(tái)運(yùn)行的功能。通過(guò)本次試驗(yàn)了解后臺(tái)服務(wù)的基本原理,掌握本地服務(wù)的使用方法。
1、創(chuàng)建一個(gè)Service服務(wù)用來(lái)完成簡(jiǎn)單的求和和比較大小的數(shù)學(xué)運(yùn)算。
2、創(chuàng)建Activity并調(diào)用該數(shù)學(xué)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="第一個(gè)數(shù):"> ? ? ? ? ? </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="第二個(gè)數(shù)"/> ? ? ? ? <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 {
? ? //服務(wù)綁定
? ? 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("兩個(gè)數(shù)默認(rèn)值都為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() {//調(diào)用服務(wù)加法
? ? ? ? ? ? @Override
? ? ? ? ? ? public void onClick(View view) {
?
? ? ? ? ? ? ? ? if(mathService==null){
? ? ? ? ? ? ? ? ? ? Toast.makeText(MainActivity.this,"未綁定服務(wù):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,"未綁定服務(wù):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)+"中最大的數(shù)是"+String.valueOf(a);
? ? ? ? ? ? ? ? }else{
? ? ? ? ? ? ? ? ? ? msg=String.valueOf(a)+"和"+String.valueOf(b)+"中最大的數(shù)是"+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>
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Android基于Toolbar實(shí)現(xiàn)頂部標(biāo)題欄及后退鍵
這篇文章主要介紹了Android基于Toolbar實(shí)現(xiàn)頂部標(biāo)題欄及后退鍵,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-09-09
Android Studio中使用lambda表達(dá)式的方法
這篇文章主要介紹了Android Studio中使用lambda表達(dá)式的方法,需要的朋友可以參考下2017-06-06
Android冷啟動(dòng)實(shí)現(xiàn)app秒開的實(shí)現(xiàn)代碼
本篇文章主要介紹了Android冷啟動(dòng)實(shí)現(xiàn)app秒開的實(shí)現(xiàn)代碼,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-08-08
Android中二維碼的掃描和生成(使用zxing庫(kù))
ZXing是一個(gè)開放源碼的,用Java實(shí)現(xiàn)的多種格式的1D/2D條碼圖像處理庫(kù),它包含了聯(lián)系到其他語(yǔ)言的端口,下面這篇文章主要給大家介紹了關(guān)于Android中二維碼掃描和生成的相關(guān)資料,主要使用的zxing庫(kù),需要的朋友可以參考下2022-09-09
Android利用代碼控制設(shè)備上其他音樂(lè)播放器的方法
這篇文章主要給大家介紹了關(guān)于Android利用代碼如何控制設(shè)備上其他音樂(lè)播放器的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2018-06-06
Android開發(fā)實(shí)現(xiàn)長(zhǎng)按返回鍵彈出關(guān)機(jī)框功能
這篇文章主要介紹了Android開發(fā)實(shí)現(xiàn)長(zhǎng)按返回鍵彈出關(guān)機(jī)框功能,涉及Android針對(duì)長(zhǎng)按事件的響應(yīng)與處理相關(guān)操作技巧,需要的朋友可以參考下2017-09-09
Android ViewPager畫廊效果詳解及實(shí)例
這篇文章主要介紹了Android ViewPager畫廊效果詳解及實(shí)例的相關(guān)資料,這里提供實(shí)例代碼及實(shí)現(xiàn)效果圖,具有參考價(jià)值,需要的朋友可以參考下2016-12-12
Android Studio 引用外部依賴時(shí)報(bào)錯(cuò)的解決方法
這篇文章主要介紹了Android Studio報(bào)錯(cuò)Unable to resolve dependency for':app@release/compileClasspath':無(wú)法引用任何外部依賴的解決辦法,需要的朋友可以參考下2018-01-01

