
Broadcast是一种广泛运用的在应用程序之间传输信息的机制。我们拿广播电台来做个比方。我们平常使用收音机收音是这样的:许许多多不同的广播电台通过特定的频率来发送他们的内容,而我们用户只需要将频率调成和广播电台的一样就可以收听他们的内容了。Android中的广播机制就和这个差不多的道理。
电台发送的内容是语音,而在Android中我们要发送的广播内容是一个Intent。这个Intent中可以携带我们要传送的数据。
电台通过大功率的发射器发送内容,而在Android中则是通过sendBroadcast这个方法来发送(很形象的名字吧)。
用户通过调整到具体的电台频率接受电台的内容。而在Android中要接受广播中的内容则是通过注册一个BroadCastReceiver来接收的。只有发送广播的action和接收广播的action相同,接受者才能接受这个广播。
广播有什么用
其实,在什么是广播的第一句就已经说明了广播有什么用了。对了,笼统一点讲就是用来传输数据的。具体一点说就是:
1.实现了不同的程序之间的数据传输与共享,因为只要是和发送广播的action相同的接受者都能接受这个广播。典型的应用就是android自带的短信,电话等等广播,只要我们实现了他们的action的广播,那么我们就能接收他们的数据了,以便做出一些处理。比如说拦截系统短信,拦截骚扰电话等等
2.起到了一个通知的作用,比如在service中要通知主程序,更新主程序的UI等。因为service是没有界面的,所以不能直接获得主程序中的控件,这样我们就只能在主程序中实现一个广播接受者专门用来接受service发过来的数据和通知了。
Demo 分析:
下面通过一个小DEMO 来讲解一下广播在Android 中如何编写,在Demo中我们设置了一个按钮为按钮设置点击监听通过点击发送广播,在后台中接收到广播并打印LOG信息。代码如下:
activity:
public class MainActivityextends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void send(View view){
Intent intent =new Intent(this,MyBroadcastReceiver.class);
//定义广播的事件类型
intent.setAction("Help_stitch");
//发送广播
sendBroadcast(intent);
}
@Override
protected void onDestroy() {
super.onDestroy();
}
}
布局xml:
<?xml version="1.0" encoding="utf-8"?>
< LinearLayout xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<Button android:id="@+id/sendBtn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="send"
android:text="发送"
tools:ignore="MissingConstraints">
</Button>
</LinearLayout>
广播接收器:
public class MyBroadcastReceiverextends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Log.i("MyBroadcastReceiver","自定义广播接受者,收到了求救广播事件");
Log.i("MyBroadcastReceiver", intent.getAction());
Toast.makeText(context,"收到广播", Toast.LENGTH_SHORT).show();
}
}
AndroidMainifest.xml文件 :
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
package="com.example.broadcastdemo">
<uses-permission android:name="android.permission.WRITE_CALENDAR" />
<uses-permission android:name="android.permission.BROADCAST_STICKY" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.BroadcastDemo">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver
android:name=".MyBroadcastReceiver"
android:enabled="true"
android:exported="true"
tools:ignore="MissingClass,WrongManifestParent" android:permission="android.permission.PROCESS_OUTGOING_CALLS">
<intent-filter>
<action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
</intent-filter>
</receiver>
</application>
</manifest>
接下来就是你来尝试一次的时候了!以上整理部分来自网络!