Android --- 有关Service的面试题

1.Service是什么

Service(服务)是一个可以在后台执行长时间运行操作而没有用户界面的应用组件。
注:Service是运行在主线程中的,不能进行耗时操作

2.Service和Thread的区别

Thread 程序执行的最小单元,我们可以用它执行一些异步操作,相对独立而Service依托于他所在的主线程上,并不独立

3.为什么说Service 是后台服务

因为 Service没有UI,用户无法感知 ,Service 和 Activity 是两个不同的线程,他们之间要通过IPC通信

4.开启Service 的两种方式

startService:
(1)定义一个类继承Service
(2)在Mainfest.xml 配置文件中配置该Service
(3)使用Context的startService(Intent) 方法启动Service,传进去的参数是Intent
(4)不使用时调用stopService(Intent)
当Service 被启动之后将无限的运行下去,即使Activity被销毁也不会停止,除非手动停止 Service

bindService:
Activity 和 Service 进行绑定,如果绑定全部取消之后,这个Service自动被销毁

代码实例:
MyService.java

package com.example.demo20220303;

import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;
import android.util.Log;

public class MyService extends Service {
    public MyService() {
    }

    public class MyBinder extends Binder {
        public MyService getService() {
            return MyService.this;
        }
    }
    private MyBinder myBinder = new MyBinder();

    @Override
    public void onCreate() {
        Log.i("onCreate:","onCreate ways");
        super.onCreate();
    }

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

    @Override
    public void onDestroy() {
        Log.i("onDestroy:","onDestroy ways");
        super.onDestroy();
    }

    @Override
    public IBinder onBind(Intent intent) {
        Log.i("onBind:","onBind ways");
        return myBinder;
    }

    @Override
    public boolean onUnbind(Intent intent) {
        Log.i("onUnbind:","onUnbind ways");
        return super.onUnbind(intent);
    }

    public String getString() {
        return "binder bangding!!!";
    }
}

MainActivity.java

package com.example.demo20220303;

import androidx.appcompat.app.AppCompatActivity;

import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.os.Trace;
import android.util.Log;
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 implements View.OnClickListener {
    private Button startBtn;
    private Button stopBtn;
    private Button bindBtn;
    private Button unBindBtn;
    private EditText editText;
    private TextView textView;
    private Intent startIntent;
    private boolean isBound = false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        initView();
    }
    public void initView() {
        startBtn = findViewById(R.id.main_start_btn);
        stopBtn = findViewById(R.id.main_stop_btn);
        bindBtn = findViewById(R.id.main_bind_btn);
        unBindBtn = findViewById(R.id.main_unbind_btn);
        editText = findViewById(R.id.main_et);
        textView = findViewById(R.id.main_tv);

        startBtn.setOnClickListener(this);
        stopBtn.setOnClickListener(this);
        bindBtn.setOnClickListener(this);
        unBindBtn.setOnClickListener(this);

    }
    private ServiceConnection connection = new ServiceConnection() {
        // 当服务连接的时候调用 onServiceConnected 获取 IBinder 中的公共方法进行使用
        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            isBound = true;
            MyService.MyBinder myBinder = (MyService.MyBinder) service;
            String text =  myBinder.getService().getString();
            Log.i("sss", text);
        }

        // onServiceDisconnected() 在连接正常关闭的情况下是不会被调用的.
        // 该方法只在Service 被破坏了或者被杀死的时候调用. 例如, 系统资源不足, 要关闭一些Services
        // 刚好连接绑定的 Service 是被关闭者之一,  这个时候onServiceDisconnected() 就会被调用.

        @Override
        public void onServiceDisconnected(ComponentName name) {
            isBound = false;
        }
    };

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.main_start_btn:
                //启动服务
                startIntent = new Intent(MainActivity.this,MyService.class);
                this.startService(startIntent);
                break;
            case R.id.main_stop_btn:
                //关闭服务
                this.stopService(startIntent);
                break;
            case R.id.main_bind_btn:
                Intent bindIntent = new Intent(MainActivity.this,MyService.class);
                this.bindService(bindIntent,connection,BIND_AUTO_CREATE);
                break;
            case R.id.main_unbind_btn:
                
                if(isBound) {
                    unbindService(connection);
                }
                break;
            default:
        }
    }
}

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    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"
    android:orientation="vertical">

    <com.example.demo20220303.view.MyTittleView
        android:id="@+id/customview_title"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

    </com.example.demo20220303.view.MyTittleView>

    <EditText
        android:id="@+id/main_et"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
    <TextView
        android:text=""
        android:gravity="center"
        android:textSize="30sp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/main_tv"/>

    <Button
        android:id="@+id/main_start_btn"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="启动服务" />

    <Button
        android:id="@+id/main_stop_btn"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="停止服务" />

    <Button
        android:id="@+id/main_bind_btn"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="绑定服务" />

    <Button
        android:id="@+id/main_unbind_btn"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="取消绑定服务" />

</LinearLayout >

点击启动服务按钮:

I/onCreate:: onCreate ways
I/onStartCommand:: onStartCommand ways

点击停止服务按钮:

I/onDestroy:: onDestroy ways

点击绑定服务按钮:

I/onCreate:: onCreate ways
I/onBind:: onBind ways
I/sss: binder bangding!!!

点击取消绑定服务按钮

I/onUnbind:: onUnbind ways
I/onDestroy:: onDestroy ways

5.Service的周期

推荐博主地址:
https://www.cnblogs.com/huihuizhang/p/7623760.html

与Activity类似,Service也有自己的生命周期函数,在不同的时刻,系统会调用对应的Service生命周期函数,不过与Activity声明周期相比,Service的声明周期更加简单,我们通过官方给出的一张图片来体会一下:
在这里插入图片描述

这里我们总结一下:

1). 被启动的服务的生命周期:如果一个Service被某个Activity 调用 Context.startService 方法启动,那么不管是否有Activity使用bindService绑定或unbindService解除绑定到该Service,该Service都在后台运行。如果一个Service被startService 方法多次启动,那么onCreate方法只会调用一次,onStart将会被调用多次(对应调用startService的次数),并且系统只会创建Service的一个实例(因此你应该知道只需要一次stopService调用)。该Service将会一直在后台运行,而不管对应程序的Activity是否在运行,直到被调用stopService,或自身的stopSelf方法。当然如果系统资源不足,android系统也可能结束服务。

2). 被绑定的服务的生命周期:如果一个Service被某个Activity 调用 Context.bindService 方法绑定启动,不管调用 bindService 调用几次,onCreate方法都只会调用一次,同时onStart方法始终不会被调用。当连接建立之后,Service将会一直运行,除非调用Context.unbindService 断开连接或者之前调用bindService 的 Context 不存在了(如Activity被finish的时候),系统将会自动停止Service,对应onDestroy将被调用。

3). 被启动又被绑定的服务的生命周期:如果一个Service又被启动又被绑定,则该Service将会一直在后台运行。并且不管如何调用,onCreate始终只会调用一次,对应startService调用多少次,Service的onStart便会调用多少次。调用unbindService将不会停止Service,而必须调用 stopService 或 Service的 stopSelf 来停止服务。

4). 当服务被停止时清除服务:当一个Service被终止(1、调用stopService;2、调用stopSelf;3、不再有绑定的连接(没有被启动))时,onDestroy方法将会被调用,在这里你应当做一些清除工作,如停止在Service中创建并运行的线程。

特别注意:

1、你应当知道在调用 bindService 绑定到Service的时候,你就应当保证在某处调用 unbindService 解除绑定(尽管 Activity 被 finish 的时候绑定会自      动解除,并且Service会自动停止);

2、你应当注意 使用 startService 启动服务之后,一定要使用 stopService停止服务,不管你是否使用bindService;

3、同时使用 startService 与 bindService 要注意到,Service 的终止,需要unbindService与stopService同时调用,才能终止 Service,不管 startService 与 bindService 的调用顺序,如果先调用 unbindService 此时服务不会自动终止,再调用 stopService 之后服务才会停止,如果先调用 stopService 此时服务也不会终止,而再调用 unbindService 或者 之前调用 bindService 的 Context 不存在了(如Activity 被 finish 的时候)之后服务才会自动停止;

4、当在旋转手机屏幕的时候,当手机屏幕在“横”“竖”变换时,此时如果你的 Activity 如果会自动旋转的话,旋转其实是 Activity 的重新创建,因此旋转之前的使用 bindService 建立的连接便会断开(Context 不存在了),对应服务的生命周期与上述相同。

5、在 sdk 2.0 及其以后的版本中,对应的 onStart 已经被否决变为了 onStartCommand,不过之前的 onStart 任然有效。这意味着,如果你开发的应用程序用的 sdk 为 2.0 及其以后的版本,那么你应当使用 onStartCommand 而不是 onStart。
生命周期方法说明

onStartCommand()
当另一个组件(如 Activity)通过调用 startService() 请求启动服务时,系统将调用此方法。一旦执行此方法,服务即会启动并可在后台无限期运行。 如果您实现此方法,则在服务工作完成后,需要由您通过调用 stopSelf() 或 stopService() 来停止服务。(如果您只想提供绑定,则无需实现此方法。)

onBind()
当另一个组件想通过调用 bindService() 与服务绑定(例如执行 RPC)时,系统将调用此方法。在此方法的实现中,您必须通过返回 IBinder 提供一个接口,供客户端用来与服务进行通信。请务必实现此方法,但如果您并不希望允许绑定,则应返回 null。

onCreate()
首次创建服务时,系统将调用此方法来执行一次性设置程序(在调用 onStartCommand() 或 onBind() 之前)。如果服务已在运行,则不会调用此方法。

onDestroy()
当服务不再使用且将被销毁时,系统将调用此方法。服务应该实现此方法来清理所有资源,如线程、注册的侦听器、接收器等。 这是服务接收的最后一个调用。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

梁同学与Android

你的鼓励将是我创作的最大动力!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值