一.利用BroadcastReceiever监听短信

AndroidManifest.xml

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"
  3. package="com.example.boradcast">
  4. <uses-permission android:name="android.permission.RECEIVE_SMS"/>
  5.  
  6. <application
  7. android:allowBackup="true"
  8. android:icon="@mipmap/ic_launcher"
  9. android:label="@string/app_name"
  10. android:supportsRtl="true"
  11. android:theme="@style/AppTheme">
  12. <activity android:name=".MainActivity"
  13. android:screenOrientation="landscape"
  14. android:theme="@android:style/Theme.NoTitleBar.Fullscreen">
  15. <intent-filter>
  16. <action android:name="android.intent.action.MAIN" />
  17.  
  18. <category android:name="android.intent.category.LAUNCHER" />
  19. </intent-filter>
  20. </activity>
  21. <receiver android:name=".MyBoradcast">
  22. <intent-filter>
  23. <action android:name ="android.provider.Telephony.SMS_RECEIVED"></action>
  24. </intent-filter>
  25. </receiver>
  26. </application>
  27.  
  28. </manifest>

activity_main.xml

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <RelativeLayout 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.example.boradcast.MainActivity">
  11.  
  12. <TextView
  13. android:layout_width="wrap_content"
  14. android:layout_height="wrap_content"
  15. android:text="Hello World!" />
  16. </RelativeLayout>
  1. MainActivity:
  1. package com.example.boradcast;
  2.  
  3. import android.app.Activity;
  4. import android.os.Bundle;
  5.  
  6. public class MainActivity extends Activity {
  7.  
  8. @Override
  9. protected void onCreate(Bundle savedInstanceState) {
  10. super.onCreate(savedInstanceState);
  11. setContentView(R.layout.activity_main);
  12. }
  13. }
  1.  
  1.  
  1. MyBoradcast:
  1. package com.example.boradcast;
  2.  
  3. import android.content.BroadcastReceiver;
  4. import android.content.Context;
  5. import android.content.Intent;
  6. import android.widget.Toast;
  7.  
  8. /**
  9. * Created by 杨雄超 on 2016/8/17.
  10. */
  11. public class MyBoradcast extends BroadcastReceiver {
  12. @Override
  13. public void onReceive(Context context, Intent intent) {
  14. Toast.makeText(context,"你有新信息",Toast.LENGTH_SHORT).show();
  15. }
  16. }
  1.  
  1. 总结:android:theme="@android:style/Theme.NoTitleBar.Fullscreen"> 去除状态栏和标题栏
  1. <action android:name ="android.provider.Telephony.SMS_RECEIVED"></action> 广播接受者关注的是什么事件
  1. android:screenOrientation="landscape" 横屏
  1.  
    二.自定义广播的发送和接收
    发送:
    activity_main.xml:
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <RelativeLayout 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. tools:context="com.example.broadcastsender.MainActivity">
  7.  
  8. <Button
  9. android:layout_width="wrap_content"
  10. android:layout_height="wrap_content"
  11. android:id="@+id/btn_StartBroadcast"
  12. android:text="发送广播"/>
  13. </RelativeLayout>
  1. MainActivity:
  1.  
  1. package com.example.broadcastsender;
  2.  
  3. import android.content.Intent;
  4. import android.support.v7.app.AppCompatActivity;
  5. import android.os.Bundle;
  6. import android.view.View;
  7. import android.widget.Button;
  8.  
  9. public class MainActivity extends AppCompatActivity implements View.OnClickListener {
  10. private Button btnStartBroad;
  11.  
  12. @Override
  13. protected void onCreate(Bundle savedInstanceState) {
  14. super.onCreate(savedInstanceState);
  15. setContentView(R.layout.activity_main);
  16.  
  17. btnStartBroad=(Button) findViewById(R.id.btn_StartBroadcast);
  18.  
  19. btnStartBroad.setOnClickListener(this);
  20. }
  21.  
  22. @Override
  23. public void onClick(View view) {
  24. switch (view.getId()){
  25. case R.id.btn_StartBroadcast:
  26. Intent intent=new Intent();
  27. //设置action,便于接收方使用
  28. intent.setAction("com.xch");
  29. intent.putExtra("key","发送广播了!");
  30. //发送广播
  31. sendBroadcast(intent);
  32. break;
  33. }
  34. }
  35. }
  1.  

接收:

AndroidManifest.xml

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"
  3. package="com.example.broadcastreceive">
  4.  
  5. <application
  6. android:allowBackup="true"
  7. android:icon="@mipmap/ic_launcher"
  8. android:label="@string/app_name"
  9. android:supportsRtl="true"
  10. android:theme="@style/AppTheme">
  11. <activity android:name=".MainActivity">
  12. <intent-filter>
  13. <action android:name="android.intent.action.MAIN" />
  14.  
  15. <category android:name="android.intent.category.LAUNCHER" />
  16. </intent-filter>
  17. </activity>
  18. <receiver android:name=".MyBroadcast">
  19. <intent-filter>
  20. <!--此处action为发送方设置的action-->
  21. <action android:name="com.xch"/>
  22. </intent-filter>
  23. </receiver>
  24. </application>
  25.  
  26. </manifest>
  1. MyBroadcast
  1. package com.example.broadcastreceive;
  2.  
  3. import android.content.BroadcastReceiver;
  4. import android.content.Context;
  5. import android.content.Intent;
  6. import android.widget.Toast;
  7.  
  8. /**
  9. * Created by 杨雄超 on 2016/8/31.
  10. */
  11. public class MyBroadcast extends BroadcastReceiver{
  12. @Override
  13. public void onReceive(Context context, Intent intent) {
  14. String str=intent.getExtras().getString("key");
  15. Toast.makeText(context,str,Toast.LENGTH_SHORT).show();
  16. }
  17. }

总结:自定义广播发送和接收可以利用Intent,需要注意的是要设置一个action,便于接收:如下

intent.setAction("com.xch");

<action android:name="com.xch"/>

结果:

三: 广播接收者的优先级别

  1.  1.AndroidManifest.xml中设置优先级别,具体为在intent-filter标签中设置android:priority="1000";这里设置1000,如果我们要让这个接受者为最高级别,则其他接收者级别应该设置得比这个低。
  1. 2.在代码中发送方应该发送有序的广播,之前的sendBroadcast(intent);是无序的,这里我们应该用:sendOrderedBroadcast(intent,null);
  1. 3.在接收方拦截掉比当前优先级别低的广播:abortBroadcast();
  2. 具体代码如下:
    发送广播:
  1. package com.example.broadcastsender;
  2.  
  3. import android.content.Intent;
  4. import android.support.v7.app.AppCompatActivity;
  5. import android.os.Bundle;
  6. import android.view.View;
  7. import android.widget.Button;
  8.  
  9. public class MainActivity extends AppCompatActivity implements View.OnClickListener {
  10. private Button btnStartBroad;
  11.  
  12. @Override
  13. protected void onCreate(Bundle savedInstanceState) {
  14. super.onCreate(savedInstanceState);
  15. setContentView(R.layout.activity_main);
  16.  
  17. btnStartBroad=(Button) findViewById(R.id.btn_StartBroadcast);
  18.  
  19. btnStartBroad.setOnClickListener(this);
  20. }
  21.  
  22. @Override
  23. public void onClick(View view) {
  24. switch (view.getId()){
  25. case R.id.btn_StartBroadcast:
  26. Intent intent=new Intent();
  27. //设置action,便于接收方使用
  28. intent.setAction("com.xch");
  29. intent.putExtra("key","发送广播了!");
  30. //发送无序的广播
  31. //sendBroadcast(intent);
  32. //发送有序的广播
  33. sendOrderedBroadcast(intent,null);
  34. break;
  35. }
  36. }
  37. }
  1.  

接收广播:

AndroidManifest.xml

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"
  3. package="com.example.broadcastreceive">
  4.  
  5. <application
  6. android:allowBackup="true"
  7. android:icon="@mipmap/ic_launcher"
  8. android:label="@string/app_name"
  9. android:supportsRtl="true"
  10. android:theme="@style/AppTheme">
  11. <activity android:name=".MainActivity">
  12. <intent-filter>
  13. <action android:name="android.intent.action.MAIN" />
  14.  
  15. <category android:name="android.intent.category.LAUNCHER" />
  16. </intent-filter>
  17. </activity>
  18. <receiver android:name=".MyBroadcast">
  19. <!--设置广播优先级:intent-filter标签下的android:priority=""-->
  20. <intent-filter android:priority="1000">
  21. <action android:name="com.xch"/>
  22. </intent-filter>
  23. </receiver>
  24. </application>
  25.  
  26. </manifest>
  1. MyBroadcast
  1.  
  1. package com.example.broadcastreceive;
  2.  
  3. import android.content.BroadcastReceiver;
  4. import android.content.Context;
  5. import android.content.Intent;
  6. import android.widget.Toast;
  7.  
  8. /**
  9. * Created by 杨雄超 on 2016/8/31.
  10. */
  11. public class MyBroadcast extends BroadcastReceiver{
  12. @Override
  13. public void onReceive(Context context, Intent intent) {
  14. String str=intent.getExtras().getString("key");
  15. Toast.makeText(context,str,Toast.LENGTH_SHORT).show();
  16. //拦截掉比当前优先级别低的广播
  17. abortBroadcast();
  18. }
  19. }
  1.  
  1.  

Android学习总结——Broadcast的更多相关文章

  1. Android学习笔记--Broadcast, BroadcastReceiver(广播)

    参考资料:http://www.cnblogs.com/playing/archive/2011/03/23/1992030.html 在 Android 中使用 Activity, Service, ...

  2. Android学习之Broadcast初体验

    •何为 Broadcast ? Broadcast 直译广播,接下来举个形象的例子来理解下 Broadcast: 上学的时候,每个班级都会有一个挂在墙上的大喇叭,用来广播一些通知,比如,开学要去搬书, ...

  3. 【转】 Pro Android学习笔记(九七):BroadcastReceiver(1):基础小例子

    目录(?)[-] 基础小例子 发送Broadcast intent 运行情况 应用间的广播 文章转载只能用于非商业性质,且不能带有虚拟货币.积分.注册等附加条件.转载须注明出处:http://blog ...

  4. 【Android学习】《Android开发视频教程》第一季笔记

    视频地址: http://study.163.com/course/courseMain.htm?courseId=207001 课时5    Activity基础概念 1.Android开发技术结构 ...

  5. Android学习之路——简易版微信为例(二)

    1 概述 从这篇博文开始,正式进入简易版微信的开发.深入学习前,想谈谈个人对Android程序开发一些理解,不一定正确,只是自己的一点想法.Android程序开发不像我们在大学时候写C控制台程序那样, ...

  6. 我的Android学习之旅(转)

    去年大概在七月份的时候误打误撞接触了一阵子Android,之后由于工作时间比较忙,无暇顾及,九月份的时候自己空闲的时间比较多,公司相对来说加班情况没以前严重.开启了个人的Android学习之旅,初衷是 ...

  7. 《Android学习指南》目录

    源:<Android学习指南>目录 Android学习指南的内容分类: 分类 描述 0.学习Android必备的Java基础知识 没有Java基础的朋友,请不要先看Android的课程,这 ...

  8. Android学习资料总结

    从事ASP.NET Web开发两年了,主要是做Web项目(ASP.NET WebForm和ASP.NET MVC),也做过C/S架构的企业内部系统,偶然接触Android,学艺不精,项目没做出什么,倒 ...

  9. 《Android学习指南》文件夹

    转自:http://android.yaohuiji.com/about Android学习指南的内容分类: 分类 描写叙述 0.学习Android必备的Java基础知识 没有Java基础的朋友,请不 ...

随机推荐

  1. HDU1232 畅通工程 (并查集模板题)

    畅通工程 Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submis ...

  2. Graph.js

    Graph.js Graph.js A JavaScript library for rendering a graph of nodes

  3. error LNK2019

    error LNK2019: 无法解析的外部符号 "public: virtual __thiscall Fruit::~Fruit(void)" (??1Fruit@@UAE@X ...

  4. Unity 编辑器扩展自定义窗体

    这次看见Unity还可以自定义弹出窗体,让我很好奇.于是就去网上找文章看了看. 如果想自定义窗体需要把类放入Editor文件夹下面. 代码如下: using UnityEngine; using Un ...

  5. Unity 鼠标点击左右移动,人物跟随旋转

    上代码: using UnityEngine; using System.Collections; public class Test : MonoBehaviour { private Vector ...

  6. 删除一个表中的重复数据同时保留第一次插入那一条以及sql优化

    业务:一个表中有很多数据(id为自增主键),在这些数据中有个别数据出现了重复的数据. 目标:需要把这些重复数据删除同时保留第一次插入的那一条数据,还要保持其它的数据不受影响. 解题过程: 第一步:查出 ...

  7. 频繁模式挖掘apriori算法介绍及Java实现

    频繁模式是频繁地出如今数据集中的模式(如项集.子序列或者子结构).比如.频繁地同一时候出如今交易数据集中的商品(如牛奶和面包)的集合是频繁项集. 一些基本概念 支持度:support(A=>B) ...

  8. Swift中的设计模式

    设计模式(Design Pattern)是 对软件设计中普遍存在的各种问题,所提出的解决方案.这个术语是由埃里希·伽玛等人(Erich Gamma,Richard Helm,Ralph Johnson ...

  9. ES6的模块化

    在之前的 javascript 中一直是没有模块系统的,前辈们为了解决这些问题,提出了各种规范, 最主要的有CommonJS和AMD两种.前者用于服务器,后者用于浏览器.而 ES6 中提供了简单的模块 ...

  10. tail

    tail用于显示指定文件末尾内容,不指定文件时,作为输入信息进行处理.常用查看日志文件. -f 循环读取 -q 不显示处理信息 -v 显示详细的处理信息 -c<数目> 显示的字节数 -n& ...