Android四大组件之------Service(本地服务小案例)

很多童鞋被Service的本地服务与远程服务所困扰,下面就写一个小Demo来展示本地服务跟远程服务的区别:

一、概念:本地服务是Service对象与Service的启动者在同个进程中运行, 两者的通信是进程内通信。

二、这里做的本地服务呢是在本地服务中实现一个音乐的播放,废话不多说,直接上代码:
activity_main:

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

    <Button
        android:id="@+id/btn_main_play"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="play"
        android:layout_marginTop="20dp"/>

    <Button
        android:id="@+id/btn_main_stop"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="stop"
        android:layout_marginTop="20dp"/>

    <Button
        android:id="@+id/btn_main_pause"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="pause"
        android:layout_marginTop="20dp"/>

    <Button
        android:id="@+id/btn_main_exit"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="exit"
        android:layout_marginTop="20dp"/>
</LinearLayout>

MainActivity:

public class MainActivity extends Activity implements View.OnClickListener {

    private Button btn_main_play;
    private Button btn_main_stop;
    private Button btn_main_pause;
    private Button btn_main_exit;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        btn_main_play = (Button)findViewById(R.id.btn_main_play);
        btn_main_stop = (Button)findViewById(R.id.btn_main_stop);
        btn_main_pause = (Button)findViewById(R.id.btn_main_pause);
        btn_main_exit = (Button)findViewById(R.id.btn_main_exit);

        btn_main_play.setOnClickListener(this);
        btn_main_stop.setOnClickListener(this);
        btn_main_pause.setOnClickListener(this);
        btn_main_exit.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        if(v == btn_main_play) {
            Intent intent = new Intent(this,LocalService.class);
            intent.putExtra("action","play");
            startService(intent);
        }else if(v == btn_main_pause) {

            Intent intent = new Intent(this,LocalService.class);
            intent.putExtra("action","pause");
            startService(intent);

        }else if(v == btn_main_stop) {

            Intent intent = new Intent(this,LocalService.class);
            intent.putExtra("action","stop");
            startService(intent);

        }else if(v == btn_main_exit) {

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

            finish();

        }
    }
}

LocalService:

public class LocalService extends Service {
    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }


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

        String action = intent.getStringExtra("action");
        if("play".equals(action)) {
            play();
        }else if("pause".equals(action)) {
            pause();
        }else if("stop".equals(action)) {
            stop();
        }


        return super.onStartCommand(intent, flags, startId);
    }

    private MediaPlayer player;
    //停止
    private void stop() {
        Log.e("TAG", "停止播放");
        if(player != null) {
            player.stop();
            player.reset();
            player.release();
            player = null;
        }

    }


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

    //播放
    private void play() {

        if(player == null) {
            player = MediaPlayer.create(this,R.raw.water_hander);
        }
        Log.e("TAG", "播放");
        if(!player.isPlaying()) {
            player.start();
        }
    }

    @Override
    public void onDestroy() {
        stop();
        super.onDestroy();
    }
}

最后不要忘记在AndroidManifest中注册Service并且要在res目录下添加raw资源文件夹,放入要播放的音乐。
这些代码,就可以轻松实现音乐的播放梦想了!

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值