广播接收器
只响应来自其他应用程序或系统本身的广播消息。这些消息有时称为事件或意图。例如,应用程序还可以启动广播,让其他应用程序知道某些数据已下载到设备并可供他们使用,因此广播接收器将拦截此通信并启动适当的操作。
以下两个重要步骤使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系统完成启动过程后由系统触发。
广播接收器
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
现在,只要你的Android设备被启动,它就会被BroadcastReceiver MyReceiver 截获,并且 onReceive()
内部实现的逻辑将被执行。
有几个系统生成的事件被定义为 Intent 类中的最终静态字段。下表列出了一些重要的系统事件。
序号
事件常量和描述
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) 方法,则Intent是 粘性的 ,这意味着您发送的 Intent
在广播完成后保持不变。
public void broadcastIntent(View view) {
Intent intent = new Intent();
intent.setAction("com.codingdict.CUSTOM_INTENT");
sendBroadcast(intent);
}
这个意图 _com.codingdict.CUSTOMINTENT 也可以像我们有regsitered系统生成的意图一样注册。
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
例
此示例将向您解释如何创建 BroadcastReceiver
以拦截自定义意图。一旦熟悉了自定义意图,就可以对应用程序进行编程,以拦截系统生成的意图。因此,让我们按照以下步骤修改我们在 Hello World示例
章节中创建的Android应用程序-
步
描述
1
您将使用Android studio创建一个Android应用程序,并在com.example.codingdict7.myapplication包下将其命名为My Application,如Hello World示例章节中所述。
2
修改主活动文件MainActivity.java以添加broadcastIntent()方法。
3
在com.example.codingdict7.myapplication包下创建一个名为MyReceiver.java的新Java文件,以定义BroadcastReceiver。
4
应用程序可以无任何限制地处理一个或多个自定义和系统意图。您要拦截的每个意图都必须使用标记在AndroidManifest.xml文件中注册
5
修改res / layout / activity_main.xml文件的默认内容以包含用于广播意图的按钮。
6
无需修改字符串文件,Android studio负责处理string.xml文件。
7
运行应用程序以启动Android模拟器并验证应用程序中所做更改的结果。
以下是已修改的主活动文件 MainActivity.java 的内容。该文件可以包括每个基本生命周期方法。我们添加了
broadcastIntent() 方法来广播自定义意图。
package com.example.codingdict7.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.codingdict.CUSTOM_INTENT"); sendBroadcast(intent);
}
}
以下是 MyReceiver.java 的内容:
package com.example.codingdict7.myapplication;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.widget.Toast;
/**
* Created by codingdict7 on 8/23/2016.
*/
public class MyReceiver extends BroadcastReceiver{
@Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(context, "Intent Detected.", Toast.LENGTH_LONG).show();
}
}
以下是 AndroidManifest.xml 文件的修改内容 。这里我们添加了标签来包含我们的服务:
package="com.example.codingdict7.myapplication">
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
以下是 res / layout / activity_main.xml 文件的内容,包括一个用于广播我们自定义意图的按钮 -
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">
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" />
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" />
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" />
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" />
让我们尝试运行我们修改过的 Hello World! 刚刚修改过的应用程序 我假设您在进行环境设置时创建了 AVD
。要从Android工作室运行应用程序,请打开项目的某个活动文件,然后单击
工具栏中的“运行”
图标。Android Studio在您的AVD上安装应用程序并启动它,如果您的设置和应用程序一切正常,它将显示以下模拟器窗口 -
现在播放我们的自定义意图,让我们点击 Broadcast Intent 按钮,这将播放我们的自定义意图 _“com.codingdict.CUSTOMINTENT”
,它将被我们注册的BroadcastReceiver拦截,即MyReceiver,根据我们实施的逻辑,toast将出现在底部模拟器如下 -
您可以尝试实现其他BroadcastReceiver来拦截系统生成的意图,如系统启动,更改日期,电池电量不足等。