四大组件之服务(Service)

在介绍服务之前,我们有必要先了解一下Android中的进程
1进程的概念介绍
【1】Android的四大组件都是运行在主线程中
【2】服务是在后台运行的,相当于没有界面的Activity
2进程的优先级
【1】Foreground process 前台进程,优先级最高,相当于Activity执行了onResume()方法,用户正在交互。
【2】Visible process可视进程,一直影响用户看的见,相当于Activity执行了onPause()方法。
【3】Service process服务进程,通过startService方法开启了一个服务。
【4】background process后台进程,相当于Activity执行了onStop方法,界面不可见,但是Activity并没有销毁。
【5】Empty process空进程,不会维持任何组件运行,但当再次开启该进程的Activity时,速度会快一些。
startService开启服务
定义一个类继承Service,便完成了服务的定义

package com.zhixing.s.styleandtheme;

import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.support.annotation.Nullable;

public class DemoService extends Service {
    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    //当服务被创建的时候调用
    @Override
    public void onCreate() {
        super.onCreate();
    }
    //相当于Activity的onStart
    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        return super.onStartCommand(intent, flags, startId);
    }

    //当服务被销毁的时候调用
    @Override
    public void onDestroy() {
        super.onDestroy();
    }
}

在清单文件中配置服务

 <!--清单文件中配置服务-->
        <service android:name=".DemoService">
            
        </service>

开启服务

Intent intent=new Intent(this,DemoService.class);
startService(intent);

停止服务

Intent intent=new Intent(this,DemoService.class);
stopService(intent);

start开启服务的特点
【1】定义四大组件的方式是一样的,都是继承一个类,在清单文件中注册。
【2】服务是定义一个类继承Service
【3】第一次点击按钮开启服务,服务执行oncreate方法和onStartCommand方法
【4】第二次点击按钮,再次开启服务,服务执行onStartCommand方法。
【5】服务一旦被开启,服务就会在后台长期运行,直到用户手工停止。(重点)

第二种开启服务的方式bindService
服务的定义和之前一样
bindService开启服务

		Intent intent=new Intent(this,DemoService.class);
        MyConn myConn = new MyConn();
        bindService(intent,myConn,BIND_AUTO_CREATE);

MyConn定义

//定义一个类,用来监视服务的状态
    public class MyConn implements ServiceConnection{

        //当服务连接时调用
        @Override
        public void onServiceConnected(ComponentName componentName, IBinder iBinder) {
			//只有当服务中onBind方法返回IBinder类型时,此方法才调用。
        }
        //当服务断开连接时调用
        @Override
        public void onServiceDisconnected(ComponentName componentName) {

        }
    }

解绑服务

unbindService(myConn);

bindService开启服务的特点
【1】第一次点击按钮,会执行服务的onCreate方法和onBind方法。
【2】当onBind方法返回为null时,onServiveConnected方法是不执行的。
【3】第二次点击开启按钮,服务没有响应。
**【4】不求同时生,但求同时死。指的是调用者(activity)和服务之间。**在activity执行onDestroy()时必须执行解绑(重点)
【5】服务不可以多次解绑,多次解绑会报异常。
【6】通过bind方式开启服务,服务不能再设置页面找到,相当于是一个隐形的服务。

为什么要引入bindService呢
目的是调用服务中的方法。关于调用服务中方法的讲解,请看博客
https://blog.csdn.net/weixin_43311389/article/details/82975599

在广播接收者中开启服务(想要收到广播之后再开启服务)

public class BootReceiver extends BroadcastReceiver {

	@Override
	public void onReceive(Context context, Intent intent) {
		//把服务开启
		Intent intent1=new Intent(context,PhoneService.class);
		context.startService(intent1);
	}
}

混合方式开启服务
需求:我既想让服务在后台长期运行,又想调用服务中的方法。
【1】先调用startService方法开启服务,能够保证服务在后台长期运行。
【2】调用bindService方法,去获取中间人对象
【3】调用unbindService方法,解绑服务,服务解绑,但服务未被销毁。
【4】调用stopService。
混合方式开启服务案例音乐盒案例
https://blog.csdn.net/weixin_43311389/article/details/83050954

发布了54 篇原创文章 · 获赞 17 · 访问量 4万+

猜你喜欢

转载自blog.csdn.net/weixin_43311389/article/details/83050020