AndroidManifast.xml

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"
  3. package="com.hanqi.testbr">
  4. <!--接收开机完成的广播权限-->
  5. <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
  6.  
  7. <application
  8. android:allowBackup="true"
  9. android:icon="@mipmap/ic_launcher"
  10. android:label="@string/app_name"
  11. android:supportsRtl="true"
  12. android:theme="@style/AppTheme">
  13. <activity android:name=".MainActivity">
  14. <intent-filter>
  15. <action android:name="android.intent.action.MAIN" />
  16.  
  17. <category android:name="android.intent.category.LAUNCHER" />
  18. </intent-filter>
  19. </activity>
  20.  
  21. <receiver
  22. android:name=".MyReceiver"
  23. android:enabled="true"
  24. android:exported="true">
  25. <intent-filter android:priority="20">
  26. <action android:name="com.hanqi.textbr.action" />
  27. </intent-filter>
  28. </receiver>
  29.  
  30. <service android:name=".BootService"/>
  31. <receiver
  32. android:name=".BootReceiver"
  33. android:enabled="true"
  34. android:exported="true">
  35. <intent-filter>
  36. <action android:name="android.intent.action.BOOT_COMPLETED"/>
  37. </intent-filter>
  38. </receiver>
  39. </application>
  40.  
  41. </manifest>

activity_main.xml

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout 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:paddingBottom="@dimen/activity_vertical_margin"
  7. android:paddingLeft="@dimen/activity_horizontal_margin"
  8. android:paddingRight="@dimen/activity_horizontal_margin"
  9. android:paddingTop="@dimen/activity_vertical_margin"
  10. tools:context="com.hanqi.testbr.MainActivity"
  11. android:orientation="vertical">
  12.  
  13. <Button
  14. android:layout_width="match_parent"
  15. android:layout_height="wrap_content"
  16. android:text="发送一般广播"
  17. android:onClick="bt1_onclick"/>
  18. <Button
  19. android:layout_width="match_parent"
  20. android:layout_height="wrap_content"
  21. android:text="发送有序广播"
  22. android:onClick="bt2_onclick"/>
  23. <Button
  24. android:layout_width="match_parent"
  25. android:layout_height="wrap_content"
  26. android:text="动态注册广播接收器"
  27. android:onClick="bt3_onclick"/>
  28. <Button
  29. android:layout_width="match_parent"
  30. android:layout_height="wrap_content"
  31. android:text="解注册广播接收器"
  32. android:onClick="bt4_onclick"/>
  33.  
  34. </LinearLayout>

MainActivity.java

  1. package com.hanqi.testbr;
  2.  
  3. import android.content.Intent;
  4. import android.content.IntentFilter;
  5. import android.support.v7.app.AppCompatActivity;
  6. import android.os.Bundle;
  7. import android.view.View;
  8. import android.widget.Toast;
  9.  
  10. public class MainActivity extends AppCompatActivity {
  11.  
  12. @Override
  13. protected void onCreate(Bundle savedInstanceState) {
  14. super.onCreate(savedInstanceState);
  15. setContentView(R.layout.activity_main);
  16. }
  17. public void bt1_click(View v)
  18. {
  19. //发送一般广播
  20. //1.准备意图Intent
  21. Intent intent = new Intent("com.hanqi.textbr.action");
  22.  
  23. intent.putExtra("data","广播发出了");
  24. //2.发送
  25. sendBroadcast(intent);
  26.  
  27. Toast.makeText(MainActivity.this, "我发送了广播", Toast.LENGTH_SHORT).show();
  28. }
  29. public void bt2_click(View v)
  30. {
  31. //发送有序广播
  32. //1.准备意图Intent
  33. Intent intent = new Intent("com.hanqi.textbr.action");
  34.  
  35. intent.putExtra("data", "有序广播发出了");
  36. //2.发送
  37. sendOrderedBroadcast(intent, null);
  38.  
  39. Toast.makeText(MainActivity.this, "我发送了有序广播", Toast.LENGTH_SHORT).show();
  40. }
  41. MyReceiver2 myReceiver2;
  42. public void bt3_onclick(View v)
  43. {
  44. if (myReceiver2 == null) {
  45. //动态注册
  46. //1.实例化接收器
  47. myReceiver2 = new MyReceiver2();
  48. //2.实例化IntentFilter
  49. IntentFilter intentFilter = new IntentFilter("com.hanqi.textbr.action");
  50.  
  51. intentFilter.setPriority(1000);
  52.  
  53. //3.注册
  54.  
  55. registerReceiver(myReceiver2, intentFilter);
  56. }
  57. }
  58. public void bt4_onclick(View v)
  59. {
  60. //解注册
  61. if (myReceiver2 != null) {
  62. unregisterReceiver(myReceiver2);
  63. myReceiver2 = null;
  64.  
  65. Toast.makeText(MainActivity.this, "解注册接收器", Toast.LENGTH_SHORT).show();
  66. }
  67. }
  68.  
  69. @Override
  70. protected void onDestroy() {
  71. super.onDestroy();
  72. if (myReceiver2 != null) {
  73. unregisterReceiver(myReceiver2);
  74.  
  75. myReceiver2 =null;
  76. //Toast.makeText(MainActivity.this, "解注册接收器", Toast.LENGTH_SHORT).show();
  77. }
  78. }
  79. }

MyReceiver.java

  1. package com.hanqi.testbr;
  2.  
  3. import android.content.BroadcastReceiver;
  4. import android.content.Context;
  5. import android.content.Intent;
  6. import android.util.Log;
  7. import android.widget.Toast;
  8.  
  9. public class MyReceiver extends BroadcastReceiver {
  10. public MyReceiver() {
  11. Log.e("ATG","构造广播接收器");
  12. }
  13.  
  14. @Override
  15. public void onReceive(Context context, Intent intent) {
  16. String str = intent.getStringExtra("data");
  17.  
  18. // 处理广播
  19. Log.e("ATG","收到广播了 = "+str);
  20.  
  21. Toast.makeText(context, "收到广播了 = "+str, Toast.LENGTH_SHORT).show();
  22. //是否是有序广播
  23. if (isOrderedBroadcast())
  24. {
  25. abortBroadcast();
  26. Log.e("ATG", "我阻断了有序广播");
  27. Toast.makeText(context, "我阻断了有序广播", Toast.LENGTH_SHORT).show();
  28. }
  29.  
  30. }
  31. }

MyReceiver2.java

  1.  
  1. package com.hanqi.testbr;
  1.  
  2. import android.content.BroadcastReceiver;
  3. import android.content.Context;
  4. import android.content.Intent;
  5. import android.util.Log;
  6.  
  7. public class MyReceiver2 extends BroadcastReceiver {
  8. public MyReceiver2() {
  9. Log.e("ATG","构造广播接收器2");
  10. }
  11.  
  12. @Override
  13. public void onReceive(Context context, Intent intent) {
  14. String str = intent.getStringExtra("data");
  15.  
  16. // 处理广播
  17. Log.e("ATG","收到广播了2 = "+str);
  18.  
  19. }
  20. }

android BroadcastReceiver的更多相关文章

  1. android BroadcastReceiver ACTION_TIME_TICK 系统时间监听不到

    android BroadcastReceiver ACTION_TIME_TICK 系统时间监听不到 今天做android上的消息推送,启动了一个独立service,然后在里面监听系统的ACTION ...

  2. Android BroadcastReceiver 简介

    Android BroadcastReceiver 简介  在 Android 中使用 Activity, Service, Broadcast, BroadcastReceiver     活动(A ...

  3. 4、android BroadcastReceiver详细用法

    BroadcastReceiver也就是“广播接收者”的意思,顾名思义,它就是用来接收来自系统和应用中的广播. 在Android系统中,广播体现在方方面面,例如当开机完成后系统会产生一条广播,接收到这 ...

  4. Android BroadcastReceiver广播接受者

    静态注册 配置清单表注册:只要曾经注册过哪怕关闭也能调用  方式一:sendBroadCastReceive   广播的步骤:       发送  无序广播,普通广播       (1).发送方    ...

  5. Android BroadcastReceiver实时监听电量

    Android系统中实时的监听手机电量以及开机启动功能都是通过BroadcastReceiver组件实现的.我们可以动态注册这个类的一个实例通过 Context.registerReceiver()方 ...

  6. Android BroadcastReceiver实例Demo(有序广播的发送)

    上一篇简介了广播的发送,这篇主要介绍下,有序广播的发送. 设置完相关属性的时候,广播就会依照有序的方式进行发送: 发送顺序: 先发送第二条广播: 再发送第一条广播: 最后发送第三条广播. 代码例如以下 ...

  7. Android BroadcastReceiver 接收收到短信的广播

    一.知识介绍 1.broadcastReceiver是广播接受者,四大组件之一. 2.Android中内置了很多系统级别的广播,可以在应用程序中得到各种系统的状态信息. 3.使用场景: ①当手机没有电 ...

  8. android BroadcastReceiver组件简单的使用

    1.清单文件 <?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android=& ...

  9. Android BroadcastReceiver解析

    目录   示意图 1. 定义 即 广播,是一个全局的监听器,属于Android四大组件之一 Android 广播分为两个角色:广播发送者.广播接收者 2. 作用 监听 / 接收 应用 App 发出的广 ...

随机推荐

  1. TOMCAT如何建立两个端口或服务

    近日,一个客户需要将系统放到公网上,局网测试的时候用的8080,但该端口已经被其它应用占用,但又不想更改之前的端口,于是查了下资料,以供后阅 针对客户的这个情况,只是说想增加一个端口,这时只需要去to ...

  2. php返回json,xml,JSONP等格式的数据

    php返回json,xml,JSONP等格式的数据 返回json数据: header('Content-Type:application/json; charset=utf-8'); $arr = a ...

  3. ThinkPad E440 加内存后导致开不了机

    上周五新买的ThinkPad E440,原装内存是4G DDR3 1600Hz,明显不够用,于是在京东上买了一根南亚易胜的4G DDR3 1600Hz.安装之后正常开机,明显感觉速度快了很多.可是用了 ...

  4. 1. Longest Palindromic Substring ( 最长回文子串 )

    要求: Given a string S, find the longest palindromic substring in S. (从字符串 S 中最长回文子字符串.) 何为回文字符串? A pa ...

  5. 多个字段用and和or时要注意用括号。

    多个字段用and和or时要注意用括号. 新技能get! create table wly_test (name1 varchar2(10),number1 number(6),score1 numbe ...

  6. 剑指Offer:面试题24——二叉搜索树的后序遍历序列(java实现)

    问题描述: 输入一个整数数组,判断该数组是不是某二叉搜索树的后序遍历的结果.如果是则返回true,否则返回false.假设输入的数组的任意两个数字都互不相同. 思路: 1.首先后序遍历的结果是[(左子 ...

  7. QueryRunner(common-dbutils.jar)

    QueryRunner update方法:* int update(String sql, Object... params) --> 可执行增.删.改语句* int update(Connec ...

  8. IT在线笔试总结(一)

    综述:IT公司的技术类笔试,主要考察: (1)知识面的广度.主要考察一些业内通用性的知识,以及某一职务所必须具备的业务知识. (2)智力测试.主要考察记忆力,思维能力和学习新知识的能力. (3)技能测 ...

  9. Ubuntu14.04通过pyenv配置多python

    参考链接: https://github.com/yyuu/pyenv-virtualenv https://github.com/yyuu/pyenv http://seisman.info/pyt ...

  10. 30、shiro框架入门2,关于Realm

    1.Jdbc的Realm链接,并且获取权限 首先创建shiro-jdbc.ini的配置文件,主要配置链接数据库的信息 配置文件中的内容如下所示 1.变量名=全限定类名会自动创建一个类实例 2.变量名. ...