百度音乐盒案例(混合方式开启服务)

时间:2024-03-15 12:01:30

接四大组件之服务
https://blog.csdn.net/weixin_43311389/article/details/83050020
原理详解请参考
https://blog.csdn.net/weixin_43311389/article/details/82975599
activity_main

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity">

   <Button
       android:onClick="click1"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:text="播放"/>
    <Button
        android:onClick="click2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="暂停"/>
    <Button
        android:onClick="click3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="继续播放"/>

</LinearLayout>

MainActivity

package com.zhixing.s.music;

import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.IBinder;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;

public class MainActivity extends AppCompatActivity {

    private Iservice iservice;
    private MyConn myConn;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Intent intent=new Intent(this,MusicService.class);
        startService(intent);
        myConn = new MyConn();
        bindService(intent, myConn,BIND_AUTO_CREATE);

    }

    @Override
    protected void onDestroy() {
        unbindService(myConn);
        super.onDestroy();
    }

    //播放音乐
    public void click1(View view){
        iservice.callPlayMusic();
    }
    //暂停
    public void click2(View view){
        iservice.callPauseMuic();
    }
    //继续
    public void click3(View view){
        iservice.callRePlayMusic();
    }
    private class MyConn implements ServiceConnection{

        @Override
        public void onServiceConnected(ComponentName componentName, IBinder iBinder) {
            iservice = (Iservice) iBinder;
        }

        @Override
        public void onServiceDisconnected(ComponentName componentName) {

        }
    }
}

音乐服务

package com.zhixing.s.music;

import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;
import android.support.annotation.Nullable;

public class MusicService extends Service {
    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return new MyBinder();
    }

    @Override
    public void onCreate() {
        super.onCreate();
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        return super.onStartCommand(intent, flags, startId);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }
    public void playMusic(){
        System.out.println("播放音乐");
    }
    public void pauseMusic(){
        System.out.println("音乐暂停");
    }
    public void rePlayMusic(){
        System.out.println("继续播放音乐");
    }
    private class MyBinder extends Binder implements Iservice{

        @Override
        public void callPlayMusic() {
            playMusic();
        }

        @Override
        public void callPauseMuic() {
            pauseMusic();
        }

        @Override
        public void callRePlayMusic() {
            rePlayMusic();
        }
    }
}

Iservice

package com.zhixing.s.music;

public interface Iservice {
    public void callPlayMusic();
    public void callPauseMuic();
    public void callRePlayMusic();
}

运行结果
百度音乐盒案例(混合方式开启服务)
百度音乐盒案例(混合方式开启服务)