广播接收器只是响应来自其他应用程序或系统本身的广播消息.这些消息有时被称为事件或意图.例如,应用程序还可以启动广播,让其他应用程序知道某些数据已下载到设备并可供他们使用,因此广播接收者将拦截此通信并启动适当的操作.
以下两个重要步骤使BroadcastReceiver适用于系统广播的意图 :
创建广播接收器.
注册广播接收器
如果您要实现自定义意图,还有一个额外的步骤,那么您将不得不创建并广播这些意图.
创建广播接收器
广播接收器实现为 BroadcastReceiver 类的子类,并覆盖onReceive()方法,其中每条消息作为 Intent 对象参数接收.
- public class MyReceiver extends BroadcastReceiver {
- @Override
- public void onReceive(Context context, Intent intent) {
- Toast.makeText(context, "Intent Detected.", Toast.LENGTH_LONG).show();
- }
- }
注册广播接收器
应用程序通过以下方式侦听特定的广播意图在 AndroidManifest.xml 文件中注册广播接收器。考虑我们要为系统生成的事件ACTION_BOOT_COMPLETED注册MyReceiver,系统会在Android系统完成启动过程后触发该事件。
广播接收器
- <application
- android:icon="@drawable/ic_launcher"
- android:label="@string/app_name"
- android:theme="@style/AppTheme" >
- <receiver android:name="MyReceiver">
- <intent-filter>
- <action android:name="android.intent.action.BOOT_COMPLETED">
- </action>
- </intent-filter>
- </receiver>
- </application>
现在,只要你的Android设备被启动,它就会被BroadcastReceiver MyReceiver 拦截,并在 onReceive()中实现逻辑将被执行。
有几个系统生成的事件被定义为 Intent 类中的最终静态字段.下表列出了一些重要的系统事件。
Sr.No | 事件常数&描述 |
---|---|
1 | android.intent.action.BATTERY_CHANGED 粘性广播包含有关的充电状态,级别和其他信息电池. |
2 | android.intent.action.BATTERY_LOW 表示设备电池电量不足. |
3 | android.intent.action.BATTERY_OKAY 表示电池电量低后现在没问题. |
4 | android.intent.action.BOOT_COMPLETED 在系统完成启动后,这是广播一次. |
5 | android.intent.action.BUG_REPORT 显示活动报告错误. |
6 | android.intent.action.CALL 执行对数据指定人员的呼叫. |
7 | android.intent.action.CALL_BUTTON 用户按下"呼叫"按钮转到拨号器或其他适当的用于发出呼叫的UI. |
8 | android.intent.action.DATE_CHANGED 日期已更改. |
9 | android.intent.action.REBOOT 让设备重启. |
广播自定义意图
如果您希望应用程序本身生成并发送自定义意图,那么您必须使用里面的 sendBroadcast()方法创建并发送这些意图你的活动课.如果您使用sendStickyBroadcast(Intent)方法,则意图粘性,这意味着您发送的意图在广播完成后保持不变.
- public void broadcastIntent(View view) {
- Intent intent = new Intent();
- intent.setAction("com.IT屋.CUSTOM_INTENT");
- sendBroadcast(intent);
- }
这个意图 com.it1352.CUSTOM_INTENT 也可以像注册系统一样注册生成的意图.
- <application
- android:icon="@drawable/ic_launcher"
- android:label="@string/app_name"
- android:theme="@style/AppTheme" >
- <receiver android:name="MyReceiver">
- <intent-filter>
- <action android:name="com.IT屋.CUSTOM_INTENT">
- </action>
- </intent-filter>
- </receiver>
- </application>
示例
此示例将向您解释如何创建 BroadcastReceiver 来拦截自定义意图.一旦熟悉了自定义意图,就可以对应用程序进行编程,以拦截系统生成的意图.因此,让我们按照以下步骤修改我们在 Hello World示例章节中创建的Android应用程序:
步骤 | 描述 |
---|---|
1 | 您将使用Android studio创建Android应用程序和名称它在 com.example.it13527.myapplication 下的我的应用程序,如 Hello World Example 章节中所述. |
2 | 修改主要活动文件 MainActivity.java 添加 broadcastIntent()方法. |
3 | 在包 com.example.it13527.myapplication <下创建一个名为 MyReceiver.java 的新java文件/i>定义BroadcastReceiver. |
4 | 应用程序可以无任何限制地处理一个或多个自定义和系统意图.您要拦截的每个意图都必须使用< receiver .../>在 AndroidManifest.xml 文件中注册. tag |
5 | 修改 res/layout/activity_main.xml 文件的默认内容,包括一个广播意图的按钮. |
6 | 无需修改字符串文件,Android studio负责处理string.xml文件. |
7 | 运行应用程序以启动Android模拟器并验证结果在应用程序中完成的更改. |
以下是修改后的主活动文件的内容 MainActivity.java 的.该文件可以包括每个基本生命周期方法.我们添加了 broadcastIntent()方法来广播自定义意图.
- package com.example.it13527.myapplication;
- import android.app.Activity;
- import android.content.Intent;
- import android.os.Bundle;
- import android.view.View;
- public class MainActivity extends Activity {
- /** Called when the activity is first created. */
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- }
- // broadcast a custom intent.
- public void broadcastIntent(View view){
- Intent intent = new Intent();
- intent.setAction("com.it1352.CUSTOM_INTENT"); sendBroadcast(intent);
- }
- }
以下是 MyReceiver.java 的内容:
- package com.example.it13527.myapplication;
- import android.content.BroadcastReceiver;
- import android.content.Context;
- import android.content.Intent;
- import android.widget.Toast;
- public class MyReceiver extends BroadcastReceiver{
- @Override
- public void onReceive(Context context, Intent intent) {
- Toast.makeText(context, "Intent Detected.", Toast.LENGTH_LONG).show();
- }
- }
以下是 AndroidManifest.xml 文件的修改内容.这里我们添加了< receiver .../>标记包括我们的服务:
- <?xml version="1.0" encoding="utf-8"?>
- <manifest xmlns:android="http://schemas.android.com/apk/res/android"
- package="com.example.it13527.myapplication">
- <application
- android:allowBackup="true"
- android:icon="@mipmap/ic_launcher"
- android:label="@string/app_name"
- android:supportsRtl="true"
- android:theme="@style/AppTheme">
- <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="MyReceiver">
- <intent-filter>
- <action android:name="com.it1352.CUSTOM_INTENT">
- </action>
- </intent-filter>
- </receiver>
- </application>
- </manifest>
以下是 res/layout/activity_main.xml 文件的内容,包括一个用于广播我们的自定义意图的按钮 :
- <RelativeLayout
- xmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:tools="http://schemas.android.com/tools"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:paddingLeft="@dimen/activity_horizontal_margin"
- android:paddingRight="@dimen/activity_horizontal_margin"
- android:paddingTop="@dimen/activity_vertical_margin"
- android:paddingBottom="@dimen/activity_vertical_margin"
- tools:context=".MainActivity">
- <TextView
- android:id="@+id/textView1"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="Example of Broadcast"
- android:layout_alignParentTop="true"
- android:layout_centerHorizontal="true"
- android:textSize="30dp" />
- <TextView
- android:id="@+id/textView2"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="Tutorials point "
- android:textColor="#ff87ff09"
- android:textSize="30dp"
- android:layout_above="@+id/imageButton"
- android:layout_centerHorizontal="true"
- android:layout_marginBottom="40dp" />
- <ImageButton
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:id="@+id/imageButton"
- android:src="@drawable/abc"
- android:layout_centerVertical="true"
- android:layout_centerHorizontal="true" />
- <Button
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:id="@+id/button2"
- android:text="Broadcast Intent"
- android:onClick="broadcastIntent"
- android:layout_below="@+id/imageButton"
- android:layout_centerHorizontal="true" />
- </RelativeLayout>
让我们尝试运行我们刚修改过的修改过的 Hello World!应用程序.我假设您在进行环境设置时创建了 AVD .要从Android工作室运行应用程序,请打开项目的一个活动文件,然后单击运行 icon从工具栏. Android Studio会在您的AVD上安装该应用并启动它,如果您的设置和应用程序一切正常,它将显示以下模拟器窗口 :
现在播放我们的自定义意图,让我们点击广播意图按钮,这将播放我们的自定义意图"com.it1352.CUSTOM_INTENT"将被我们注册的BroadcastReceiver拦截,即MyReceiver,根据我们实施的逻辑,toast将出现在模拟器的底部,如下所示:
您可以尝试实现其他BroadcastReceiver来拦截系统生成的意图,如系统启动,更改日期,电量不足等。
转载本站内容时,请务必注明来自W3xue,违者必究。