Android基础学习【历史流程重走】 ---- 四大组件之Service

一、 Service简介

         Service是android 系统中的四大组件之一(Activity、Service、BroadcastReceiver、ContentProvider)不能自己运行,只能后台运行。可以和其他组件进行交互。常用于后台监听或者保持程序后台运行。例如:使用其他APP的时候,音乐播放器后台继续播放。


二、Service的生命周期

context.startService() 启动流程:

<span style="font-size:18px;">context.startService()  -> onCreate()  -> onStart()  -> Service running  -> context.stopService()  -> onDestroy()  -> Service stop </span>

context.bindService()启动流程:

<span style="font-size:18px;">context.bindService()  -> onCreate()  -> onBind()  -> Service running  -> onUnbind()  -> onDestroy()  -> Service stop</span>

三、示例使用

MainActivity

<span style="font-size:18px;">public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    private Button playBt;
    private Button pauseBt;
    private Button stopBt;
    private Button closeBt;
    private Button exitBt;

    private Intent intent;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        playBt = (Button) findViewById(R.id.play);
        pauseBt = (Button) findViewById(R.id.pause);
        stopBt = (Button) findViewById(R.id.stop);
        closeBt = (Button) findViewById(R.id.close);
        exitBt = (Button) findViewById(R.id.exit);

        playBt.setOnClickListener(this);
        pauseBt.setOnClickListener(this);
        stopBt.setOnClickListener(this);
        closeBt.setOnClickListener(this);
        exitBt.setOnClickListener(this);
    }


    @Override
    public void onClick(View v) {
        int op = -1;
        intent = new Intent("com.future.servicedemo.ServiceMain");
        switch (v.getId()) {
            case R.id.play:
                op = 1;
                break;
            case R.id.pause:
                op = 2;
                break;
            case R.id.stop:
                op = 3;
                break;
            case R.id.close:
                this.finish();
                break;
            case R.id.exit:
                op = 4;
                stopService(intent);
                this.finish();
                break;
        }
        Bundle bundle = new Bundle();
        bundle.putInt("op", op);
        intent.putExtras(bundle);

        startService(intent);
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (intent != null) {
            stopService(intent);
        }
    }
}</span>
ServiceMain

<span style="font-size:18px;">/**
 * 功能:Service主类
 * 作者:vision
 * 时间:2016/9/18
 */
public class ServiceMain extends Service {
    private static final String TAG = "ServiceMain";
    private MediaPlayer mediaPlayer;


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

    @Override
    public void onCreate() {
        Log.v(TAG, "onCreate");
        Toast.makeText(this, "show media palyer", Toast.LENGTH_SHORT).show();
        if (mediaPlayer == null) {
            mediaPlayer = MediaPlayer.create(this, R.raw.music);
            mediaPlayer.setLooping(false);
        }
    }

    @Override
    public void onDestroy() {
        Log.v(TAG, "onDestroy");
        Toast.makeText(this, "stop media palyer", Toast.LENGTH_SHORT).show();
        if (mediaPlayer != null) {
            mediaPlayer.stop();
            mediaPlayer.release();
        }
    }

    @Override
    public void onStart(Intent intent, int startId) {
        Log.v(TAG, "onStart");
        if (intent != null) {
            Bundle bundle = intent.getExtras();
            if (bundle != null) {
                int op = bundle.getInt("op");
                switch (op) {
                    case 1:
                        play();
                        break;
                    case 2:
                        pause();
                        break;
                    case 3:
                        stop();
                        break;
                }
            }
        }

    }

    /**
     * 暂停
     */
    private void pause() {
        if (mediaPlayer != null && mediaPlayer.isPlaying()) {
            mediaPlayer.pause();
        }
    }

    /**
     * 停止
     */
    private void stop() {
        if (mediaPlayer != null) {
            mediaPlayer.stop();
            try {
                mediaPlayer.prepare();  // 在调用stop后如果需要再次通过start进行播放,需要之前调用prepare函数
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }
    }

    /**
     * 播放
     */
    private void play() {
        if (!mediaPlayer.isPlaying()) {
            mediaPlayer.start();
        }
    }
}</span>

页面效果:


源码


不要被别人的思维局限,不要用自己的思维局限别人。


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

壹叁零壹

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值