标签:broadcast mediaplayer 音乐播放器
公司项目中要使用webview下载程序,所以必须用到广播接收器,配合service下载。
忘记的东西重来一遍。
后面有最经典的音乐播放器的源码,Service BroadCastReeiver都用到了
简单4步,动态加载广播
1.定义一个广播接收器
public class MyReceiver extends BroadcastReceiver
{
public MyReceiver()
{
Log.i(TAG,"MyReceiver");
}
//可用Intent的getAction()区分接收到的不同广播
@Override
public void onReceive(Context arg0, Intent arg1)
{
String
action=intent.getAction();
//TO-DO
LIST
}
}
2.发送广播,定义好action标志,用Intent发送
//实例化该BroadcastReceiver
MyReceiver mReceiver=new MyReceiver();
//设置唯一action,用Intent发送
Intent intent=new Intent();
intent.setAction(str);
sendBroadcast(intent);
3.注册只接收指定action的广播接收器
IntentFilter filter1=new IntentFilter();
filter1.addAction(str);
registerReceiver(mReceiver,filter1);
4.取消该广播接收器
unregisterReceiver(mReceiver);
1.广播的定义
广播接收程序是android四大组件之一,可对客户端发出的广播消息作出响应的组件,消息本身是一个intent,可由多个接收程序接收
2.注册方式
a.动态注册,即通过程序注册(Context.registerReceiver()),程序退出后广播即实效
b.静态注册,即在manifest.xml中通过receiver tag注册,可作为全局广播
注意:对于动态注册,如果不执行Context.unregisterReceiver,也可作为全局广播,通常,在项目中,一般把注册放在Activity.onResume方法中,把取注册放在Activity.onPause中
3.发送广播
广播有两种接收方式
a.普通广播(Context.sendBroadcast)
完全异步的,所有的广播接收者在同一时刻以未定义的顺序运行。
b.顺序广播(Context.sendOrderedBroadcast)
所有的广播接收者按顺序执行,其中一个执行的广播接受者可以将结果传递给下一个广播接受者,也可以退出广播以便于不将结果传递到下一个广播接受者,可以在manifest.xml中使用android:priority属性来确定广播接收者的优先级。同一优先级的接受者将以随机顺序执行
二、广播高级话题
1.安全性
a.确保Intent的Action名称是你自己应用的命名空间,否则你会在不经意间和其他应用冲突
b.当你使用 registerReceiver(BroadcastReceiver,
IntentFilter)时,其他应用也能够发送相关广播到那个注册的接收者中,这时你应
该使用相关权限来控制谁能够发送广播到那个接收者
c.可以使用android:exported="false"来防止其他应用发送广播到你发布的广播接收者中
d.当你使用sendBroadcast(Intent)时,通过一些权限控制来阻止发送广播到其他接收者中,从android4.0开始可以用Intent.setPackage
2.接收者生命周期
一个BroadcastReceiver对象的生命周期仅存在于onReceive方法中,当你的代码从该方法中返回时,系统会认为该接收者对象不再有效
不要在onReceive方法中进行一些异步操作的处理,因为该方法结束后整个接收者的生命周期就已经结束了
不要在onReceive方法中显示一个dialog或者绑定service,对于前者,你可以使用NotificationManager,而对于后者使用Context.startService来发送一个命令到service中
3.进程生命周期
当前进程是指执行该广播接收程序的进程,它是一个前台进程,即使该广播生命周期结束后,也会继续运行,直到系统内存紧张被回收
如果当广播接收程序生命周期结束后,该进程就会变为空进程,有可能会被系统回收,你可以结合service和broadcast receiver使用,这样可以使该进程不被回收
三、demo示例
1.动态注册
该程序中,当点击按钮时会发送一个广播,在该广播中执行在控制台打印广播参数的操作,并且每隔10秒发送一次广播,当退出程序后该广播生命周期结束,不会再在控制台打印
2.静态注册
a.项目结构
b.MainActivity
c.TestReceiver2
d.AndroidManefist.xml
e.运行结果
点击按钮后回在logcat中一直打印消息,无论程序是否退出
最经典的音乐播放器的例子:源码
-
package com.wwj.sb.service;
-
-
import java.util.List;
-
-
import android.annotation.SuppressLint;
-
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.media.MediaPlayer.OnCompletionListener;
-
import android.media.MediaPlayer.OnPreparedListener;
-
import android.os.Handler;
-
import android.os.IBinder;
-
import android.util.Log;
-
-
import com.wwj.sb.activity.PlayerActivity;
-
import com.wwj.sb.domain.AppConstant;
-
import com.wwj.sb.domain.Mp3Info;
-
import com.wwj.sb.utils.MediaUtil;
-
-
-
-
-
-
@SuppressLint("NewApi")
-
public class PlayerService extends Service {
-
private MediaPlayer mediaPlayer;
-
private String path;
-
private int msg;
-
private boolean isPause;
-
private int current = 0;
-
private List<Mp3Info> mp3Infos;
-
private int status = 3;
-
private MyReceiver myReceiver;
-
private int currentTime;
-
private int duration;
-
-
-
public static final String UPDATE_ACTION = "com.wwj.action.UPDATE_ACTION";
-
public static final String CTL_ACTION = "com.wwj.action.CTL_ACTION";
-
public static final String MUSIC_CURRENT = "com.wwj.action.MUSIC_CURRENT";
-
public static final String MUSIC_DURATION = "com.wwj.action.MUSIC_DURATION";
-
-
-
-
-
private Handler handler = new Handler() {
-
public void handleMessage(android.os.Message msg) {
-
if (msg.what == 1) {
-
if(mediaPlayer != null) {
-
currentTime = mediaPlayer.getCurrentPosition();
-
Intent intent = new Intent();
-
intent.setAction(MUSIC_CURRENT);
-
intent.putExtra("currentTime", currentTime);
-
sendBroadcast(intent);
-
handler.sendEmptyMessageDelayed(1, 1000);
-
}
-
-
}
-
};
-
};
-
-
@Override
-
public void onCreate() {
-
super.onCreate();
-
Log.d("service", "service created");
-
mediaPlayer = new MediaPlayer();
-
mp3Infos = MediaUtil.getMp3Infos(PlayerService.this);
-
-
-
-
-
-
mediaPlayer.setOnCompletionListener(new OnCompletionListener() {
-
-
@Override
-
public void onCompletion(MediaPlayer mp) {
-
if (status == 1) {
-
mediaPlayer.start();
-
} else if (status == 2) {
-
current++;
-
if(current > mp3Infos.size() - 1) {
-
current = 0;
-
}
-
Intent sendIntent = new Intent(UPDATE_ACTION);
-
sendIntent.putExtra("current", current);
-
-
sendBroadcast(sendIntent);
-
path = mp3Infos.get(current).getUrl();
-
play(0);
-
} else if (status == 3) {
-
current++;
-
if (current <= mp3Infos.size() - 1) {
-
Intent sendIntent = new Intent(UPDATE_ACTION);
-
sendIntent.putExtra("current", current);
-
-
sendBroadcast(sendIntent);
-
path = mp3Infos.get(current).getUrl();
-
play(0);
-
}else {
-
mediaPlayer.seekTo(0);
-
current = 0;
-
Intent sendIntent = new Intent(UPDATE_ACTION);
-
sendIntent.putExtra("current", current);
-
-
sendBroadcast(sendIntent);
-
}
-
} else if(status == 4) {
-
current = getRandomIndex(mp3Infos.size() - 1);
-
System.out.println("currentIndex ->" + current);
-
Intent sendIntent = new Intent(UPDATE_ACTION);
-
sendIntent.putExtra("current", current);
-
-
sendBroadcast(sendIntent);
-
path = mp3Infos.get(current).getUrl();
-
play(0);
-
}
-
}
-
});
-
-
myReceiver = new MyReceiver();
-
IntentFilter filter = new IntentFilter();
-
filter.addAction(PlayerActivity.CTL_ACTION);
-
registerReceiver(myReceiver, filter);
-
}
-
-
-
-
-
-
-
protected int getRandomIndex(int end) {
-
int index = (int) (Math.random() * end);
-
return index;
-
}
-
-
@Override
-
public IBinder onBind(Intent arg0) {
-
return null;
-
}
-
-
@Override
-
public void onStart(Intent intent, int startId) {
-
path = intent.getStringExtra("url");
-
current = intent.getIntExtra("listPosition", -1);
-
msg = intent.getIntExtra("MSG", 0);
-
if (msg == AppConstant.PlayerMsg.PLAY_MSG) {
-
play(0);
-
} else if (msg == AppConstant.PlayerMsg.PAUSE_MSG) {
-
pause();
-
} else if (msg == AppConstant.PlayerMsg.STOP_MSG) {
-
stop();
-
} else if (msg == AppConstant.PlayerMsg.CONTINUE_MSG) {
-
resume();
-
} else if (msg == AppConstant.PlayerMsg.PRIVIOUS_MSG) {
-
previous();
-
} else if (msg == AppConstant.PlayerMsg.NEXT_MSG) {
-
next();
-
} else if (msg == AppConstant.PlayerMsg.PROGRESS_CHANGE) {
-
currentTime = intent.getIntExtra("progress", -1);
-
play(currentTime);
-
} else if (msg == AppConstant.PlayerMsg.PLAYING_MSG) {
-
handler.sendEmptyMessage(1);
-
}
-
super.onStart(intent, startId);
-
}
-
-
-
-
-
-
-
private void play(int currentTime) {
-
try {
-
mediaPlayer.reset();
-
mediaPlayer.setDataSource(path);
-
mediaPlayer.prepare();
-
mediaPlayer.setOnPreparedListener(new PreparedListener(currentTime));
-
handler.sendEmptyMessage(1);
-
} catch (Exception e) {
-
e.printStackTrace();
-
}
-
}
-
-
-
-
-
private void pause() {
-
if (mediaPlayer != null && mediaPlayer.isPlaying()) {
-
mediaPlayer.pause();
-
isPause = true;
-
}
-
}
-
-
private void resume() {
-
if (isPause) {
-
mediaPlayer.start();
-
isPause = false;
-
}
-
}
-
-
-
-
-
private void previous() {
-
Intent sendIntent = new Intent(UPDATE_ACTION);
-
sendIntent.putExtra("current", current);
-
-
sendBroadcast(sendIntent);
-
play(0);
-
}
-
-
-
-
-
private void next() {
-
Intent sendIntent = new Intent(UPDATE_ACTION);
-
sendIntent.putExtra("current", current);
-
-
sendBroadcast(sendIntent);
-
play(0);
-
}
-
-
-
-
-
private void stop() {
-
if (mediaPlayer != null) {
-
mediaPlayer.stop();
-
try {
-
mediaPlayer.prepare();
-
} catch (Exception e) {
-
e.printStackTrace();
-
}
-
}
-
}
-
-
@Override
-
public void onDestroy() {
-
if (mediaPlayer != null) {
-
mediaPlayer.stop();
-
mediaPlayer.release();
-
mediaPlayer = null;
-
}
-
-
}
-
-
-
-
-
-
-
private final class PreparedListener implements OnPreparedListener {
-
private int currentTime;
-
-
public PreparedListener(int currentTime) {
-
this.currentTime = currentTime;
-
}
-
-
@Override
-
public void onPrepared(MediaPlayer mp) {
-
mediaPlayer.start();
-
if (currentTime > 0) {
-
mediaPlayer.seekTo(currentTime);
-
}
-
Intent intent = new Intent();
-
intent.setAction(MUSIC_DURATION);
-
duration = mediaPlayer.getDuration();
-
intent.putExtra("duration", duration);
-
sendBroadcast(intent);
-
}
-
}
-
-
public class MyReceiver extends BroadcastReceiver {
-
-
@Override
-
public void onReceive(Context context, Intent intent) {
-
int control = intent.getIntExtra("control", -1);
-
switch (control) {
-
case 1:
-
status = 1;
-
break;
-
case 2:
-
status = 2;
-
break;
-
case 3:
-
status = 3;
-
break;
-
case 4:
-
status = 4;
-
break;
-
}
-
}
-
}
-
-
}
安卓下载功能中的广播接收器
标签:broadcast mediaplayer 音乐播放器
原文地址:http://blog.csdn.net/kan1kan5/article/details/40164409