Android四大组件之Service

什么是Service

  1. Service是一个应用组件, 它用来在后台完成一个时间跨度比较大的工作且没有关联任何界面
  2. 一个Service可以完成下面这些工作:
    • 访问网络
    • 播放音乐
    • 文件IO操作
    • 大数据量的数据库操作……
  3. 服务的特点:
    • Service在后台运行,不用与用户进行交互
    • 即使应用退出, 服务也不会停止
    • 在默认情况下,Service运行在应用程序进程的主线程(UI线程)中,如果需要在Service中处理一些网络连接等耗时的操作,那么应该将这些任务放在分线程中处理,避免阻塞用户界面

Service的分类

  • Local Service(本地服务): Service对象与Serive的启动者在同个进程中运行, 两者的通信是进程内通信,通俗点就是启动同一应用的Service就是本地服务。

  • Remote Service(远程服务): Service对象与Service的启动者不在同一个进程中运行, 这时存在一个进程间通信的问题, Android专门为此设计了AIDL来实现进程间通信。通俗点就是启动其他应用的Service就是远程服务。

Service和Thread区别

  1. Service:

    • 用来在后台完成一个时间跨度比较大的工作的应用组件
    • Service的生命周期方法运行在主线程, 如果Service想做持续时间比较长的工作, 需要启动一个分线程(Thread)
    • 应用退出: Service不会停止
    • 应用再次进入: 可以与正在运行的Service进行通信
  2. Thread:

    • 用来开启一个分线程的类, 做一个长时间的工作
    • Thread对象的run()在分线程执行
    • 应用退出: Thread不会停止,
    • 应用再次进入: 不能再控制前面启动的Thread对象

定义Service

  1. 定义一个类继承于Service类

    public class MyService extends Service {
        
        @Nullable
        @Override
        public IBinder onBind(Intent intent) {
            return null;
        }
        
        ......
    }
    
  2. 在AndroidManifest.xml中配置Service

    <service android:name=".MyService">
        <intent-filter>
            <action android:name="guo.ping.activitydemo.MyService"/>
        </intent-filter>
    </service>
    

启动、停止Service

  • 一般启动与停止

    context.startService(Intent intent)
    context.stopService(Intent intent)
    
  • 绑定启动与解绑

    context.bindService(Intent intent, ServiceConnection connection)
    context.unbindService(ServiceConnection connection)
    
  • 一般启动停止Service

    public void startMyService(View view){
        Intent intent = new Intent(this, MyService.class);
        startService(intent);
    }
    
    public void stopMyService(View view){
        Intent intent = new Intent(this, MyService.class);
        stopService(intent);
    }
    
  • 绑定启动Service与解绑:

    public void bindMyService(View view) {
        Intent intent = new Intent(this, MyService.class);
    
        if (mServiceConnection == null) {
            mServiceConnection = new ServiceConnection() {
                @Override
                public void onServiceConnected(ComponentName name, IBinder service) {
                    Log.i(TAG, "onServiceConnected()" + service.hashCode());
                }
    
                @Override
                public void onServiceDisconnected(ComponentName name) {
                    Log.i(TAG, "onServiceDisconnected()");
                }
            };
    
            bindService(intent, mServiceConnection, Context.BIND_AUTO_CREATE);
        } else {
            Log.i(TAG, "已经绑定...");
        }
    }
    
    public void unbindMyService(View view) {
        if (mServiceConnection != null) {
            Intent intent = new Intent(this, MyService.class);
            unbindService(mServiceConnection);
    
            mServiceConnection = null;
        } else {
            Log.i(TAG, "已经解绑...");
        }
    }
    

    MyService类重写onBind()和onUnbind()方法:

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        Log.i(TAG, "MyService onBind()");
        Binder binder = new Binder();
        Log.i(TAG, "binder.hashCode() = " + binder.hashCode());
        return binder;
    }
    
    @Override
    public boolean onUnbind(Intent intent) {
        Log.i(TAG, "MyService onUnbind()");
        return super.onUnbind(intent);
    }
    

    测试结果如下,可以看到在MyService的onBind()方法中返回的Binder对象就是在onServiceConnected()回调方法中的IBinder类型的形参service。

    07-29 10:52:43.002 2794-2794/guo.ping.activitydemo I/MyService: MyService()
    07-29 10:52:43.002 2794-2794/guo.ping.activitydemo I/MyService: MyService onCreate()
    07-29 10:52:43.002 2794-2794/guo.ping.activitydemo I/MyService: MyService onBind()
    07-29 10:52:43.002 2794-2794/guo.ping.activitydemo I/MyService: 1394383824
    07-29 10:52:43.002 2794-2794/guo.ping.activitydemo I/MainActivity: onServiceConnected()1394383824
    07-29 10:52:46.854 2794-2794/guo.ping.activitydemo I/MyService: MyService onUnbind()
    07-29 10:52:46.854 2794-2794/guo.ping.activitydemo I/MyService: MyService onDestroy()
    

    这种方式下,Activity和Service是通过ServiceConnection相关联的,如果Activity销毁则这个链接是要断开的,会引起ServiceConnectionLeaked异常:

    07-29 11:00:39.046 2794-2794/guo.ping.activitydemo E/ActivityThread: Activity guo.ping.activitydemo.MainActivity has leaked ServiceConnection guo.ping.activitydemo.MainActivity$1@5320601c that was originally bound here
    
    android.app.ServiceConnectionLeaked: Activity guo.ping.activitydemo.MainActivity has leaked ServiceConnection guo.ping.activitydemo.MainActivity$1@5320601c that was originally bound here
    

    需要我们在Activity的onDestory方法中进行解绑操作:

    @Override
    protected void onDestroy() {
        super.onDestroy();
        Log.i(TAG, "onDestroy()");
        if(mServiceConnection != null){
            Intent intent = new Intent(this, MyService.class);
            unbindService(mServiceConnection);
    
            mServiceConnection = null;
        }
    }
    

Service的生命周期

Service的生命周期

  • 当用一般方式启动Service时,如果Service已经启动,则不会调用onCreated(),直接调用onStartCommand()方法;
  • 当有多个Activity等组件和一个Service进行绑定,必须所有的客户端全部调用unbindService()方法进行解绑Service端才能调用onUnbind()方法,这一点还是合乎情理的。

AIDL

  • 每个应用程序都运行在自己的独立进程中,并且可以启动另一个应用进程的服务,而且经常需要在不同的进程间传递数据对象。
  • 在Android平台,一个进程不能直接访问另一个进程的内存空间,所以要想对话,需要将对象分解成操作系统可以理解的基本单元,并且有序的通过进程边界。
  • 如果在一个进程中(例如Activity)要调用另一个进程中(例如Service)对象的操作,就可以使用AIDL生成可序列化的参数。
  • AIDL (Android Interface Definition Language):用于生成可以在Android设备上两个进程之间进行进程间通信(interprocess communication, IPC)的代码。

使用AIDL的思路理解

整个的流程其实大致是,在本进程(应用)中,实现某一需求需要调用另一个进程(应用)的某个服务(服务中包含某个具体的方法),所以我们需要先将请求参数发给另一个进程的远程服务,然后远程服务将参数接收调用方法处理得出结果,并将结果返回给原来的进程。而AIDL就是充当这个传输数据的角色,将参数送至、结果传回。
AIDL整体流程

AIDL文件的书写规则

  1. 接口名和aidl文件名相同.
  2. 接口和方法前不用加访问权限修饰符public,private,protected等,也不能用final,static.
  3. Aidl默认支持的类型包话java基本类型(int,long,boolean等)和(String,List,Map, CharSequence),使用这些类型时不需要import声明.对于List和Map中的元素类型必须是Aidl支持的类型.如果使用自定义类型作为参数或返回值,自定义类型必须实现Parcelable接口.
  4. 自定义类型和AIDL生成的其它接口类型在aidl描述文件中,应该显式import,即便在该类和定义的包在同一个包中.
  5. 在aidl文件中所有非Java基本类型参数必须加上in、out、inout标记,以指明参数是输入参数、输出参数还是输入输出参数.
  6. Java原始类型默认的标记为int,不能为其它标记.

使用AIDL的Demo

推荐博客:

需求:

  • 传递俩个int类型数字a、b,调用远程方法计算俩者之和并返回
  • 通过id来调用远程服务查找对应的学生记录并返回

实现:

  1. 定义Student的JavaBean文件,需要实现Parcelable接口。在打包与解包时,顺序要一致,否则会出现错误

    package guo.ping.testservice;
    
    import android.os.Parcel;
    import android.os.Parcelable;
    
    public class Student implements Parcelable {
    
        private int id;
        private String name;
        private double weight;
        
        public Student() {
        }
        
        public Student(int id, String name, double weight) {
            this.id = id;
            this.name = name;
            this.weight = weight;
        }
    
        public int getId() {
            return id;
        }
    
        public void setId(int id) {
            this.id = id;
        }
    
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    
        public double getWeight() {
            return weight;
        }
    
        public void setWeight(double weight) {
            this.weight = weight;
        }
    
        @Override
        public String toString() {
            return "Student{" +
                    "id=" + id +
                    ", name='" + name + '\'' +
                    ", weight=" + weight +
                    '}';
        }
    
        // 添加一个静态成员,名为CREATOR,该对象实现了Parcelable.Creator接口
        public static final Parcelable.Creator<Student> CREATOR = new Creator<Student>() {
            @Override
            public Student createFromParcel(Parcel source) {
                int id = source.readInt();
                String name = source.readString();
                double weight = source.readDouble();
                return new Student(id, name, weight);
            }
    
            @Override
            public Student[] newArray(int size) {
                return new Student[size];
            }
        };
    
        @Override
        public int describeContents() {
            return 0;
        }
    
        //将当前对象的属性数据写到Parcel包对象中(也就是打包)
        @Override
        public void writeToParcel(Parcel dest, int flags) {
            dest.writeInt(id);
            dest.writeString(name);
            dest.writeDouble(weight);
        }
    }
    
  2. 定义Student.aidl文件。文件名只能是Student,其他会出错;

    package guo.ping.testservice;
    
    parcelable Student;
    
  3. 定义IAdd.aidl文件,这个文件里面包含着我们需要远程服务包含的抽象方法;

    package guo.ping.testservice;
    import guo.ping.testservice.Student;
    
    interface IAdd {
        int add(int a, int b);
        Student queryStudentById(int id);
    }
    
  4. 将上述三个文件拷贝至另一个工程,总之客户端与服务端都需要拥有,包名要一致,否则也是出错;

    工程目录结构

  5. AndroidStudio中选择Build——>Make Project,生成AIDL的代码;

  6. 在服务端编写Service,并实现AIDL文件中的抽象方法;

    public class MyRemoteService extends Service {
    
        private IBinder mIBinder = new IAdd.Stub() {
            @Override
            public int add(int a, int b) throws RemoteException {
                return a + b;
            }
    
            @Override
            public Student queryStudentById(int id) throws RemoteException {
                return new Student(id, "假装查了数据库叫Tom", 140.5);
            }
        };
    
    
        @Nullable
        @Override
        public IBinder onBind(Intent intent) {
            return mIBinder;
        }
    
        @Override
        public boolean onUnbind(Intent intent) {
            return super.onUnbind(intent);
        }
    }
    

    这里定义了成员变量mIBinder,new IAdd.Stub()是Android依据我们编写的AIDL文件生成的Java代码里面写好的。然后就是注册Service,这里需要intent-filter定义好便于隐式意图启动。

    <service android:name=".MyRemoteService">
        <intent-filter>
            <action android:name="guo.ping.testservice.MyRemoteService"/>
        </intent-filter>
    </service>
    
  7. 客户端编写启动远程Service的方法;

    public void callTheRemoteServiceMethod(View view) throws RemoteException {
        int add = mIAdd.add(2, 5);
        Log.i(TAG, add + "");
    
        Student student = mIAdd.queryStudentById(10);
        Log.i(TAG, student.toString());
    }
    
    
    public void startRemoteService(View view) {
        Intent intent = new Intent("guo.ping.testservice.MyRemoteService");
    
        if (mServiceConnection == null) {
            mServiceConnection = new ServiceConnection() {
                @Override
                public void onServiceConnected(ComponentName name, IBinder service) {
                    mIAdd = IAdd.Stub.asInterface(service);
                    Log.i(TAG, "onServiceConnected()");
                }
    
                @Override
                public void onServiceDisconnected(ComponentName name) {
                    Log.i(TAG, "onServiceDisconnected()");
                }
            };
    
            bindService(intent, mServiceConnection, Context.BIND_AUTO_CREATE);
        } else {
            Log.i(TAG, "已经绑定...");
        }
    }
    
    public void stopRemoteService(View view) {
    
        if (mServiceConnection != null) {
            unbindService(mServiceConnection);
            mIAdd = null;
            mServiceConnection = null;
        } else {
            Log.i(TAG, "已经解绑...");
        }
    }
    
  8. 测试结果如下;

    测试结果

补充

在关闭远程服务的时候,发现重写的onServiceDisconnected()方法Log不打印,想着onServiceConnected()要打印必须在Service的onBind()方法中返回IBinder对象,所以便在Service中实现onUnbind()方法,发现并没有乱用。
百度了一下,原来当service所在进程crash或者被kill的时候,onServiceDisconnected才会被呼叫。具体参考:我的onServiceDisconnected为什么没有被呼叫

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值