android BroadcastReceiver
AndroidManifast.xml
- <?xml version="1.0" encoding="utf-8"?>
- <manifest xmlns:android="http://schemas.android.com/apk/res/android"
- package="com.hanqi.testbr">
- <!--接收开机完成的广播权限-->
- <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
- <application
- android: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"
- android:enabled="true"
- android:exported="true">
- <intent-filter android:priority="20">
- <action android:name="com.hanqi.textbr.action" />
- </intent-filter>
- </receiver>
- <service android:name=".BootService"/>
- <receiver
- android:name=".BootReceiver"
- android:enabled="true"
- android:exported="true">
- <intent-filter>
- <action android:name="android.intent.action.BOOT_COMPLETED"/>
- </intent-filter>
- </receiver>
- </application>
- </manifest>
activity_main.xml
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout 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:paddingBottom="@dimen/activity_vertical_margin"
- android:paddingLeft="@dimen/activity_horizontal_margin"
- android:paddingRight="@dimen/activity_horizontal_margin"
- android:paddingTop="@dimen/activity_vertical_margin"
- tools:context="com.hanqi.testbr.MainActivity"
- android:orientation="vertical">
- <Button
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:text="发送一般广播"
- android:onClick="bt1_onclick"/>
- <Button
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:text="发送有序广播"
- android:onClick="bt2_onclick"/>
- <Button
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:text="动态注册广播接收器"
- android:onClick="bt3_onclick"/>
- <Button
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:text="解注册广播接收器"
- android:onClick="bt4_onclick"/>
- </LinearLayout>
MainActivity.java
- package com.hanqi.testbr;
- import android.content.Intent;
- import android.content.IntentFilter;
- import android.support.v7.app.AppCompatActivity;
- import android.os.Bundle;
- import android.view.View;
- import android.widget.Toast;
- public class MainActivity extends AppCompatActivity {
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- }
- public void bt1_click(View v)
- {
- //发送一般广播
- //1.准备意图Intent
- Intent intent = new Intent("com.hanqi.textbr.action");
- intent.putExtra("data","广播发出了");
- //2.发送
- sendBroadcast(intent);
- Toast.makeText(MainActivity.this, "我发送了广播", Toast.LENGTH_SHORT).show();
- }
- public void bt2_click(View v)
- {
- //发送有序广播
- //1.准备意图Intent
- Intent intent = new Intent("com.hanqi.textbr.action");
- intent.putExtra("data", "有序广播发出了");
- //2.发送
- sendOrderedBroadcast(intent, null);
- Toast.makeText(MainActivity.this, "我发送了有序广播", Toast.LENGTH_SHORT).show();
- }
- MyReceiver2 myReceiver2;
- public void bt3_onclick(View v)
- {
- if (myReceiver2 == null) {
- //动态注册
- //1.实例化接收器
- myReceiver2 = new MyReceiver2();
- //2.实例化IntentFilter
- IntentFilter intentFilter = new IntentFilter("com.hanqi.textbr.action");
- intentFilter.setPriority(1000);
- //3.注册
- registerReceiver(myReceiver2, intentFilter);
- }
- }
- public void bt4_onclick(View v)
- {
- //解注册
- if (myReceiver2 != null) {
- unregisterReceiver(myReceiver2);
- myReceiver2 = null;
- Toast.makeText(MainActivity.this, "解注册接收器", Toast.LENGTH_SHORT).show();
- }
- }
- @Override
- protected void onDestroy() {
- super.onDestroy();
- if (myReceiver2 != null) {
- unregisterReceiver(myReceiver2);
- myReceiver2 =null;
- //Toast.makeText(MainActivity.this, "解注册接收器", Toast.LENGTH_SHORT).show();
- }
- }
- }
MyReceiver.java
- package com.hanqi.testbr;
- import android.content.BroadcastReceiver;
- import android.content.Context;
- import android.content.Intent;
- import android.util.Log;
- import android.widget.Toast;
- public class MyReceiver extends BroadcastReceiver {
- public MyReceiver() {
- Log.e("ATG","构造广播接收器");
- }
- @Override
- public void onReceive(Context context, Intent intent) {
- String str = intent.getStringExtra("data");
- // 处理广播
- Log.e("ATG","收到广播了 = "+str);
- Toast.makeText(context, "收到广播了 = "+str, Toast.LENGTH_SHORT).show();
- //是否是有序广播
- if (isOrderedBroadcast())
- {
- abortBroadcast();
- Log.e("ATG", "我阻断了有序广播");
- Toast.makeText(context, "我阻断了有序广播", Toast.LENGTH_SHORT).show();
- }
- }
- }
MyReceiver2.java
- package com.hanqi.testbr;
- import android.content.BroadcastReceiver;
- import android.content.Context;
- import android.content.Intent;
- import android.util.Log;
- public class MyReceiver2 extends BroadcastReceiver {
- public MyReceiver2() {
- Log.e("ATG","构造广播接收器2");
- }
- @Override
- public void onReceive(Context context, Intent intent) {
- String str = intent.getStringExtra("data");
- // 处理广播
- Log.e("ATG","收到广播了2 = "+str);
- }
- }
android BroadcastReceiver的更多相关文章
- android BroadcastReceiver ACTION_TIME_TICK 系统时间监听不到
android BroadcastReceiver ACTION_TIME_TICK 系统时间监听不到 今天做android上的消息推送,启动了一个独立service,然后在里面监听系统的ACTION ...
- Android BroadcastReceiver 简介
Android BroadcastReceiver 简介 在 Android 中使用 Activity, Service, Broadcast, BroadcastReceiver 活动(A ...
- 4、android BroadcastReceiver详细用法
BroadcastReceiver也就是“广播接收者”的意思,顾名思义,它就是用来接收来自系统和应用中的广播. 在Android系统中,广播体现在方方面面,例如当开机完成后系统会产生一条广播,接收到这 ...
- Android BroadcastReceiver广播接受者
静态注册 配置清单表注册:只要曾经注册过哪怕关闭也能调用 方式一:sendBroadCastReceive 广播的步骤: 发送 无序广播,普通广播 (1).发送方 ...
- Android BroadcastReceiver实时监听电量
Android系统中实时的监听手机电量以及开机启动功能都是通过BroadcastReceiver组件实现的.我们可以动态注册这个类的一个实例通过 Context.registerReceiver()方 ...
- Android BroadcastReceiver实例Demo(有序广播的发送)
上一篇简介了广播的发送,这篇主要介绍下,有序广播的发送. 设置完相关属性的时候,广播就会依照有序的方式进行发送: 发送顺序: 先发送第二条广播: 再发送第一条广播: 最后发送第三条广播. 代码例如以下 ...
- Android BroadcastReceiver 接收收到短信的广播
一.知识介绍 1.broadcastReceiver是广播接受者,四大组件之一. 2.Android中内置了很多系统级别的广播,可以在应用程序中得到各种系统的状态信息. 3.使用场景: ①当手机没有电 ...
- android BroadcastReceiver组件简单的使用
1.清单文件 <?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android=& ...
- Android BroadcastReceiver解析
目录 示意图 1. 定义 即 广播,是一个全局的监听器,属于Android四大组件之一 Android 广播分为两个角色:广播发送者.广播接收者 2. 作用 监听 / 接收 应用 App 发出的广 ...
随机推荐
- TOMCAT如何建立两个端口或服务
近日,一个客户需要将系统放到公网上,局网测试的时候用的8080,但该端口已经被其它应用占用,但又不想更改之前的端口,于是查了下资料,以供后阅 针对客户的这个情况,只是说想增加一个端口,这时只需要去to ...
- php返回json,xml,JSONP等格式的数据
php返回json,xml,JSONP等格式的数据 返回json数据: header('Content-Type:application/json; charset=utf-8'); $arr = a ...
- ThinkPad E440 加内存后导致开不了机
上周五新买的ThinkPad E440,原装内存是4G DDR3 1600Hz,明显不够用,于是在京东上买了一根南亚易胜的4G DDR3 1600Hz.安装之后正常开机,明显感觉速度快了很多.可是用了 ...
- 1. Longest Palindromic Substring ( 最长回文子串 )
要求: Given a string S, find the longest palindromic substring in S. (从字符串 S 中最长回文子字符串.) 何为回文字符串? A pa ...
- 多个字段用and和or时要注意用括号。
多个字段用and和or时要注意用括号. 新技能get! create table wly_test (name1 varchar2(10),number1 number(6),score1 numbe ...
- 剑指Offer:面试题24——二叉搜索树的后序遍历序列(java实现)
问题描述: 输入一个整数数组,判断该数组是不是某二叉搜索树的后序遍历的结果.如果是则返回true,否则返回false.假设输入的数组的任意两个数字都互不相同. 思路: 1.首先后序遍历的结果是[(左子 ...
- QueryRunner(common-dbutils.jar)
QueryRunner update方法:* int update(String sql, Object... params) --> 可执行增.删.改语句* int update(Connec ...
- IT在线笔试总结(一)
综述:IT公司的技术类笔试,主要考察: (1)知识面的广度.主要考察一些业内通用性的知识,以及某一职务所必须具备的业务知识. (2)智力测试.主要考察记忆力,思维能力和学习新知识的能力. (3)技能测 ...
- Ubuntu14.04通过pyenv配置多python
参考链接: https://github.com/yyuu/pyenv-virtualenv https://github.com/yyuu/pyenv http://seisman.info/pyt ...
- 30、shiro框架入门2,关于Realm
1.Jdbc的Realm链接,并且获取权限 首先创建shiro-jdbc.ini的配置文件,主要配置链接数据库的信息 配置文件中的内容如下所示 1.变量名=全限定类名会自动创建一个类实例 2.变量名. ...