Android 用 broadcast receiver详解用例
Android 用 broadcast receiver详解用例
一、BroadcastReceiver概述
1.BroadcastReceiver定义
Broadcast(广播机制) 是一种广泛运用的应用程序之间 传输信息 的机制,而 BroadcastReceiver(广播接收器) 则是用于接收来自系统和应用的广播对并对其进行响应的组件,Android中我们要发送的广播内容是一个Intent,这个Intent中可以携带我们要传送的数据。
2.Broadcast类型
2.1普通广播:
它对于多个接收者来说是完全异步的,每个接受者不需要等待就可以接收到广播,接收者之间无法相互影响,也无法终止广播;
2.2有序广播:
顾名思义,它只发送到优先级较高的接收者那里,然后再由其转播到优先级低的接收者那里,同时,优先级高的接收者可以终止这个广播。例如短信可以被拦截。
2.3粘性广播:
就是假如接收者这边没有启动,但是发出了广播,然后接收者这边才启动,但是接收者依然可以接收到这个广播。
3.BroadcastReceiver注册方式
3.1动态注册
动态注册广播接收器特点是当用来注册的Activity关掉后,广播也就失效了。
3.2静态注册
静态注册无需担忧广播接收器是否被关闭,只要设备是开启状态,广播接收器也是打开着的。也就是说哪怕app本身未启动,该app订阅的广播在触发时也会对它起作用。
二、应用实例(音乐盒)
要求:
实现歌曲的切换,播放暂停,显示歌曲简单信息
事件监听控制:
MainActivity.java文件
1、监听4个按钮的点击事件,发生点击事件就发送广播携带相应信息(control的值);
2、自定义一个BroadcastReceiver负责监听从Service中传回的广播;
3、根据传回广播中携带的信息(update、current)设置控制系统的状态,更改界面中播放键的图案显示和文本显示的歌曲信息。
MusicService.java文件
1、控制音乐播放;
2、监听每首歌是否播完,播完改变current的值,接着播放下一首,同时发送广播(current)告诉Activity歌曲已更换,页面内容得更换;
3、接收Activity发出的广播,根据广播携带的信息(control的值)改变标识状态的变量(status)的值,同时控制音乐是否播放/暂停,控制当前音乐播放哪首歌(current);
4、把更改过的状态值和current值用广播传给Activity,让Activity做界面变动。
部分代码如下(示例):
布局文件:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageButton
android:id="@+id/previous"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@android:drawable/ic_media_previous" />
<ImageButton
android:id="@+id/play"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@android:drawable/ic_media_play" />
<ImageButton
android:id="@+id/stop"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@android:drawable/ic_media_pause" />
<ImageButton
android:id="@+id/next"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@android:drawable/ic_media_next" />
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20dp"
android:textColor="#000000"
android:ellipsize="marquee"
android:layout_weight="1"
android:marqueeRepeatLimit="marquee_forever"/>
<TextView
android:id="@+id/author"
android:textSize="12dp"
android:gravity="center_vertical"
android:layout_weight="1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
mediaPlayer = MediaPlayer.create(this,R.raw.white);
//创建音乐播放器
onCreate:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
activityReceiver = new ActivityReceiver();
IntentFilter filter = new IntentFilter();
//指定BroadCastReceiver监听的action
filter.addAction(UPDATE_ACTION);
registerReceiver(activityReceiver,filter);
Intent intent = new Intent(this,MusicService.class);
startService(intent);
//找到对应控件
play = this.findViewById(R.id.imgplay);
stop = this.findViewById(R.id.imgstop);
next = this.findViewById(R.id.imgnext);
last = this.findViewById(R.id.imglast);
title = this.findViewById(R.id.txttitle);
author = this.findViewById(R.id.txtauthor);
//添加监听
play.setOnClickListener(this);
stop.setOnClickListener(this);
next.setOnClickListener(this);
last.setOnClickListener(this);
}
MainActivity.java:
Intent intent = getIntent(); //获取广播意图对象
isCast = intent.getBooleanExtra("iscast", false); //默认值为false
btnStop.setEnabled(isCast); //设置停止按钮可用和单击监听
if(isCast) Toast.makeText(this, "正在播放音乐...", Toast.LENGTH_SHORT).show();
btnStop.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v){
Intent intent=new Intent(MainActivity.this,MyAudioService.class);
//在Activity组件里,停止音乐播放服务
stopService(intent);
finish(); //销毁本活动
}
});
MusicService.java:
@Override
public void onClick(View source) {
//创建Intent
Intent intent=new Intent("org.crazyit.action.CTL_ACTION");
switch (source.getId())
{
//按下播放/暂停按钮
case R.id.play:
intent.putExtra("control",1);
break;
//按下停止按钮
case R.id.stop:
intent.putExtra("control",2);
break;
//按下上一首按钮
case R.id.last:
intent.putExtra("control",3);
break;
//按下下一首按钮
case R.id.next:
intent.putExtra("control",4);
break;
}
//发送广播,将被Service组件中的BroadcastReceiver接收到
sendBroadcast(intent);
}
##界面效果:
总结
原本是想实现自动获取音乐消息,最后现实不了,只能手动导入对应歌曲的基本信息。
本文大部分参考了Android中对broadcast receiver组件部分的描述,希望本文对大家更好地使用broadcast receiver对象有所帮助。
如果有疑问,欢迎大家在评论中给我留言,看到会及时回复。
作者:林嘉豪
原文链接:https://blog.csdn.net/weixin_51513528/article/details/121893422