BroadcastReceiver监听步骤
1.实现BroadcastReceiver子类 ,并在androidmainfest.xml注册监听者,并设置action属性
public class MyReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(
context,
intent.getAction() + "\n消息的内容是:" + intent.getStringExtra("msg"),
Toast.LENGTH_LONG).show();
}
}
<receiver
android:name="com.android.xiong.bordcasetestonetest.MyReceiver">
<intent-filter>
<action android:name="com.android.xiong.bordcasetestonetest.MYRECEIVER"/>
</intent-filter>
</receiver>
2.创建Intent对象 并设置Action属性,然后广播消息(广播有两种 一种是普通广播,一种是 有序广播;有序广播是通过设置优先级来一个一个来接收广播信息 接收的后者可以得到前接收者的广播修改信息)
//创建Intent对象
Intent intent=new Intent();
intent.setAction("com.android.xiong.bordcasetestonetest.MYRECEIVER");
intent.putExtra("msg", "简单的消息");
//发送广播
sendBroadcast(intent);
实例
package com.example.musicbox;
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.Button;
import android.widget.TextView;
public class MainActivity extends Activity implements OnClickListener {
private TextView title, author;
private Button play, stop;
public static final String CTL_ACTION = "com.android.xiong.CTL_ACTION";
public static final String UPDATE_ACTION = "com.android.xiong.UPDATE_ACTION";
// 定义音乐的播放状态 0x11代表没有播放 0x12表示正在播放 0x13表示暂停中
int status = 0x11;
String[] titleStrs = new String[] { "心愿", "约定", "美丽的新世界" };
String[] authorStrs = new String[] { "未知艺术家", "周蕙", "五百" };
private ActivityReceiver activitys;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
title = (TextView) findViewById(R.id.title);
author = (TextView) findViewById(R.id.author);
play = (Button) findViewById(R.id.play);
stop = (Button) findViewById(R.id.stop);
play.setOnClickListener(this);
stop.setOnClickListener(this);
// 创建IntentFilter
IntentFilter filter = new IntentFilter();
filter.addAction(UPDATE_ACTION);
activitys = new ActivityReceiver();
registerReceiver(activitys, filter);
Intent intentService = new Intent(this, MusicService.class);
// 启动Service
startService(intentService);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
// 自定义的BoradcastReceiver,负责监听从Serive传回来的广播
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:
status = 0x11;
break;
// 控制系统进入播放状态
case 0x12:
status = 0x12;
break;
//控制系统进入暂停状态
case 0x13:
status = 0x13;
break;
}
}
}
@Override
public void onClick(View v) {
// 创建Intent
Intent intent = new Intent("com.android.xiong.CTL_ACTION");
switch (v.getId()) {
case R.id.play:
intent.putExtra("control", 1);
break;
case R.id.stop:
intent.putExtra("control", 2);
break;
default:
break;
}
//发送广播
sendBroadcast(intent);
}
}
package com.example.musicbox;
import android.app.Service;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.res.AssetFileDescriptor;
import android.content.res.AssetManager;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnCompletionListener;
import android.os.IBinder;
public class MusicService extends Service {
MyReceviver serviceReceviver;
AssetManager am;
String[] musics = new String[] { "wish.mp3", "promise.mp3", "beautiful.mp3" };
MediaPlayer mPlayer;
// 当前的状态,0x11代表没有播放;0x12代表正在播放;0x13代表暂停
int status = 0x11;
// 记录当前正在播放的音乐
int current = 0;
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public void onCreate() {
am = getAssets();
// 创建BroadcastRecer
serviceReceviver = new MyReceviver();
// 创建IntentFilter
IntentFilter filter = new IntentFilter();
filter.addAction(MainActivity.CTL_ACTION);
registerReceiver(serviceReceviver, filter);
// 创建MediaPlay播放完成事件绑定的监听器
mPlayer = new MediaPlayer();
mPlayer.setOnCompletionListener(new OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mp) {
current++;
if (current >= 3) {
current = 0;
}
// 发送广播通知的activity更改文本
Intent sendIntent = new Intent(MainActivity.UPDATE_ACTION);
sendIntent.putExtra("current", current);
// 发送广播,将被activity组件的BroadcastReceiver接收
sendBroadcast(sendIntent);
// 准备并播放音乐
preparAndplay(musics[current]);
}
});
super.onCreate();
}
public class MyReceviver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
int control = intent.getIntExtra("control", -1);
switch (control) {
//播放或暂停
case 1:
//原来处于没有播放状态
if(status==0x11){
//准备并播放音乐
preparAndplay(musics[current]);
status=0x12;
}
//原来处于播放的状态
else if(status==0x12){
//暂停
mPlayer.pause();
//改变为暂停的状态
status=0x13;
}
//原来处于暂停的状态
else if(status==0x13){
//播放
mPlayer.start();
//改变为播放状态
status=0x12;
}
break;
//停止播放
case 2:
//如果原来正在播放
if(status==0x12||status==0x13){
//停止播放
mPlayer.stop();
status=0x11;
}
default:
break;
}
//广播通知Activity更改文本
Intent sendIntent=new Intent(MainActivity.UPDATE_ACTION);
sendIntent.putExtra("update", status);
sendIntent.putExtra("current",current);
//发送广播
sendBroadcast(sendIntent);
}
}
private void preparAndplay(String music) {
try {
// 打开指定的文件
AssetFileDescriptor afd = am.openFd(music);
mPlayer.reset();
// 使用MediaPlayer加载指定的文件
mPlayer.setDataSource(afd.getFileDescriptor(),
afd.getStartOffset(), afd.getLength());
// 准备声音
mPlayer.prepare();
// 播放
mPlayer.start();
} catch (Exception e) {
e.printStackTrace();
}
}
}