当前位置:   article > 正文

Android——广播与服务_android广播服务验证码实验心得

android广播服务验证码实验心得

Android——广播与服务

作业3:广播和服务
1、编写广播代码
参考网址中的example6_2模块;
2、编写音乐盒代码
参考群文件-源码-Musicbox,添加音乐播放的上一首和下一首 控制
3、2比1的难度大主要在于将广播嵌入了服务,在真正的代码撰写中,广播就是内嵌于activity和service的

本篇博客编写音乐盒代码

  • 1.准备工作

添加Mp3格式音乐文件、图标的资源;mp3格式音乐文件播放、暂停、上下首、循环图标

  • 2.页面布局 tab02.xml ;

如下图所示,添加一个LinearLayout,选择垂直分布。再在里面添加两个LinearLayout:
①一个LinearLayout,选择水平分布:用于播放控制,添加四个ImageButton;
②一个LinearLayout,选择垂直分布:展示歌手和歌曲信息。添加两个TextView;

在这里插入图片描述

  • 3.Java文件编写

本实验通过广播机制来实现,整体实现的逻辑为:
监听播放控件,控件被点击后,发送数据给MusicService,MusicService根据传送来的数据选择播放、暂停、上一首或下一首、循环播放,再将播放状态发送给MainActivity,MainActivity根据状态更新歌曲信息。

绑定页面函数

      // 获取程序界面界面中的两个按钮
        play = (ImageButton) view.findViewById(R.id.play);
        stop = (ImageButton) view.findViewById(R.id.circulation);
        title = (TextView) view.findViewById(R.id.title);
        author = (TextView) view.findViewById(R.id.author);

        //获取上一首、下一首按钮
        button1 = (ImageButton) view.findViewById(R.id.previous);
        button2 = (ImageButton) view.findViewById(R.id.next);

        // 为两个按钮的单击事件添加监听器
        play.setOnClickListener(this);
        stop.setOnClickListener(this);
        //为上一首、下一首按钮添加监听器
        button1.setOnClickListener(this);
        button2.setOnClickListener(this);
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16

功能函数

            //发送广播通知Activity更改文本框
            Intent sendIntent = new Intent(ShoppingFragment.UPDATE_ACTION);
            sendIntent.putExtra("current", current);
            // 发送广播,将被Activity组件中的BroadcastReceiver接收到
            sendBroadcast(sendIntent);
            // 准备并播放音乐
            prepareAndPlay(musics[current]);
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
声明:本文内容由网友自发贡献,转载请注明出处:【wpsshop博客】
推荐阅读
相关标签
  

闽ICP备14008679号