android?studio后臺服務使用詳解
Service 是 Android 系統(tǒng)的服務組件,適用于開發(fā)沒有用戶界面且長時間在后臺運行的功能。通過本次試驗了解后臺服務的基本原理,掌握本地服務的使用方法。
1、創(chuàng)建一個Service服務用來完成簡單的求和和比較大小的數(shù)學運算。
2、創(chuàng)建Activity并調用該數(shù)學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="第一個數(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="第二個數(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 { ? ? //服務綁定 ? ? 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("兩個數(shù)默認值都為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)+"中最大的數(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>
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
Android?NDK開發(fā)(C語言--聯(lián)合體與枚舉)
這篇文章主要介紹了Android?NDK開發(fā)C語言聯(lián)合體與枚舉,共用體是一種特殊的數(shù)據(jù)類型,允許您在相同的內存位置存儲不同的數(shù)據(jù)類型。您可以定義一個帶有多成員的共用體,但是任何時候只能有一個成員帶有值。下面詳細介紹該內容,需要的朋友可以參考一下2021-12-12Android自定義ScrollView實現(xiàn)放大回彈效果
這篇文章主要為大家詳細介紹了Android自定義ScrollView實現(xiàn)放大回彈效果,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-05-05詳解Android WebView的input上傳照片的兼容問題
本篇文章主要介紹了詳解Android WebView的input上傳照片的兼容問題,非常具有實用價值,需要的朋友可以參考下2017-08-08android 類似微信的搖一搖功能實現(xiàn)思路及代碼
微信的搖一搖功能的出現(xiàn),讓彼此之間的距離有近了一步,本文也想實現(xiàn)以下微信的搖一搖功能,感興趣的朋友可以了解下啊,希望本人對你有所幫助2013-01-01Android自定義view實現(xiàn)帶header和footer的Layout
這篇文章主要介紹了Android自定義view實現(xiàn)帶header和footer的Layout,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習吧2023-02-02Android如何使用RecyclerView打造首頁輪播圖
這篇文章主要為大家詳細介紹了Android如何使用RecyclerView打造首頁輪播圖,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-02-02Android使用開源組件PagerBottomTabStrip實現(xiàn)底部菜單和頂部導航功能
這篇文章主要介紹了Android使用PagerBottomTabStrip實現(xiàn)底部菜單和頂部導航功能,本文給大家介紹的非常詳細,具有一定的參考借鑒價值,需要的朋友可以參考下2018-08-08