Android之Service

菜鸟教程Service详解

一、Service简介:

Service是一种后台服务机制,允许在没有用户界面的情况下,使程序能够长时间在后台运行。
Service是四大组件之一,适用于开发无UI界面、长时间后台运行、做一些用时比较长的操作。
在这里插入图片描述

二、Service创建:

第一步:创建类继承Service类

package com.example.myapplication;

import android.app.Service;
import android.content.Intent;
import android.os.IBinder;

public class MyService extends Service {
    public MyService() {
    }
    //当Service第一次被创建后立即回调该方法,该方法在整个生命周期中只会调用一次
    @Override
    public void onCreate() {
        super.onCreate();
    }
    //当该Service被关闭/销毁时会回调该方法。
    @Override
    public void onDestroy() {
        super.onDestroy();
    }
    //客户端通过bindService()方式启动服务时执行该方法。该方法返回一个Ibinder接口的实现类对象,应用程序可通过该对象与Service组件通信
    @Override
    public IBinder onBind(Intent intent) {
        throw new UnsupportedOperationException("Not yet implemented");
    }
    //客户端调用unBindService()方法断开该Service上绑定的所有客户端连接时将会回调该方法
    @Override
    public boolean onUnbind(Intent intent) {
        return super.onUnbind(intent);
    }
    /*每次客户端调用startService(Intent)方法启动该Service时都会回调该方法。系统可能处于内存不足的缘故摧毁这个service,其中返回值用来定义系统该如何重建service
        Intent:从startService(Intent)传来的Intent。
        flags:启动请求的附加数据,表示启动服务的方式,取值有三个:
            0:通过startService()方法启动Service时传入
            START_FLAG_REDELIVERY:代表onStartCommand方法的返回值为START_REDELIVER_INTENT,而且在上一次服务被杀死前会去调用stopSelf方法停止服务
            START_FLAG_RETRY:代表当onStartCommand调用后一直没有返回值时,会尝试重新去调用onStartCommand()
        startId:指明当前服务的唯一ID
        返回值return:
            START_NOT_STICKY:表明不要重建service,除非显式调用
            START_STICKY: 表明需要重建service, 并在重建service之后调用onStartCommand()方法, 传递给该方法的intent为null
            START_REDELIVER_INTENT: 表明需要重建service, 并在重建service之后调用onStartCommand()方法, 传递给该方法的intent为service被摧毁之前接收到的最后一个intent
    */
    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        return super.onStartCommand(intent, flags, startId);
    }
}

第二步:在AndroidManifest.xml文件中注册该Service

<service
    android:name=".MyService"
    android:enabled="true"
    android:exported="true">
</service>

三、Service的调用:

Activity等其它组件不同的是,Service不能自己主动运行,需要调用相应的方法来启动。

方式一:startService()方法启动

适用于客户端与服务之间没有调用交互的情况。

1.特点:
  1. 客户端与Service之间没有关联,即使客户端退出了,Service仍然运行
  2. 客户端和Service之间无法通信,无法进行数据交互
  3. 如果不调用stopService()或stopSelf(),这种方式开启的服务会长期在后台运行。
    在这里插入图片描述
    首次启动会创建一个Service实例,依次调用onCreate()和onStartCommand()方法,此时Service 进入运行状态,如果再次调用startService()启动Service,将不会再创建新的Service对象,也不会调用onCreate()方法,系统会直接复用前面创建的Service对象,调用它的onStartCommand()方法。
2.显式启动:

Service和调用服务的组件在同一个应用程序中,可以使用显式启动或隐式启动。

Intent intent = new Intent(this, MyService.class);
startService(intent);
3.隐式启动:

若服务和调用服务的组件在不同的应用程序中,则只能使用隐式启动。

Intent intent = new Intent();
intent.setAction("com.a.b.myservice");
startService(intent);
<service
    android:name=".MyService"
    android:enabled="true"
    android:exported="true">
    <!--声明该Service可以被哪个Intent隐式启动-->
    <intent-filter>
        <action android:name=""/>
    </intent-filter>
</service>

方式二:bindService()方法启动

适用于客户端与服务之间需要传递参数或方法调用的情况。

1.特点:
  1. 客户端与Service绑定在一起,所有的客户端退出,Service也就停止。 但是只要有一个客户端存在,那么Service继续运行。
  2. 客户端和Service之间可以通信,可以进行数据交互。

在这里插入图片描述

当首次使用 bindService() 绑定一个Service时,系统会实例化一个Service实例,并调用其onCreate()和onBind()方法,然后调用者就可以通过IBinder和Service进行交互了,此后如果再次使用bindService绑定Service,系统不会创建新的Sevice7实例,也不会再调用onBind()方法,只会直接把IBinder对象传递给其他后来增加的客户端。
如果我们解除与服务的绑定,只需调用 unbindService() ,此时onUnbind和onDestory方法将会被调用。

2.显式启动:
  1. 创建类实现ServiceConnection:
class MyConn implements ServiceConnection {
    /*成功绑定到Service时调用的方法
    	iBinder是Service中onBind()方法的返回值,可以通过强转转成IBinder接口的实现类
    */
    @Override
    public void onServiceConnected(ComponentName name, IBinder ibinder){
        //MyBinder是service中继承Binder的内部类
        MyBinder myBinder=(MyBinder)ibinder;
    }

    //当服务意外失去连接时调用的方法
    @Override
    public void onServiceDisconnected(ComponentName componentName){
    }
}
  1. 创建Intent对象,调用Context的bindService方法:
Intent intent = new Intent(this,MyService.class);
MyConn myconn=new MyConn();
/**
 * bindService (Intent service,ServiceConnection conn,int flags)
 * 参数说明:
 *    service:该Intent对象用于指定要启动的service
 *    conn:该参数是一个serviceConnection对象,用于监听客户端与service之间的连接情况,当调用者与Service连接成功时将回调serviceConnection接口实现类对象conn的onServiceConnected(ComponentName name,IBinder service)方法,如果意外断开将回调onServiceDisConnected(ComponentName name)方法
 *    flags:指定绑定时是否自动创建service,可指定为0(不自动创建)或BIND_AUTO_CREATE(自动创建)。
 */
bindService(intent,myconn,BIND_AUTO_CREATE);
3.交换数据的思路:
(1)Activity向Service传递数据:

Activity中:

//第一个参数intent就是向Service中传递的数据,我们可以把想要传递给Service的数据封装在该intent对象中
bindService (Intent intent,ServiceConnection conn,int flags)

Service中:

//Service生命周期中的onBind()方法的intent参数就是Activity传给Service的数据
public IBinder onBind(Intent intent) {
	//获取Intent对象中封装的Activity传给Service的数据
	int n = intent.getIntExtra("n", 0);
}
(2)Service向Activity传递数据:

Service中:

public class MyService extends Service {
    //sum是Service发送给Activity的数据
    private int sum;
    //数据交互内部类,用于将sum数据传给Activity(通过在Activity中调用getSum方法的形式获得sum)
    class MyBinder extends Binder {
        //该方法用于获取Service传给Acyivity的数据
        public int getSum(){
            return sum;
        }
    }
    //Service生命周期方法,该方法返回一个MyBinder对象,MyBinder对象的getSum()方法能获取Service传给Acyivity的数据
    @Override
    public IBinder onBind(Intent intent) {
        int n = intent.getIntExtra("n", 0);
        calculate(n);
        return new MyBinder();
    }
}

Activity中:

public class MainActivity extends AppCompatActivity {
	//Activity中的该内部类对象用来与Service进行通信
    ServiceConnection MyConn = new ServiceConnection() {
        /*成功绑定到Service时调用的方法
             IBinder:就是Service生命周期onBind方法的返回值,通过该变量调用getSum方法就能够获取Service传给Activity的数据
         */
        @Override
        public void onServiceConnected(ComponentName componentName, IBinder iBinder) {
            //强转成MyService中的MyBinder类型,方便调用MyBinder的getSum()方法获取Service传给ACtivity的数据
            MyService.MyBinder mb= (MyService.MyBinder) iBinder;
            //sum就是Service向Activity传递的数据
            int sum = mb.getSum();
        }

        @Override
        public void onServiceDisconnected(ComponentName componentName) {

        }
    };
}

四、Service生命周期:

当程序使用startService()和stopService()启动、关闭服务时,服务与调用者之间基本不存在太多关联,也无法与访问者进行通信、数据交互等。

如果服务需要与调用者进行方法调用和数据交互时,应该使用bindService()和unbindService()启动、关闭服务。

五、案例:bindService方式启动

编写一个Service类,里面有求一个1~n之和的方法。
编写一个Activity类,里面有绑定、解除绑定、输入一个上届数字n的输入框、计算和的按钮、显示结果的输入框。

1.MainActivity.java:

向Service发送n并获取Service中的计算结果。

package com.example.myapplication;

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.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {
    Button bind;
    Button unbind;
    Button btn;
    EditText edit;
    TextView text;
    //初始未绑定Service
    boolean flag = false;
    //Activity与Service传送的数据1
    Intent intent;
    //Activity中的该内部类对象用来与Service进行通信
    ServiceConnection MyConn = new ServiceConnection() {
        /*成功绑定到Service时调用的方法
             IBinder:就是Service生命周期onBind方法的返回值,通过该变量调用getSum方法就能够获取Service传给Activity的数据
         */
        @Override
        public void onServiceConnected(ComponentName componentName, IBinder iBinder) {
            //强转成MyService中的MyBinder类型,方便调用MyBinder的getSum()方法获取Service传给ACtivity的数据
            MyService.MyBinder mb= (MyService.MyBinder) iBinder;
            //sum就是Service向Activity传递的数据
            int sum = mb.getSum();
            text.setText(sum+"");
        }
        /*绑定失败时执行的方法
         */
        @Override
        public void onServiceDisconnected(ComponentName componentName) {

        }
    };
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        bind = findViewById(R.id.bind);
        unbind = findViewById(R.id.unbind);
        btn = findViewById(R.id.btn);
        edit = findViewById(R.id.edit);
        text = findViewById(R.id.text);
        intent = new Intent(this,MyService.class);

        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //点击bind按钮
                unbind.callOnClick();
                //传值
                intent.putExtra("n",Integer.valueOf(edit.getText().toString()));
                //点击unbind按钮1111000
                bind.callOnClick();
            }
        });

        bind.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (flag == false){
                    flag = true;
                    //bindService方式绑定Service
                    bindService(intent,MyConn,BIND_AUTO_CREATE);
                    Toast.makeText(MainActivity.this,"绑定Service成功",Toast.LENGTH_SHORT).show();
                }
            }
        });

        unbind.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(flag == true){
                    flag = false;
                    //解绑Service,不再与Service交互
                    unbindService(MyConn);
                    Toast.makeText(MainActivity.this, "解绑Service成功", Toast.LENGTH_SHORT).show();
                }
            }
        });

    }
}

2.MyService.java:

获取Activity中的n值并将计算结果传给Activity。

package com.example.myapplication;

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

import androidx.annotation.Nullable;

public class MyService extends Service {
    //sum是Service发送给Activity的数据
    private int sum;
    //数据交互内部类,用于将sum数据传给Activity(通过在Activity中调用getSum方法的形式获得sum)
    class MyBinder extends Binder {
        //该方法用于获取Service传给Acyivity的数据
        public int getSum(){
            return sum;
        }
    }

    //当Service第一次被创建后立即回调该方法,该方法在整个生命周期中只会调用一次
    @Override
    public void onCreate() {
        super.onCreate();
    }

    //Service生命周期方法,该方法返回一个MyBinder对象,MyBinder对象的getSum()方法能获取Service传给Acyivity的数据
    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        int n = intent.getIntExtra("n", 0);
        calculate(n);
        return new MyBinder();
    }

    @Override
    public boolean onUnbind(Intent intent) {
        return super.onUnbind(intent);
    }

    @Override
    public void onRebind(Intent intent) {
        super.onRebind(intent);
    }

    //当该Service被关闭/销毁时会回调该方法。
    @Override
    public void onDestroy() {
        super.onDestroy();
    }

    public int calculate(int n) {
        sum = 0;
        for (int i = 0; i <= n; i++) {
            sum += i;
        }
        return sum;
    }
}

3.布局文件:

<?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">
    <LinearLayout
        android:layout_gravity="center"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
        <Button
            android:id="@+id/bind"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="绑定"/>
        <Button
            android:id="@+id/unbind"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="10dp"
            android:text="解除绑定"/>
    </LinearLayout>
    <EditText
        android:id="@+id/edit"
        android:layout_height="wrap_content"
        android:layout_width="200dp"
        android:layout_gravity="center"
        android:background="@color/teal_200"
        android:hint="请输入一个整数n"/>
    <Button
        android:id="@+id/btn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:text="计算"/>
    <LinearLayout
        android:layout_gravity="center"
        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="结果:"/>
        <TextView
            android:id="@+id/text"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"/>
    </LinearLayout>
</LinearLayout>

4.效果:

绑定Service:
在这里插入图片描述
计算:
请添加图片描述
解绑:
在这里插入图片描述

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

姓蔡小朋友

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

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

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

打赏作者

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

抵扣说明:

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

余额充值