课程表

Android Studio

Android SDK

工具箱
速查手册

安卓 Broadcast Receivers

当前位置:免费教程 » 移动开发 » Android

广播接收器只是响应来自其他应用程序或系统本身的广播消息.这些消息有时被称为事件或意图.例如,应用程序还可以启动广播,让其他应用程序知道某些数据已下载到设备并可供他们使用,因此广播接收者将拦截此通信并启动适当的操作.

以下两个重要步骤使BroadcastReceiver适用于系统广播的意图 :

  • 创建广播接收器.

  • 注册广播接收器

如果您要实现自定义意图,还有一个额外的步骤,那么您将不得不创建并广播这些意图.

创建广播接收器

广播接收器实现为 BroadcastReceiver 类的子类,并覆盖onReceive()方法,其中每条消息作为 Intent 对象参数接收.

  1. public class MyReceiver extends BroadcastReceiver {
  2.    @Override
  3.    public void onReceive(Context context, Intent intent) {
  4.       Toast.makeText(context, "Intent Detected.", Toast.LENGTH_LONG).show();
  5.    }
  6. }

注册广播接收器

应用程序通过以下方式侦听特定的广播意图在 AndroidManifest.xml 文件中注册广播接收器。考虑我们要为系统生成的事件ACTION_BOOT_COMPLETED注册MyReceiver,系统会在Android系统完成启动过程后触发该事件。

1.jpg

广播接收器

  1. <application
  2.    android:icon="@drawable/ic_launcher"
  3.    android:label="@string/app_name"
  4.    android:theme="@style/AppTheme" >
  5.    <receiver android:name="MyReceiver">
  6.    
  7.       <intent-filter>
  8.          <action android:name="android.intent.action.BOOT_COMPLETED">
  9.          </action>
  10.       </intent-filter>
  11.    
  12.    </receiver>
  13. </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)方法,则意图粘性,这意味着您发送的意图在广播完成后保持不变.

  1. public void broadcastIntent(View view) {
  2.    Intent intent = new Intent();
  3.    intent.setAction("com.IT屋.CUSTOM_INTENT");
  4.    sendBroadcast(intent);
  5. }

这个意图 com.it1352.CUSTOM_INTENT 也可以像注册系统一样注册生成的意图.

  1. <application
  2.    android:icon="@drawable/ic_launcher"
  3.    android:label="@string/app_name"
  4.    android:theme="@style/AppTheme" >
  5.    <receiver android:name="MyReceiver">
  6.    
  7.       <intent-filter>
  8.          <action android:name="com.IT屋.CUSTOM_INTENT">
  9.          </action>
  10.       </intent-filter>
  11.    
  12.    </receiver>
  13. </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()方法来广播自定义意图.

  1. package com.example.it13527.myapplication;
  2.  
  3. import android.app.Activity;
  4. import android.content.Intent;
  5. import android.os.Bundle;
  6. import android.view.View;
  7.  
  8. public class MainActivity extends Activity {
  9.  
  10.    /** Called when the activity is first created. */
  11.    @Override
  12.    
  13.    public void onCreate(Bundle savedInstanceState) {
  14.       super.onCreate(savedInstanceState);
  15.       setContentView(R.layout.activity_main);
  16.    }
  17.  
  18.  
  19.    // broadcast a custom intent.
  20.       
  21.    public void broadcastIntent(View view){
  22.       Intent intent = new Intent();
  23.       intent.setAction("com.it1352.CUSTOM_INTENT"); sendBroadcast(intent);
  24.    }
  25. }

以下是 MyReceiver.java 的内容:

  1. package com.example.it13527.myapplication;
  2.  
  3. import android.content.BroadcastReceiver;
  4. import android.content.Context;
  5. import android.content.Intent;
  6. import android.widget.Toast;
  7.  
  8. public class MyReceiver extends BroadcastReceiver{
  9.    @Override
  10.    public void onReceive(Context context, Intent intent) {
  11.       Toast.makeText(context, "Intent Detected.", Toast.LENGTH_LONG).show();
  12.    }
  13. }

以下是 AndroidManifest.xml 文件的修改内容.这里我们添加了< receiver .../>标记包括我们的服务:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"
  3.    package="com.example.it13527.myapplication">
  4.  
  5.    <application
  6.       android:allowBackup="true"
  7.       android:icon="@mipmap/ic_launcher"
  8.       android:label="@string/app_name"
  9.       android:supportsRtl="true"
  10.       android:theme="@style/AppTheme">
  11.       <activity android:name=".MainActivity">
  12.          <intent-filter>
  13.             <action android:name="android.intent.action.MAIN" />
  14.  
  15.             <category android:name="android.intent.category.LAUNCHER" />
  16.          </intent-filter>
  17.       </activity>
  18.    
  19.       <receiver android:name="MyReceiver">
  20.          <intent-filter>
  21.             <action android:name="com.it1352.CUSTOM_INTENT">
  22.             </action>
  23.          </intent-filter>
  24.  
  25.       </receiver>
  26.    </application>
  27.  
  28. </manifest>

以下是 res/layout/activity_main.xml 文件的内容,包括一个用于广播我们的自定义意图的按钮 :

  1. <RelativeLayout 
  2.    xmlns:android="http://schemas.android.com/apk/res/android"
  3.    xmlns:tools="http://schemas.android.com/tools" 
  4.    android:layout_width="match_parent"
  5.    android:layout_height="match_parent" 
  6.    android:paddingLeft="@dimen/activity_horizontal_margin"
  7.    android:paddingRight="@dimen/activity_horizontal_margin"
  8.    android:paddingTop="@dimen/activity_vertical_margin"
  9.    android:paddingBottom="@dimen/activity_vertical_margin" 
  10.    tools:context=".MainActivity">
  11.    
  12.    <TextView
  13.       android:id="@+id/textView1"
  14.       android:layout_width="wrap_content"
  15.       android:layout_height="wrap_content"
  16.       android:text="Example of Broadcast"
  17.       android:layout_alignParentTop="true"
  18.       android:layout_centerHorizontal="true"
  19.       android:textSize="30dp" />
  20.       
  21.    <TextView
  22.       android:id="@+id/textView2"
  23.       android:layout_width="wrap_content"
  24.       android:layout_height="wrap_content"
  25.       android:text="Tutorials point "
  26.       android:textColor="#ff87ff09"
  27.       android:textSize="30dp"
  28.       android:layout_above="@+id/imageButton"
  29.       android:layout_centerHorizontal="true"
  30.       android:layout_marginBottom="40dp" />
  31.       
  32.    <ImageButton
  33.       android:layout_width="wrap_content"
  34.       android:layout_height="wrap_content"
  35.       android:id="@+id/imageButton"
  36.       android:src="@drawable/abc"
  37.       android:layout_centerVertical="true"
  38.       android:layout_centerHorizontal="true" />
  39.       
  40.    <Button
  41.       android:layout_width="wrap_content"
  42.       android:layout_height="wrap_content"
  43.       android:id="@+id/button2"
  44.       android:text="Broadcast Intent"
  45.       android:onClick="broadcastIntent"
  46.       android:layout_below="@+id/imageButton"
  47.       android:layout_centerHorizontal="true" />
  48.  
  49. </RelativeLayout>

让我们尝试运行我们刚修改过的修改过的 Hello World!应用程序.我假设您在进行环境设置时创建了 AVD .要从Android工作室运行应用程序,请打开项目的一个活动文件,然后单击运行Eclipse Run Icon icon从工具栏. Android Studio会在您的AVD上安装该应用并启动它,如果您的设置和应用程序一切正常,它将显示以下模拟器窗口 :

1.png

现在播放我们的自定义意图,让我们点击广播意图按钮,这将播放我们的自定义意图"com.it1352.CUSTOM_INTENT"将被我们注册的BroadcastReceiver拦截,即MyReceiver,根据我们实施的逻辑,toast将出现在模拟器的底部,如下所示:

2.png

您可以尝试实现其他BroadcastReceiver来拦截系统生成的意图,如系统启动,更改日期,电量不足等。

转载本站内容时,请务必注明来自W3xue,违者必究。
 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号