Android单机版音乐盒
来源:互联网 发布:java代码编写规范华为 编辑:程序博客网 时间:2024/04/29 05:39
这个小软件主要用到了可以后台运行的Service组件、BroadcastReceiver组件、Intent、菜单对话框的使用及音乐的播放等。
布局很明了,通过三个线性布局的嵌套。
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent"><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="horizontal" android:layout_width="fill_parent" android:layout_height="wrap_content"> <ImageButton android:id="@+id/start" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/png2"/> <ImageButton android:id="@+id/stop" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/png1"/> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent"> <TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="25px" android:textColor="#ffffff" android:ellipsize="marquee"//跑马灯效果 android:layout_weight="1" android:marqueeRepeatLimit="marquee_forever"//跑马灯无限循环 android:text="@string/myTextView1"/> <TextView android:id="@+id/textView2" android:textSize="15px" android:gravity="center_vertical" android:layout_weight="1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/myTextView2"/> </LinearLayout></LinearLayout><ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/jinsha" /></LinearLayout>主要用到了ImageButton、ImageView还有TextView,还指定了文字颜色和大小。
喜欢研究TextView的可以看看:http://flysnow.iteye.com/blog/822358
接下来是后台播放音乐的Service:
package yxy.music;import android.app.Service;import android.content.BroadcastReceiver;import android.content.Context;import android.content.Intent;import android.content.IntentFilter;import android.media.MediaPlayer;import android.os.IBinder;public class MyService extends Service{MediaPlayer mp;ServiceReceiver serviceReceiver;int status = 1;//当前的状态,1没有声音播放 ,2 正在播放声音,3暂停@Overridepublic IBinder onBind(Intent intent) {//重写的onBind方法// TODO Auto-generated method stubreturn null;}@Overridepublic void onCreate() {//重写的onCreate方法// TODO Auto-generated method stubstatus = 1;serviceReceiver = new ServiceReceiver();//创建BroadcastReceiverIntentFilter filter = new IntentFilter();//创建过滤器filter.addAction("yxy.music.control");//添加ActionregisterReceiver(serviceReceiver, filter);//注册BroadcastReceiversuper.onCreate();}@Overridepublic void onDestroy() {//重写的onDestroy方法// TODO Auto-generated method stubunregisterReceiver(serviceReceiver);//取消注册super.onDestroy();}public class ServiceReceiver extends BroadcastReceiver{//自定义BroadcastReceiver@Overridepublic void onReceive(Context context, Intent intent) {//重写的响应方法// TODO Auto-generated method stubint action = intent.getIntExtra("ACTION", -1);//得带需要的数据switch(action){case 1://播放或暂停声音if(status == 1){//当前没有声音播放mp = MediaPlayer.create(context, R.raw.nx);status = 2;Intent sendIntent = new Intent("yxy.music.update");sendIntent.putExtra("update", 2);sendBroadcast(sendIntent);mp.start();}else if(status == 2){//正在播放声音mp.pause();//停止status = 3;//改变状态 Intent sendIntent = new Intent("yxy.music.update");sendIntent.putExtra("update", 3);//存放数据sendBroadcast(sendIntent);//发送广播}else if(status == 3){//暂停中mp.start();//播放声音status = 2;//改变状态Intent sendIntent = new Intent("yxy.music.update");sendIntent.putExtra("update", 2);//存放数据sendBroadcast(sendIntent);//发送广播}break;case 2://停止声音if(status == 2 || status == 3){//播放中或暂停中mp.stop();//停止播放status = 1;//改变状态Intent sendIntent = new Intent("yxy.music.update");sendIntent.putExtra("update", 1);//存放数据sendBroadcast(sendIntent);//发送广播}}}}}
给大家一个还算不错的介绍Service的网站:http://zhangyan1158.blog.51cto.com/2487362/545422
关于绑定,我还是不懂。。。所以重写那几个方法的时候用的是onCreat()&onDestroy()。
关于Intent和IntentFilter的介绍可以参考:http://flysnow.iteye.com/blog/961576
还用到了一个广播的机制,里面的很多用法我还不是很熟,越来越发现Intent的强大。。。下面看看Activity的代码:
package yxy.music;import android.app.Activity;import android.app.AlertDialog;import android.app.Dialog;import android.content.BroadcastReceiver;import android.content.Context;import android.content.DialogInterface;import android.content.Intent;import android.content.IntentFilter;import android.os.Bundle;import android.view.Menu;import android.view.MenuItem;import android.view.View;import android.view.View.OnClickListener;import android.widget.ImageButton;public class MusicPodActivity extends Activity implements OnClickListener{ImageButton start;//播放、暂停按钮ImageButton stop;//停止按钮ActivityReceiver activityReceiver;int status = 1;//当前的状态,1没有声音播放 ,2 正在播放声音,3暂停 /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) {//重写的onCreate方法 super.onCreate(savedInstanceState); setContentView(R.layout.main);//设置当前的用户界面 start = (ImageButton) this.findViewById(R.id.start);//得到start的引用 stop = (ImageButton) this.findViewById(R.id.stop);//得到stop按钮的引用 start.setOnClickListener(this);//为按钮添加监听 stop.setOnClickListener(this);//为按钮添加监听 activityReceiver = new ActivityReceiver();//创建BroadcastReceiver IntentFilter filter = new IntentFilter();//创建IntentFilter过滤器 filter.addAction("yxy.music.update");//添加Action registerReceiver(activityReceiver, filter);//注册监听 Intent intent = new Intent(this, MyService.class);//创建Intent startService(intent);//启动后台Service } public class ActivityReceiver extends BroadcastReceiver{//自定义的BroadcastReceiver@Overridepublic void onReceive(Context context, Intent intent) {//重写的onReceive方法// TODO Auto-generated method stubint update = intent.getIntExtra("update", -1);//得到intent中的数据switch(update){//分支判断case 1://没有声音播放status = 1; //设置当前状态break;case 2://正在播放声音start.setImageResource(R.drawable.png3);//更换图片status = 2; //设置当前状态break;case 3://暂停中start.setImageResource(R.drawable.png2);//更换图片status = 3; //设置当前状态break;}} }@Overridepublic void onClick(View v) {//接口中的方法// TODO Auto-generated method stubIntent intent = new Intent("yxy.music.control");//创建Intentswitch(v.getId()){//分支判断case R.id.start://按下播放、暂停按钮intent.putExtra("ACTION", 1);//存放数据sendBroadcast(intent);//发送广播break;case R.id.stop://按下停止按钮intent.putExtra("ACTION", 2);//存放数据sendBroadcast(intent);//发送广播break;}}@Overrideprotected void onDestroy() {//释放时被调用// TODO Auto-generated method stubsuper.onDestroy(); Intent intent = new Intent(this, MyService.class);//创建Intent stopService(intent);//停止后台的Service}@Overridepublic boolean onCreateOptionsMenu(Menu menu){//弹出菜单 menu.add(0,Menu.FIRST,0,"退出").setIcon(android.R.drawable.ic_menu_delete);//设置图标return true;}@Overridepublic boolean onOptionsItemSelected(MenuItem item){//选择的菜单项switch(item.getItemId()){//分支判断case Menu.FIRST:showDialog(1);//显示对话框break;}//将来可在此进行扩展return false;}@Overrideprotected Dialog onCreateDialog(int id){//创建对话框switch(id){//判断case 1:return new AlertDialog.Builder(this).setTitle("您确定退出?").setPositiveButton("确定", new android.content.DialogInterface.OnClickListener(){@Overridepublic void onClick(DialogInterface dialog, int which) {// TODO Auto-generated method stubSystem.exit(0);//直接退出}}).setNegativeButton("取消", null)//取消按钮.create();default:return null;}}}
我们会发现Service和Activity在互相接收广播,Activity接收到Service的广播后作出界面的变化,Service接收到Activity的广播后作出后台音乐播放的变化。
歌曲是放在raw里面的只有一首歌,呵呵。
- Android单机版音乐盒
- Android 百度音乐盒
- Android 音乐盒完整版
- Android百度音乐盒框架的完成
- 百度音乐盒网页版去广告
- Android仓库管理系统(单机版)
- 类似酷我音乐盒设计小总结(C#版)
- 音乐盒录像带第二版之注册并取得ControllerEvent方法
- css网页版的心形音乐盒,小白作品、
- 数字音乐盒
- 个音乐盒
- 网页音乐盒
- 酷我音乐盒去广告方法(我用的是酷我音乐盒2010贺岁版)
- [Android]单机斗地主,破解版,无广告
- Android开源益智游戏“斗地主”单机版源代码
- 新游戏-Android单机麻将
- 单机版泡泡堂
- 甲正进销存 单机版 bt
- new和delete运算符用于动态分配和撤销内存的运算符
- android 启动自动调用自己创建的脚本(应用程序)
- 判定一个点是否在三角形内
- CSS在线课程学习笔记
- centos下为firefox安装flash插件的几种方法
- Android单机版音乐盒
- flashSlider和s3Slider,slides——代码分析
- MapReduce Internal -- JobClient
- Clap:握在手心里的微型USB相机
- size_type 和 size_t 的区别
- 问什么样的问题,更容易推进你和她的关系?
- ASP.net编程规范注释规范
- ASP.net编程规范之命名规范
- 揭秘node.js事件