一.利用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. Windows多线程同步系列之四-----信号量

    信号量说实话自己没怎么使用过.书上大概这样说,信号量设置一个资源访问计数.当该计数值大于0的时候,该信号量对象 为有信号状态,当该计数值等于0的时候,该信号量对象为无信号状态. 我们来查几个主要的AP ...

  2. SQL Server 数据库定时自动备份【转】

    在SQL Server中出于数据安全的考虑,所以需要定期的备份数据库.而备份数据库一般又是在凌晨时间基本没有数据库操作的时候进行,所以我们不可能要求管理员每天守到晚上1点去备份数据库.要实现数据库的定 ...

  3. linux虚拟主机管理系统wdcp系列教程之三

    我们安装了网站服务管理系统wdcp之后,在使用过程中可能会出现这样或那样的疑问,下面给大家整理几点出来,方便大家学习.还有不懂的可以到wdlinux论坛寻找相关教程. 1.wdcp后台访问安全设置即限 ...

  4. ibatis错误汇总

    1) 错误:The prefix "context" for element "context:property-placeholder" is not bou ...

  5. 设置grub密码

    一,明文加密的方法 vi /etc/grub.conf 在hiddenmenu下添加password=1234,保存退出. 二,密文加密的方法 2.1, 使用SHA加密方式.grub-crypt  回 ...

  6. stagefright框架(二)- 和OpenMAX的運作

    Stagefright的編解碼功能是利用OpenMAX框架,而且用的還是OpenCORE之OMX的實作,我們來看一下Stagefright和OMX是如何運作的. (1) OMX_Init OMXCli ...

  7. iOS 后台播放音乐

    在info.plist文件中添加 下面是后台播放音频的完整测试代码: 引入文件<AVFoundation/AVFoundation.h> //后台播放音频设置 AVAudioSession ...

  8. XMAL 中x名称控件的Auttribute

    1 X:Class 作用告诉XAML编译器将XAML标签的编译结果与后台代码中指定的类合并,只能用于根节点,并且与之同名的类需要有Partial 例如窗口 2 X:ClassModifier 作用告诉 ...

  9. USACO 1.3... 虫洞 解题报告(搜索+强大剪枝+模拟)

    这题可真是又让我找到了八数码的感觉...哈哈. 首先,第一次见题,没有思路,第二次看题,感觉是搜索,就这样写下来了. 这题我几乎是一个点一个点改对的(至于为什么是这样,后面给你看一个神奇的东西),让我 ...

  10. CAS实现单点登录流程

    CAS实现单点登录 环境 客户端: www.app1.com CAS服务器: www.cas-server.com 1.浏览器:发起请求 www.app1.com 2. 客户端:Authenticat ...