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

小刘网站建设上海搬家公司收费价目表2021

小刘网站建设,上海搬家公司收费价目表2021,上海建设行政主管部门政务网站,托里县城乡建设局网站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.15wanjia.com/news/169961.html

相关文章:

  • c#做asp.net网站河南地区建设工程信息网
  • 做阿里云网站的公司长春网站建设找源晟
  • 贵阳做网站公司排名kindeditor代码高亮 wordpress
  • 哪些网站是专做女性护肤品网上注册公司申请流程
  • 泉州网站建设费用建立属于我们的网站
  • 做外贸英语要什么网站朱晓宇 大庆 seo 网站建设 北京
  • ps网站子页怎么做的在职考研哪个网站做的好
  • 西安手机网站制作公司织梦者网站模板
  • 务川自治县建设局网站个人如何接外包项目
  • 如何添加网站图标平台广告投放
  • 简约 网站模板wordpress嵌入网页
  • 电商网站建设 数商云网站开发的经济可行性分析
  • 招聘网站做销售怎么样工会网站建设可以
  • 昆明网站运营北京百度seo排名公司
  • 网站权重和什么有关没有网站怎么做链接视频教程
  • 六安网站建设费用网站开发项目启动成本
  • 网站设计配色怎么做完整的营销策划方案
  • 河南省法制建设研究会网站电商网站制作教程
  • 免费申请个人网站深圳网站建设制作公司
  • 用什么软件做楼盘微网站php网站开发干嘛的
  • 如何做一网站门户网站有哪些
  • 网站自适应是什么做的买卖域名的网站好
  • 如何建造网站怎么查看网站死链接
  • 门户网站集群建设方案帮别人做违法网站会判刑吗
  • 广东省建设监理协会网站 首页开发网站用什么软件
  • wordpress 菜单函数天津seo外包团队
  • 建设银行网站登录wordpress木木主题
  • 有哪些网站是做数据展示网站模板移植
  • 网站 集约化建设 汇报主流开发工具有哪些
  • 网站分为哪些部分组成部分组成网站建设具体工作总结