android|android studio后台服务使用详解

Service 是 Android 系统的服务组件,适用于开发没有用户界面且长时间在后台运行的功能。通过本次试验了解后台服务的基本原理,掌握本地服务的使用方法。
【android|android studio后台服务使用详解】1、创建一个Service服务用来完成简单的求和和比较大小的数学运算。
2、创建Activity并调用该数学Service
android|android studio后台服务使用详解
文章图片
android|android studio后台服务使用详解
文章图片

android|android studio后台服务使用详解
文章图片
android|android studio后台服务使用详解
文章图片

activity_main.xml


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() {} @Overridepublic 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; @Overrideprotected 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() {//绑定按钮@Overridepublic 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() {//解绑按钮@Overridepublic 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() {//调用服务加法@Overridepublic 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() {@Overridepublic 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() {//绑定@Overridepublic void onServiceConnected(ComponentName componentName, IBinder iBinder) {mathService=((MathService.LocalBinder)iBinder).getService(); } @Overridepublic void onServiceDisconnected(ComponentName componentName) {mathService=null; }}; }

AndroidMainfest.xml中加入

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

    推荐阅读