Android开发案例2–标准方式绑定方式开启停止服务,完成加法计算
前言
1.服务
当使用startService()方法启动服务时,执行的生命周期方法为onCreate(),onStartCommand(),然后服务处于运行状态,直到自身调用stopSelf()方法或者其他组件调用stopService()方法时服务停止,最终被系统销毁。当使用bindService()方法启动服务时,执行的生命周期方法为onCreate()、onBind(),然后服务处于运行状态,直到调用unBindService()方法时,服务被解绑调用onUnbind()方法,最终被销毁。
2.1Manifest文件
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.a15676.myservice">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service
android:name=".MyService"
android:enabled="true"
android:exported="true" />
<service
android:name=".bindService"
android:enabled="true"
android:exported="true"></service>
</application>
</manifest>
2.2MainActivity
package com.example.a15676.myservice;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.IBinder;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
private EditText et1;
private EditText et2;
private TextView tv3;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
et1= findViewById(R.id.tv1);
et2=findViewById(R.id.tv2);
tv3=findViewById(R.id.tv3);
}
public void start(View view)
{
Intent intent=new Intent(this,MyService.class);
startService(intent);
}
public void stop(View view)
{
Intent intent=new Intent(this,MyService.class);
stopService(intent);
}
///
//服务连接
private class MyConn implements ServiceConnection {
@Override
public void onServiceDisconnected(ComponentName name) {
System.out.println("服务失去绑定");
}
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
System.out.println("服务被成功绑定");
binder = (bindService.MyBinder) service;
}
}
private MyConn myConn;
private bindService.MyBinder binder;
//绑定
public void bind(View v) {
Intent intent = new Intent(this, bindService.class);
myConn=new MyConn();
bindService(intent, myConn, Context.BIND_AUTO_CREATE);
}
//计算
public void calculate(View v) {
int a = Integer.parseInt(et1.getText().toString());
int b = Integer.parseInt(et2.getText().toString());
int num=binder.callMethodInService(a,b);
tv3.setText(String.valueOf(num));
System.out.println("相加之和为:" +num);
}
//解绑
public void unbind(View v) {
unbindService(myConn);
}
}
2.3界面设计文件
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.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"
android:background="@drawable/b2"
android:orientation="vertical"
tools:context=".MainActivity">
<LinearLayout
android:layout_width="276dp"
android:layout_height="154dp"
android:layout_marginLeft="16dp"
android:layout_marginStart="16dp"
android:orientation="vertical"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<EditText
android:id="@+id/tv1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text=""
android:textSize="20dp" />
<EditText
android:id="@+id/tv2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text=""
android:textSize="20dp" />
<TextView
android:id="@+id/tv3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text=""
android:textSize="25dp" />
</LinearLayout>
<LinearLayout
android:layout_width="215dp"
android:layout_height="102dp"
android:layout_marginBottom="8dp"
android:orientation="vertical"
app:layout_constraintBottom_toTopOf="@+id/linearLayout"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent">
<Button
android:id="@+id/button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="start"
android:text="标准方式开启服务"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
tools:layout_editor_absoluteY="55dp" />
<Button
android:id="@+id/button2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="stop"
android:text="停止服务"
app:layout_constraintEnd_toEndOf="parent"
tools:layout_editor_absoluteY="117dp" />
</LinearLayout>
<LinearLayout
android:id="@+id/linearLayout"
android:layout_width="219dp"
android:layout_height="166dp"
android:layout_marginBottom="8dp"
android:orientation="vertical"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.503"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.768">
<Button
android:id="@+id/button3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="bind"
android:text="绑定方式开启服务" />
<Button
android:id="@+id/button4"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="calculate"
android:text="加法计算" />
<Button
android:id="@+id/button5"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="unbind"
android:text="取消绑定" />
</LinearLayout>
</android.support.constraint.ConstraintLayout>
2.4标准方式
package com.example.a15676.myservice;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
public class MyService extends Service {
public MyService() {
}
@Override
public IBinder onBind(Intent intent) {
// TODO: Return the communication channel to the service.
//throw new UnsupportedOperationException("Not yet implemented");
return null;
}
@Override
public void onCreate() {
System.out.println("服务被创建了onCreate");
super.onCreate();
}
@Override
public void onDestroy() {
System.out.println("服务被销毁了onDestroy");
super.onDestroy();
}
@Override
public int onStartCommand(Intent intent,int flags,int startId) {
System.out.println("服务开启onStartCommand");
return super.onStartCommand(intent,flags,startId);
}
}
2.5绑定方式
package com.example.a15676.myservice;
import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;
public class bindService extends Service {
public bindService() {
}
private MyBinder myBinder;
public class MyBinder extends Binder{
public int callMethodInService(int a,int b){
int x;
x=sum(a,b);
return x;
}
}
@Override
public IBinder onBind(Intent intent) {
myBinder=new MyBinder();
System.out.println("服务被绑定");
return myBinder;
}
@Override
public void onCreate() {
System.out.println("服务被创建");
super.onCreate();
}
@Override
public void onDestroy() {
System.out.println("服务被销毁");
super.onDestroy();
}
@Override
public int onStartCommand(Intent intent,int flags,int startId) {
System.out.println("服务被开启");
return super.onStartCommand(intent,flags,startId);
}
@Override
public boolean onUnbind(Intent intent) {
System.out.println("服务被解绑");
return super.onUnbind(intent);
}
public int sum(int a,int b){
return a+b;
}
}
3运行
(1)点击运行,可看到如下界面
(2)点击“标准方式开启服务”按钮
(3)点击“标准方式停止服务”按钮
(4)点击 “绑定方式开启服务”按钮
(5)点击 “加法计算”按钮
输入5,5 点击按钮后,即可求出两数之和10
(6)点击 “取消绑定“按钮
好了,结束