Android开发

Android四大组件之Service

2017-07-30  本文已影响0人  Mr丶sorrow

什么是Service

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

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

Service的生命周期

Service的生命周期

AIDL

使用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

推荐博客:

需求:

实现:

  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为什么没有被呼叫

上一篇 下一篇

猜你喜欢

热点阅读