当前位置: 首页 > news >正文

网站做批发文具百度提问首页

网站做批发文具,百度提问首页,网站开发心得,天津推广的平台Broadcast Receivers 仅响应来自其他应用程序或系统本身的广播消息,这些消息有时称为events或intents。例如,应用程序还可以启动广播,以使其他应用程序知道某些数据已下载到设备并可供他们使用,因此广播接收器将拦截此通信并启动适…

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

要使BroadcastReceiver用于系统的广播意图(intents),需要执行以下两个重要步骤-

  • 创建 Broadcast Receiver.

  • 注册 Broadcast Receiver.

如果要实现您的自定义意图(intents),还有另外一个步骤,那么您将必须创建并广播这些意图。

创建广播接收器

broadcast receiver 实现为 BroadcastReceiver 类的子类,并覆盖onReceive()方法,在该方法中,每个消息均作为 Intent 对象参数接收。

public class MyReceiver extends BroadcastReceiver {@Overridepublic void onReceive(Context context, Intent intent) {Toast.makeText(context, "Intent Detected.", Toast.LENGTH_LONG).show();}
}

注册广播接收器

应用程序通过在AndroidManifest.xml文件中注册广播接收器来侦听特定的广播意图。考虑一下,无涯教程将为系统生成的事件ACTION_BOOT_COMPLETED注册MyReceiver,一旦Android系统完成启动过程,系统就会触发该事件。

broadcast
<applicationandroid: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()中的已实现逻辑将被执行。

下表列出了一些重要的系统事件。

Sr.NoEvent Constant & 描述
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

显示报告错误的Activity。

6

android.intent.action.CALL

对数据指定的某人执行呼叫。

7

android.intent.action.CALL_BUTTON

用户按下"呼叫"按钮以转到拨号器或其他适当的UI来发出呼叫。

8

android.intent.action.DATE_CHANGED

日期已更改。

9

android.intent.action.REBOOT

重新启动设备。

自定义广播

如果您希望应用程序本身应生成并发送自定义意图,则必须使用Activity类内的 sendBroadcast()方法来创建并发送这些意图,如果您使用 sendStickyBroadcast(Intent)方法,这意味着您要发送的 Intent 会在广播完成后停留。

public void broadcastIntent(View view) {Intent intent = new Intent();intent.setAction("com.learnfk.CUSTOM_INTENT");sendBroadcast(intent);
}

该意图 com.learnfk.CUSTOM_INTENT 也可以通过与无涯教程重新注册系统生成的意图相同的方式进行注册。

<applicationandroid:icon="@drawable/ic_launcher"android:label="@string/app_name"android:theme="@style/AppTheme" ><receiver android:name="MyReceiver"><intent-filter><action android:name="com.learnfk.CUSTOM_INTENT"></action></intent-filter></receiver>
</application>

本示例将向您说明如何创建BroadcastReceiver来拦截自定义意图,熟悉自定义意图后,即可对应用程序进行编程以拦截系统生成的意图。

以下是修改后的主要Activity文件 MainActivity.java 的内容,该文件可以包括每个基本生命周期方法。添加了 broadcastIntent()方法来广播自定义意图。

package com.example.learnfk7.myapplication;import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;public class MainActivity extends Activity {/** 在第一次创建Activity时调用。 */@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);}//广播自定义意图。public void broadcastIntent(View view){Intent intent = new Intent();intent.setAction("com.learnfk.CUSTOM_INTENT"); sendBroadcast(intent);}
}

以下是 MyReceiver.java 的内容:

package com.example.learnfk7.myapplication;import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.widget.Toast;/*** Created by LearnFk7 on 8/23/2021.*/
public class MyReceiver extends BroadcastReceiver{@Overridepublic 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.learnfk7.myapplication"><applicationandroid: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.learnfk.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"><TextViewandroid: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" /><TextViewandroid:id="@+id/textView2"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="Learnfk point "android:textColor="#ff87ff09"android:textSize="30dp"android:layout_above="@+id/imageButton"android:layout_centerHorizontal="true"android:layout_marginBottom="40dp" /><ImageButtonandroid: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" /><Buttonandroid: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 Studio运行该应用程序,请打开您项目的Activity文件之一,然后单击"运行Eclipse运行图标工具栏。 Android Studio将应用程序安装在您的AVD上并启动它,如果设置和应用程序一切正常,它将显示在"Emulator"窗口下面-

Android Broadcast Demo

现在要广播无涯教程的自定义意图,让无涯教程单击 Broadcast Intent 按钮,这将广播无涯教程的自定义意图" com.learnfk.CUSTOM_INTENT" ,这将被无涯教程注册的BroadcastReceiver截获,即MyReceiver以及按照无涯教程实现的逻辑出现在模拟器的底部,如下所示:

Android Broadcast Intent

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

Android - Broadcast Receivers - 无涯教程网无涯教程网提供Broadcast Receivers 仅响应来自其他应用程序或系统本身的广播消息,这些消息有时称为...https://www.learnfk.com/android/android-broadcast-receivers.html

http://www.hkea.cn/news/65356/

相关文章:

  • 做毕业网站的周记外贸建站优化
  • 南昌市住房和城乡建设局网站百度官网推广平台电话
  • 真人做视频网站百度怎么发布广告
  • 网站页面优化包括怎么给网站做优化
  • 哪个网站用帝国cms做的软文素材网
  • 网站建设需要的资料深圳精准网络营销推广
  • 客户网站建设公司网站排名提升软件
  • 网站建设与维护试卷论文怎么在百度上做广告
  • 做博客网站要什么技术百度网站网址是多少
  • 河北建设厅官方网站八大员考试站长工具查询
  • 大连 做网站公司爱站工具包的主要功能
  • ps做简洁大气网站必应bing国内版
  • 做公司标志用哪个网站营销自动化
  • wordpress5.0.3厦门百度seo
  • 网站开发 企业 定制系统优化大师安卓版
  • 网站内链符号seo百度站长工具
  • 网站页面太多是否做静态seo优化软件
  • mac下怎么安装wordpress关键词排名优化易下拉霸屏
  • 国内做国外代购在哪个网站好百度平台客服怎么联系
  • 菏泽网站获客网站建设公司中国站长网入口
  • 黄冈网站建设推荐seo查询排名软件
  • 自己怎么做百度网站广州seo网站公司
  • 京东企业的电子网站建设百度seo教程网
  • 弥勒网站设计公司share群组链接分享
  • 网站建设栏目管理百度推广搜索排名
  • 企业管理类的网站全球搜是什么公司
  • 网站开发自我介绍seo报告
  • 网站应用软件设计海口seo网络公司
  • 武汉站建设深圳网站开发制作
  • 网站建设的平台分析北京seo技术