Android之Service(一)

1.Service的作用

Service是长时间运行于后台的服务,没有UI界面,常和广播在一起使用.
1.就是一种可以长时间运行于后台的应用程序组件(类似于可以长时间执行的异步任务,通过线程来实现)
2. Service 可以在应用程序切换到后台,或者Activity销毁之后,可以继续执行;(类似于Windows应用程序的驻留,右下角小图标)
3. Service是服务,可以通过特定的技术(AIDL),实现两个应用程序之间的方法调用。(类似于Socket和ServerSocket 这种)

2.Service的使用

1.开启服务的方式有两种:
Service 的两种启动方式:

satrtService()

Context.startService() 启动服务,直到stopService调用的时候,服务才会结束,也就是即使程序切换到后台,或者界面都不显示了,这个Service依然会执行

  1. startService() 如果系统没有指定的服务在运行,系统自动创建服务对象,并且调用onCreate 方法;
  2. startService() 如果系统已经运行了指定的服务,那么不会再创建服务,而是直接调用已有服务;
  3. 每次调用startService,必然会调用到onStartCommand() 这个方法就是接收参数用的。

以经典的音乐播放器来说:

// java代码中
@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

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

    public void btnPlay(View view) {
        Intent intent = new Intent(this, MusicIntentService.class);
        intent.putExtra("action","play");
        startService(intent);
    }

    public void btnStop(View view) {
        Intent intent = new Intent(this, MusicIntentService.class);
        intent.putExtra("action","pause");
        startService(intent);
    }

    public void btnExitService(View view) {
        Intent intent = new Intent(this, MusicIntentService.class);
        stopService(intent);
    }
// 一个音乐服务的类 
public class MusicService extends Service {
    private MediaPlayer mPlayer;


    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public void onCreate() {
        super.onCreate();
        mPlayer = MediaPlayer.create(this, R.raw.nobody);
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {

        if (intent != null) {
            String action = intent.getStringExtra("action");
            if (action != null) {
                switch (action) {
                    case "play":
                        mPlayer.start();
                        break;
                    case "pause":
                        mPlayer.pause();
                        break;
                }
            }
        }
        // 粘性服务
        return START_STICKY;
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if (mPlayer.isPlaying()) {
            mPlayer.stop();
            mPlayer = null;
        }
        mPlayer.release();
        mPlayer = null;
    }
}

bindService()

Context.bindService() 当前的程序组件需要将服务与自身进行关联,当程序组件结束的时候,服务自动结束。组件通常都是Activity

 @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_bind);
        Intent intent = new Intent(this, MusicBindService.class);
        bindService(intent, this, BIND_AUTO_CREATE);
    }
    @Override
    protected void onDestroy() {
        super.onDestroy();
        unbindService(this);
    }

  //当服务与当前绑定对象,绑定成功,服务的onBind方法,调用并且返回之后,回调给这个方法
    @Override
    public void onServiceConnected(ComponentName name, IBinder service) {
        mControler = ((MusicBindService.MusicControler) service);
    }

    @Override
    public void onServiceDisconnected(ComponentName name) {
        mControler = null;
    }
// 服务类
public class MusicBindService extends Service {
    private MediaPlayer mPlayer;

    public MusicBindService() {
    }

    @Override
    public void onCreate() {
        super.onCreate();
        mPlayer = MediaPlayer.create(this, R.raw.nobody);
    }

    public class MusicControler extends Binder {
        public void play() {
            mPlayer.start();
        }

        public void pause() {
            mPlayer.pause();
        }

        public void stop() {
            mPlayer.stop();
        }

        public void getDureation() {
            mPlayer.getDuration();
        }
        public void getPosition() {
            mPlayer.getCurrentPosition();
        }
    }

    @Override
    public IBinder onBind(Intent intent) {
        MusicControler controler = new MusicControler();
        return controler;
    }


    @Override
    public void onDestroy() {
        super.onDestroy();
        if (mPlayer.isPlaying()) {
            mPlayer.stop();
        }
        mPlayer.release();
        mPlayer = null;
    }
}

3.关于粘性服务

  1. 服务在onStartCommand 返回相应的数值,会影响我们的服务在意外终止的情况下,进行何种操作方式。
  2. 如果返回 START_STICKY; 那么就会在意外终止的时候,自动重新创建服务,并且调用 onStartCommand,其中参数intent 为 null.
  3. 如果返回 START_NOT_STICKY; 那么服务在意外终止的时候,将不会再次启动;
  4. 如果返回 START_REDELIVER_INTENT; 那么,服务也会重新启动,并且在进行 onStartCommand 调用的时候,将会把之前所有的intent 再次传递。
  5. 当系统通过杀死进程的形式,停止了应用程序,那么这个应用程序的服务将不能够继续运行,不论是粘性启动还是重新发送Intent.

4.关于Service的生命周期

说明startServicebindServiceActivity
如果没有启动onCreateonCreateonCreate
onStartCommandonBindstartService/bindService
onStart
结束的时候onDestoryonUnbindstopService/unbindService
onDestory
特点在没有stopService之前 服务可以一直运行上下文消失的时候,服务应该取消绑定,并且没有绑定的时候,服务自动销毁

生命周期的特点:
1. onCreate, onBind, onStartCommand … 所有生命周期的方法,全部运行于Android 的主线程。
2. 所有在Service中操作的部分不要执行耗时操作,如何实现长时间、后台的数据加载与操作?必须通过线程来实现!!!

最后一个标准的音乐播放器
1. 后台播放:startService
2. 获取进度:bindService
3. 生命周期:只要先执行startService 再进行绑定,那么不论是否还有绑定的对象,服务可以一直在后台运行;直到 没有绑定并且stopService,才会销毁

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值