android 开机广播拦截,Android广播接收器

广播接收器

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

以下两个重要步骤使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系统完成启动过程后由系统触发。

205894578bd01ec1b0f671d9d8ca0987.png

广播接收器

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工作室运行应用程序,请打开项目的某个活动文件,然后单击

76338ddfc058aba988fb4a0c0612fde3.png工具栏中的“运行”

图标。Android Studio在您的AVD上安装应用程序并启动它,如果您的设置和应用程序一切正常,它将显示以下模拟器窗口 -

88ddd4c8f06021c2060b89237c8cfd3a.png

现在播放我们的自定义意图,让我们点击 Broadcast Intent 按钮,这将播放我们的自定义意图 _“com.codingdict.CUSTOMINTENT”

,它将被我们注册的BroadcastReceiver拦截,即MyReceiver,根据我们实施的逻辑,toast将出现在底部模拟器如下 -

327e79632986a598e0a090fcda92b8c7.png

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值