zoukankan      html  css  js  c++  java
  • 第九周安卓开发学习总结(3)

    写在前面

    这段时间主要学习了安卓四大组件中很重要的一个组件:服务(service)。到此,四大组件已经学完了三个了,还有一个内容提供者(Content Provider),暂且延后。接下来的几天专心项目的开发。
    本文所有代码github地址:https://github.com/wushenjiang/ServiceDemo

    服务介绍

    在安卓中,服务有着和Activity相似的地位。不过Activity是在前台运行,而Service是在后台运行的,不可见的。Service的使用其实非常普遍。许多APP都用到了服务。考虑到可能为我们的APP添加推送功能,才花费了大把时间学习了Service。

    服务的启动方式

    服务的启动方式主要有两种:startService和bindService,以下分别介绍:

    startService方法启动

    首先来看方法:

        public void startServiceClick(View v) {
            Intent intent = new Intent();
            intent.setClass(this, FirstService.class);
            startService(intent);
        }
    

    我们需要传入一个intent即可启动服务,对应的,停止服务的方法:

       public void stopServiceClick(View v) {
            Intent intent = new Intent();
            intent.setClass(this, FirstService.class);
            stopService(intent);
    
        }
    

    同样的,也需要传入一个intent对象。

    bindService方法启动Service对象

    直接看代码:

        public void bindServiceClick(View v) {
            Intent intent = new Intent();
            intent.setClass(this, FirstService.class);
            mIsServiceBinded =  bindService(intent,mConnection,BIND_AUTO_CREATE);
        }
    private ServiceConnection mConnection = new ServiceConnection() {
            @Override
            public void onServiceConnected(ComponentName name, IBinder service) {
                Log.d(TAG,"onServiceConnected...");
                mICommunication =(ICommunication) service;
            }
    
            @Override
            public void onServiceDisconnected(ComponentName name) {
                Log.d(TAG,"onServiceDisconnected...");
                mICommunication = null;
            }
        };
    

    与startService不同的是,使用BindService需要传入一个ServiceConnection对象。我们直接在方法中创建他即可。
    那么我们再来看看解绑的方法:

       public void unbindServiceClick(View v) {
            if (mConnection != null) {
                unbindService(mConnection);
            }
    
        }
    

    只需传入一个ServiceConnection方法即可。

    两种方法的优缺点

    * 服务的开启方式有两种:
     * 1、startService-->stopService
     * 这种启动方式的优点:服务可以长期存在于后台运行
     * 缺点:不能够进行通讯
     * 生命周期:
     * 最基本的生命周期:
     * onCreate()-->onStartCommand()-->onDestroy();
     * 如果服务已经启动,就不再创建(onCreate)了,除非执行了onDestroy();
     * 2、 bindService 绑定服务,如果没有启动,自动启动
     * -->UnbindService 解绑服务
     * 优点:可以进行通讯。
     * 缺点:不可以长期处于后台运行。如果不解绑将发生泄漏leak
     * 如果解绑了服务将停止运行.
     * 生命周期:
     * onCreate()-->onBind()-->onUnBind()-->onDestroy();
    

    混合启动方式

     * 混合开启服务的生命周期
     * 1.开启服务,绑定服务.如果不取消绑定那么无法停止服务
     * 2.开启服务后,多次绑定-解绑服务,服务不会被停止,只能通过stopService()来停止
     * 推荐的混合开启服务方式:
     * 1 开启服务-->为了确保服务尅长期于后台运行
     * 2、绑定服务-->为了可以进行通讯
     * 3、调用服务内部的方法,如控制音乐播放器的播放/暂停/停止/快进
     * 4、退出Activity要记得解绑服务-->释放资源
     * 5、如果不使用服务了要让服务停止,那么就调用stopService()
     */
    

    使用混合启动方式编写一个简易的音乐播放器

    代码都已放到最上面的github链接,需要的可以去看看。这里不再详细说了。

  • 相关阅读:
    读书笔记——吴军《态度》
    JZYZOJ1237 教授的测试 dfs
    NOI1999 JZYZOJ1289 棋盘分割 dp 方差的数学结论
    [JZYZOJ 1288][洛谷 1005] NOIP2007 矩阵取数 dp 高精度
    POJ 3904 JZYZOJ 1202 Sky Code 莫比乌斯反演 组合数
    POJ2157 Check the difficulty of problems 概率DP
    HDU3853 LOOPS 期望DP 简单
    Codeforces 148D. Bag of mice 概率dp
    POJ3071 Football 概率DP 简单
    HDU4405 Aeroplane chess 飞行棋 期望dp 简单
  • 原文地址:https://www.cnblogs.com/wushenjiang/p/12727660.html
Copyright © 2011-2022 走看看