zoukankan      html  css  js  c++  java
  • BroadcastReceiver的实例----基于Service的音乐播放器之一

    下面的程序开发了一个基于Service的音乐盒,程序的音乐将会由后台运行的Service组件负责播放,当后台的播放状态发生改变时,程序将会通过发送广播通知前台Activity更新界面;当用户单击前台Activity的界面按钮时,系统将通过发送广播通知后台Service来改变播放状态。前台Activity打代码如下:

    import android.os.Bundle;
    import android.app.Activity;
    import android.content.BroadcastReceiver;
    import android.content.Context;
    import android.content.Intent;
    import android.content.IntentFilter;
    import android.view.Menu;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.widget.ImageButton;
    import android.widget.TextView;

    public class MusicBox extends Activity implements OnClickListener{
      //获取界面中显示歌曲标题、作者文本框
      TextView title;
      TextView author;
      //播放、暂停按钮
      ImageButton play;
      ImageButton stop;
      ActivityReceiver activityReceiver;
      public static final String CTL_ACTION =
            "broadcasttest.action.CTL_ACTION";
      public static final String UPDATE_ACTION =
            "broadcasttest.action.UPDATE_ACTION";
      //定义音乐的播放状态,0x11代表没有播放,0x12代表正在播放,0x13代表暂停
      int status = 0x11;
      String[] titleStrs = new String[]{
          "你是我最爱的人","又见山里红","我的好兄弟"
        };
      String[] authorStrs = new String[]{
          "叶时伟","祁隆","庞龙"
        };

      @Override
      protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //获取程序界面中的两个按钮
        play = (ImageButton) findViewById(R.id.play);
        stop = (ImageButton) findViewById(R.id.stop);
        title = (TextView) findViewById(R.id.title);
        author = (TextView) findViewById(R.id.title);
        //为两个按钮的单击事件绑定监听器
        play.setOnClickListener(this);
        stop.setOnClickListener(this);
        activityReceiver = new ActivityReceiver();
        //创建IntentFilter
        IntentFilter filter = new IntentFilter();
        //指定BroadcastReceiver监听的Action
        filter.addAction(UPDATE_ACTION);
        //注册BroadcastReceiver
        registerReceiver(activityReceiver, filter);
        Intent intent = new Intent(this, MusicService.class);
        //启动后台Service
        startService(intent);
      }

      //自定义BroadcastReceiver,负责监听从Service传回来的广播
      public class ActivityReceiver extends BroadcastReceiver{

        @Override
        public void onReceive(Context context, Intent intent) {
          // 获取Intent中的update消息,update代表播放状态
          int update = intent.getIntExtra("update", -1);
          //获取Intent中的current消息,current代表当前正在播放的歌曲
          int current = intent.getIntExtra("current", -1);
          if(current >= 0){
            title.setText(titleStrs[current]);
            author.setText(authorStrs[current]);
          }
          switch(update){
            case 0x11:
              play.setImageResource(R.drawable.play);
              status = 0x11;
              break;
            //控制系统进入播放状态
            case 0x12:
              //播放状态下设置使用暂停图标
              play.setImageResource(R.drawable.pause);
              //设置当前状态
              status = 0x12;
              break;
            //控制系统进入暂停状态
            case 0x13:
              //暂停状态下设置使用播放图标
              play.setImageResource(R.drawable.play);
              //设置当前状态
              status = 0x13;
              break;
          }
        }

      }

      @Override
      public void onClick(View v) {
        // 创建Intent
        Intent intent = new Intent("jww.broadcasttest.action.CTL_ACTION");
        switch(v.getId()){
          //按下播放/暂停按钮
          case R.id.play:
            intent.putExtra("control", 1);
            break;
          //按下停止按钮
          case R.id.stop:
            intent.putExtra("control", 2);
            break;
        }
        //发送广播,将被Service组件中的BroadcastReceiver接收到
        sendBroadcast(intent);
      }

    }

    自定义的BroadcastReceiver代码段用于响应后台Service所发出的广播,该程序将会根据广播Intent里的消息来改变播放状态,并更新程序界面中按钮的图标:当正在播放时,显示暂停图标;当正在暂停时,显示播放图标。并根据传回来的current数据来更新title、author两个文本框所显示的文本-----显示当前正在播放的歌曲的歌名和歌手。

    onClick()方法代码段则根据用户单击的按钮发送广播,发送广播时会把所按下的按钮的标识发送出来,发送的广播将激发后台Service的BroadcastReceiver,该BroadcastReceiver将会根据广播消息来改变播放状态。

  • 相关阅读:
    elk 日志处理的一点思路
    elk 日志处理的一点思路
    elk 分布式部署
    elk 分布式部署
    分布式集群
    分布式集群
    分布式里数据保证容错性有两种方法.
    elk 数据存储
    elk 数据存储
    zookeeper 数据存储特点
  • 原文地址:https://www.cnblogs.com/jiww/p/5614359.html
Copyright © 2011-2022 走看看