Android 启动和停止服务

时间:2021-12-19 04:48:04

本篇文章还是在Android 服务(service)的基础用法的基础上进行修改,定义好了服务之后,下面就来看一下如何去启动和停止这个服务,启动和停止的方法当然你也不会陌生,主要是借助Intent来实现的,下面我们在ServiceTest项目中尝试去启动和停止MyService这个服务.

activity_main.xml中的代码,如下:

<?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"
tools:context="com.example.servicetest.MainActivity">

<Button
android:id="@+id/start_service"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Start Service"
android:textAllCaps="false"/>

<Button
android:id="@+id/stop_service"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Stop Service"
android:textAllCaps="false"
app:layout_constraintTop_toBottomOf="@id/start_service"/>

</android.support.constraint.ConstraintLayout>
在布局文件中加入了两个按钮,分别是用于启动和停止服务的.

MainActivity.java中的代码,如下:

public class MainActivity extends AppCompatActivity implements View.OnClickListener{

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button startService = findViewById(R.id.start_service);
Button stopService = findViewById(R.id.stop_service);
startService.setOnClickListener(this);
stopService.setOnClickListener(this);

}

@Override
public void onClick(View view) {
switch (view.getId()){
case R.id.start_service:
Intent startIntent = new Intent(this,MyService.class);
startService(startIntent);//启动服务
break;
case R.id.stop_service:
Intent stopIntent = new Intent(this,MyService.class);
stopService(stopIntent);//停止服务
break;
default:
break;
}
}
}
可以看到,这里在onCreate()方法中分别获取到了Start Service按钮和Stop Service按钮的实例,并给它们注册了点击事件,然后在Start Service按钮的点击事件里,我们构建了一个Intent对象,并调用startService()方法来启动MyService服务,在Stop Service按钮的点击事件里,我们同样构建了Intent对象,并调用stopService()方法来停止MyService这个服务,startService()和stopService()方法都是定义在Context类中,所以我们在活动里可以直接调用这两个方法,注意,这里完全是由活动来决定服务如何停止的,如果没有点击Stop Service按钮,服务就会一直处于运行状态,那服务有没有什么办法让自己停止下来呢?当然是可以的,只需要在MyService的任何一个位置调用stopSelf()方法就能让这个服务停止下来了.

那么接下来又有一个问题,我们如何才能证实服务已经成功启动或者停止了呢?最简单的方法就是在MyService的几个方法中加入打印日志,如下:

public class MyService extends Service {

private static final String TAG = "MyService";
public MyService() {
}

@Override
public IBinder onBind(Intent intent) {
// TODO: Return the communication channel to the service.
throw new UnsupportedOperationException("Not yet implemented");
}

@Override
public void onCreate() {
super.onCreate();
Log.d(TAG, "onCreate: executed");
}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.d(TAG, "onStartCommand: executed");
return super.onStartCommand(intent, flags, startId);
}

@Override
public void onDestroy() {
super.onDestroy();
Log.d(TAG, "onDestroy: executed");
}
}
现在可以运行一下程序来进行测试,程序的主界面如图:

Android 启动和停止服务

点击一下Start Service按钮,观察logcat中的打印日志,如下:

Android 启动和停止服务

MyService中的onCreate()和onStartCommand()方法都执行了,说明这个服务确实已经启动成功了,然后再点击一下Stop Service按钮,观察logcat中的打印日志,如下:

Android 启动和停止服务

由此证明,MyService确实已经成功停止下来了.

其实onCreate()方法是在服务第一次创建的时候调用的,而onStartCommand()方法则在每次启动服务的时候都会调用,由于刚才我们是第一次点击Start Service按钮,服务此时还未创建,所以两个方法都会执行,之后如果你再连续多点击几次Start Service按钮,你就会发现只有onStartCommand()方法会得到执行了.