上一篇我们讲述了如何发送广播,那么将广播发送之后,该如何接收?谁负责接收?怎么接受?
直接看代码:
package com.lmy.lesson_10;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.media.AudioTrack;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.test.IsolatedContext;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;
import android.widget.TextView;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.AdapterView.OnItemClickListener;
import lmyPackage.LrcModel;
import lmyPackage.Model;
public class MainActivity extends Activity implements OnClickListener {
// 负责交互的动作
Intent intent;
// 列表对象
ListView mylist;
// 适配器
MyAdatpter myadapter;
// 扫描
ScanMusic scan;
// 动态数组
ArrayList arrlist;
// 多媒体系统类
MediaPlayer media;
// 播放/下一曲/上一曲
ImageView play_img, recend_img, next_img, repeat_img;
// 是否播放
boolean isplay;
// 时间和歌词
TextView stop_time, start_time, text_gc;
// 进度条
SeekBar my_seekbar;
// 广播接收
MyBrodcastRecriver mybrodcast;
ArrayList context_song = new ArrayList();
ArrayList context_singer = new ArrayList();
ArrayList context_path = new ArrayList();
// 循环标志
int repeatState = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
intent = new Intent();
intent.setClass(MainActivity.this, MySeriver.class);
startService(intent);
// 播放列表
mylist = (ListView) findViewById(R.id.music_list);
// 上一曲
recend_img = (ImageView) findViewById(R.id.previous_music);
// 重复播放
repeat_img = (ImageView) findViewById(R.id.repeat_music);
// 播放
play_img = (ImageView) findViewById(R.id.play_music);
// 下一曲
next_img = (ImageView) findViewById(R.id.next_music);
// 歌曲现时时间
start_time = (TextView) findViewById(R.id.start_time);
// 歌曲总时间
stop_time = (TextView) findViewById(R.id.stop_time);
// 进度条
my_seekbar = (SeekBar) findViewById(R.id.bar_time);
// 歌词
text_gc = (TextView) findViewById(R.id.text_gc);
mybrodcast = new MyBrodcastRecriver();
zhuce();
isplay = false;
//适配器
myadapter = new MyAdatpter();
scan = new ScanMusic();
arrlist = scan.query(arrlist, this);
for (int i = 0; i < arrlist.size(); i++) {
context_song.add(arrlist.get(i).getText_song());
context_singer.add(arrlist.get(i).getText_singer());
context_path.add(arrlist.get(i).getPath());
}
// 监听事件
play_img.setOnClickListener(this);
recend_img.setOnClickListener(this);
next_img.setOnClickListener(this);
repeat_img.setOnClickListener(this);
mylist.setAdapter(myadapter);
mylist.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView> parent, View view, int position, long id) {
// TODO Auto-generated method stub
Intent intent = new Intent();
intent.setAction("ACTION_INDEX");
intent.putExtra("index", position);
sendBroadcast(intent);
}
});
my_seekbar.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
}
//拖动进度条事件
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
// TODO Auto-generated method stub
if (fromUser == true) {
Intent intent1 = new Intent();
intent1.setAction("ACTION_SEEKBAR_DRANG");
intent1.putExtra("progress", progress);
sendBroadcast(intent1);
}
}
});
}
class MyAdatpter extends BaseAdapter {
@Override
public int getCount() {
// TODO Auto-generated method stub
return context_song.size();
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return context_song.get(position);
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
ViewHolder viewholder;
if (convertView == null) {
viewholder = new ViewHolder();
convertView = LayoutInflater.from(MainActivity.this).inflate(R.layout.item, null);
viewholder.text_singer = (TextView) convertView.findViewById(R.id.text_singer);
viewholder.text_song = (TextView) convertView.findViewById(R.id.text_song);
convertView.setTag(viewholder);
} else {
viewholder = (ViewHolder) convertView.getTag();
}
viewholder.text_singer.setText("" + context_singer.get(position));
viewholder.text_song.setText("" + context_song.get(position));
return convertView;
}
}
static class ViewHolder {
TextView text_song, text_singer;
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
//判断当前点击事件
switch (v.getId()) {
case R.id.play_music:
isplay = !isplay;
if (isplay == true) {
play_img.setBackgroundResource(R.drawable.play_selector);
} else {
play_img.setBackgroundResource(R.drawable.pause_selector);
}
MusicPaly(isplay);
break;
case R.id.previous_music:
NextMuisc(0);
break;
case R.id.next_music:
NextMuisc(1);
break;
case R.id.repeat_music:
Choosemention();
break;
default:
break;
}
}
public void MusicPaly(boolean isplay) {
Intent intent = new Intent();
intent.setAction("ACTION_ISPLAY");
intent.putExtra("isplay", isplay);
sendBroadcast(intent);
}
public void NextMuisc(int num) {
Intent intent = new Intent();
intent.setAction("ACTION_NEXT");
intent.putExtra("next", num);
sendBroadcast(intent);
}
public void Choosemention(){
switch (repeatState) {
case 0:
repeat_img.setBackgroundResource(R.drawable.repeat_all_selector);
repeatState++;
repeatState%=3;
break;
case 1:
repeat_img.setBackgroundResource(R.drawable.repeat_current_selector);
repeatState++;
repeatState%=3;
break;
case 2:
repeat_img.setBackgroundResource(R.drawable.shuffle_selector);
repeatState++;
repeatState%=3;
break;
default:
break;
}
Setmention();
}
public void Setmention(){
Intent intent =new Intent();
intent.setAction("ACTION_MENTION");
intent.putExtra("mention", repeatState);
sendBroadcast(intent);
}
class MyBrodcastRecriver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
//得到歌曲总时间
if (intent.getAction().equals("ACTION_ALLTIME")) {
int maxtime = intent.getIntExtra("action_time", 0);
my_seekbar.setMax(maxtime);
stop_time.setText(setTime(maxtime));
} //得到歌曲现在播放时间
else if (intent.getAction().equals("ACTION_CURRENTTIME")) {
int Currentime = intent.getIntExtra("action_currentime", 0);
my_seekbar.setProgress(Currentime);
start_time.setText(setTime(Currentime));
}//得到歌词显示进度
else if(intent.getAction().equals("ACTION_TEXTGC")){
String str_lrc = intent.getStringExtra("gc");
text_gc.setText(str_lrc);
}
}
}
public void zhuce() {
//描述Intent的属性
IntentFilter mintentfilter = new IntentFilter();
mintentfilter.addAction("ACTION_ALLTIME");
mintentfilter.addAction("ACTION_CURRENTTIME");
mintentfilter.addAction("ACTION_TEXTGC");
registerReceiver(mybrodcast, mintentfilter);
}
public String setTime(int time) {
time /= 1000;
int minute = time / 60;
int hour = minute / 60;
int second = time % 60;
minute %= 60;
return String.format("%02d:%02d", minute, second);
}
}