Android 四大组件之 Service

作者: kevenZheng | 来源:发表于2019-04-03 23:00 被阅读17次

    读前思考

    学习一门技术或者看一篇文章最好的方式就是带着问题去学习,这样才能在过程中有茅塞顿开、灯火阑珊的感觉,记忆也会更深刻。

    1. 谈一谈 Service 的生命周期?
    2. Service的两种启动方式?区别在哪?
    3. 一个 Activty 先 start 一个 Service 后,再 bind 时会回调什么方法?
    4. Service 如何和 Activity 进行通信?
    5. 是否能在 Service 进行耗时操作?如果非要可以怎么做?
    6. 前台服务是什么?和普通服务的不同?如何去开启一个前台服务?

    创建服务

    1. 创建一个类继承 Service ,重写 onBind( )方法 .
    public class Part1aService extends Service {
        private static final String CHANNEL_ID_STRING = "KEVEN_JIANSHU";
        
        public Part1aService() {
        
        }
    
        @Override
        public IBinder onBind(Intent intent) {
            LogUtils.i("Service 执行了 onBind( )");
            return null;
        }
    
    1. 在清单文件中注册 Service .
    <service android:name=".part1.Part1aService"></service>
    

    启动服务

    启动服务有两种方式,一种是 startService( ) ,一种是 bindService( ) .

    startService( )

    特点:
    一旦服务开启就跟调用者(开启者)没有任何关系了。开启者退出了,开启者挂了,服务还在后台长期的运行,开启者不能调用服务里面的方法。

    使用如下代码进行服务开启

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

    使用如下代码进行服务关闭(或者 Service 调用 stopSelf( ) )

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

    生命周期回调

    com.keven.jianshu I/TAG: Service 执行了 onCreate( )
    com.keven.jianshu I/TAG: Service 执行了 onStartCommand( )
    com.keven.jianshu I/TAG: Service 执行了 onDestroy( )
    

    Android 8.0 + ,对后台服务进行了限制了,官网如下所述。

    Android 8.0 还对特定函数做出了以下变更:

    如果针对 Android 8.0 的应用尝试在不允许其创建后台服务的情况下使用 startService() 函数,则该函数将引发一个 IllegalStateException。
    新的 Context.startForegroundService() 函数将启动一个前台服务。现在,即使应用在后台运行,系统也允许其调用 Context.startForegroundService()。
    不过,应用必须在创建服务后的五秒内调用该服务的 startForeground() 函数。

    所以,启动代码就可以针对文档所述进行适配。

    //进行8.0+ 以上启动服务的适配
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        Intent intent = new Intent(Part1aActivity.this, Part1aService.class);
        startForegroundService(intent);
    } else {
        Intent intent = new Intent(Part1aActivity.this, Part1aService.class);
        startService(intent);
    }
    

    在 Service 中的 onCreate( ) 方法中做如下修改。

    @Override
        public void onCreate() {
            LogUtils.i("Service 执行了 onCreate( )");
            super.onCreate();
    
            //适配8.0+service
            NotificationManager notificationManager = (NotificationManager) MyApp.getInstance().getSystemService(Context.NOTIFICATION_SERVICE);
            NotificationChannel mChannel = null;
            if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
                mChannel = new NotificationChannel(CHANNEL_ID_STRING, "Keven 简书", NotificationManager.IMPORTANCE_HIGH);
                notificationManager.createNotificationChannel(mChannel);
                Notification notification = new Notification.Builder(getApplicationContext(), CHANNEL_ID_STRING).build();
                startForeground(1, notification);
            }
    
        }
    

    bindService( )

    特点:bind 的方式开启服务,绑定服务,调用者挂了,服务也会跟着挂掉。绑定者可以调用服务里面的方法。

    使用 bind 方式开启与关闭服务的代码如下

    //绑定服务
    Intent intent = new Intent(Part1aActivity.this, Part1aService.class);
    bindService(intent, connection, BIND_AUTO_CREATE);
    
    //解绑服务
    Intent intent = new Intent(Part1aActivity.this, Part1aService.class);
    unbindService(connection);
    

    生命周期调用如下

    com.keven.jianshu I/TAG: Service 执行了 onCreate( )
    com.keven.jianshu I/TAG: Service 执行了 onBind( )
    com.keven.jianshu I/TAG: Service 执行了 onUnbind( )
    com.keven.jianshu I/TAG: Service 执行了 onDestroy( )
    

    一个 Activty 先 start 一个 Service 后,再 bind 时会回调 onBind( ) 方法,不会调用 onCreate( ) 方法,因为服务已经启动了。

    Service 和 Activity 进行通信

    方式一:广播

    这种方式比较简单,用 Android 四大组件之一的广播即可实现通信。

    方式二:针对 bindService( ) 启动方式的通信

    1. 在 Service 中创建类继承 Binder,并在其中编写需要 Activity 调用的方法。
    class MyBinder extends Binder {
            public void getServiceMethod() {
                LogUtils.e("调用了 Part1aService 的 getServiceMethod( ) 方法");
            }
        }
    
    1. 在 Service 中创建上述类的对象。
    private MyBinder binder = new MyBinder();
    
    1. 在 Service 中的 onBind( ) 方法中返回创建的对象。
    @Override
        public IBinder onBind(Intent intent) {
            LogUtils.i("Service 执行了 onBind( )");
            return binder;
        }
    
    1. 在 Activity 中创建 ServiceConnection 类,并重写它的 onServiceConnected( ) 和 onServiceDisconnected( ) 方法,并将 onServiceConnected( ) 方法中的 IBinder 类型的对象强转为 Service 中类的类型,之后就可以调用其中的方法进行通信了。
    ServiceConnection connection = new ServiceConnection() {
            @Override
            public void onServiceConnected(ComponentName name, IBinder service) {
                Part1aService.MyBinder myBinder = (Part1aService.MyBinder) service;
                myBinder.getServiceMethod();
            }
    
            @Override
            public void onServiceDisconnected(ComponentName name) {
    
            }
        };
    

    耗时操作

    因为服务也是运行在 UI 线程的,所以不能再服务中执行耗时操作,如果需要执行耗时操作,可以有如下两种方式。

    方式一: 开启子线程执行耗时操作;

    方式二: 使用 IntentService 。

    IntentService 是 Android 系统提供的类,可在其中执行耗时任务,当任务执行完后,服务会自动销毁,无需操作。

    通过查看源码可以知道 IntentService 继承 Service,在它的 onCreate( ) 方法中创建了 HandlerThread,Looper,ServiceHandler ,所以其内部有消息机制和线程,可以在 onHandleIntent( ) 方法中执行耗时操作。

    我们可以新建一个类继承 IntentService,重写它的 onHandleIntent( ) 方法,并在其中执行耗时操作,然后在清单文件中注册这个服务即可。

    文章已经读到末尾了,不知道最初的几个问题你都会了吗?如果不会的话?可以再针对不会的问题进行精读哦!答案都在文中,相信你肯定可以解决的!

    相关文章

      网友评论

        本文标题:Android 四大组件之 Service

        本文链接:https://www.haomeiwen.com/subject/sitxiqtx.html