一.利用BroadcastReceiever监听短信

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.boradcast">
<uses-permission android:name="android.permission.RECEIVE_SMS"/> <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"
android:screenOrientation="landscape"
android:theme="@android:style/Theme.NoTitleBar.Fullscreen">
<intent-filter>
<action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver android:name=".MyBoradcast">
<intent-filter>
<action android:name ="android.provider.Telephony.SMS_RECEIVED"></action>
</intent-filter>
</receiver>
</application> </manifest>

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<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: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.example.boradcast.MainActivity"> <TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!" />
</RelativeLayout>
MainActivity:
package com.example.boradcast;

import android.app.Activity;
import android.os.Bundle; public class MainActivity extends Activity { @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}


MyBoradcast:
package com.example.boradcast;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.widget.Toast; /**
* Created by 杨雄超 on 2016/8/17.
*/
public class MyBoradcast extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(context,"你有新信息",Toast.LENGTH_SHORT).show();
}
}

总结:android:theme="@android:style/Theme.NoTitleBar.Fullscreen"> 去除状态栏和标题栏
     <action android:name ="android.provider.Telephony.SMS_RECEIVED"></action> 广播接受者关注的是什么事件
     android:screenOrientation="landscape" 横屏
 
二.自定义广播的发送和接收
发送:
activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<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"
tools:context="com.example.broadcastsender.MainActivity"> <Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/btn_StartBroadcast"
android:text="发送广播"/>
</RelativeLayout>
MainActivity:

package com.example.broadcastsender;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button; public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private Button btnStartBroad; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); btnStartBroad=(Button) findViewById(R.id.btn_StartBroadcast); btnStartBroad.setOnClickListener(this);
} @Override
public void onClick(View view) {
switch (view.getId()){
case R.id.btn_StartBroadcast:
Intent intent=new Intent();
//设置action,便于接收方使用
intent.setAction("com.xch");
intent.putExtra("key","发送广播了!");
//发送广播
sendBroadcast(intent);
break;
}
}
}

接收:

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.broadcastreceive"> <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=".MyBroadcast">
<intent-filter>
<!--此处action为发送方设置的action-->
<action android:name="com.xch"/>
</intent-filter>
</receiver>
</application> </manifest>
MyBroadcast:
package com.example.broadcastreceive;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.widget.Toast; /**
* Created by 杨雄超 on 2016/8/31.
*/
public class MyBroadcast extends BroadcastReceiver{
@Override
public void onReceive(Context context, Intent intent) {
String str=intent.getExtras().getString("key");
Toast.makeText(context,str,Toast.LENGTH_SHORT).show();
}
}

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

intent.setAction("com.xch");

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

结果:

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

 1.在AndroidManifest.xml中设置优先级别,具体为在intent-filter标签中设置android:priority="1000";这里设置1000,如果我们要让这个接受者为最高级别,则其他接收者级别应该设置得比这个低。
 2.在代码中发送方应该发送有序的广播,之前的sendBroadcast(intent);是无序的,这里我们应该用:sendOrderedBroadcast(intent,null);
 3.在接收方拦截掉比当前优先级别低的广播:abortBroadcast();

具体代码如下:
发送广播:
package com.example.broadcastsender;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button; public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private Button btnStartBroad; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); btnStartBroad=(Button) findViewById(R.id.btn_StartBroadcast); btnStartBroad.setOnClickListener(this);
} @Override
public void onClick(View view) {
switch (view.getId()){
case R.id.btn_StartBroadcast:
Intent intent=new Intent();
//设置action,便于接收方使用
intent.setAction("com.xch");
intent.putExtra("key","发送广播了!");
//发送无序的广播
//sendBroadcast(intent);
//发送有序的广播
sendOrderedBroadcast(intent,null);
break;
}
}
}

接收广播:

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.broadcastreceive"> <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=".MyBroadcast">
<!--设置广播优先级:intent-filter标签下的android:priority=""-->
<intent-filter android:priority="1000">
<action android:name="com.xch"/>
</intent-filter>
</receiver>
</application> </manifest>
MyBroadcast:

package com.example.broadcastreceive;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.widget.Toast; /**
* Created by 杨雄超 on 2016/8/31.
*/
public class MyBroadcast extends BroadcastReceiver{
@Override
public void onReceive(Context context, Intent intent) {
String str=intent.getExtras().getString("key");
Toast.makeText(context,str,Toast.LENGTH_SHORT).show();
//拦截掉比当前优先级别低的广播
abortBroadcast();
}
}

 

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. Dynamic Flash Messages

    Dynamic Flash Messages的类似软件 - 其他jQuery插件 - 开源中国社区 Dynamic Flash Messages

  2. MyCat 安装部署,实现数据库分片存储

    一.安装MySQL或MariaDB(本文以MariaDB为例) MySQL手动安装方法:点击查看 MariaDB安装: 1.下载MariaDB的repo $ vi /etc/yum.repos.d/M ...

  3. COJ 1059 - Numeric Parity 位操作

    非常好玩的一道题.能够熟悉下位操作实现和玩一玩bitset这个容器 Description We define the parity of an integer N as the sum of the ...

  4. 我的美国(北美)计算机CS实习面试经验分享

    过去的一年多里,参加了一些面试,虽然面过的公司不多,但都从头一直走到尾.毕竟自己也是花了大量的时间和精力在这一场场的面试里.所以,就絮叨下自己的一些经验,希望能给在美国找实习找工作的同学们提供一点点帮 ...

  5. windows 7 旗舰版 切换 中英文 界面

    http://jingyan.baidu.com/article/f7ff0bfc4963612e26bb131e.html 如果遇到:想下载英语语言包,但是出现代码80070643,windowsu ...

  6. UIView的交换实现,子视图交替变换

    其中加了一些动画  2016-01-13 其中主要的方法有:Demo下载地址,Demo中有介绍:https://github.com/lizhaojie001/UIview.git

  7. IOS中设置状态栏的状态

    IOS上 关于状态栏的相关设置(UIStatusBar) 知识普及 ios上状态栏 就是指的最上面的20像素高的部分 状态栏分前后两部分,要分清这两个概念,后面会用到: 前景部分:就是指的显示电池.时 ...

  8. zoj 1200 Mining

    这道题被划到了动态规划里面去了,结果就是一道模拟题,懒了一点,直接用stl的优先队列,重载了一下运算符,写的时候保证只能一个在采,因为如果需要的采的次数比可以生产的次数少,那么生产的次数等于需要采的次 ...

  9. Oprofile安装与使用探索

    本文分别尝试了oprofile在x86平台和龙芯平台上的安装 一:oprofile的安装与配置(intel+ubuntu12.04) I. Oprofile 安装 Oprofile 包含在 Linux ...

  10. Mysql自定义变量的使用

    用户自定义变量是一个容易被遗忘的MySQL特性,但是如果能用的好,发挥其潜力,在某些场景可以写出非常高效的查询语句.在查询中混合使用过程化和关系化逻辑的时候,自定义变量可能会非常有用.单纯的关系查询将 ...