Android学习总结——Broadcast
一.利用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的更多相关文章
- Android学习笔记--Broadcast, BroadcastReceiver(广播)
参考资料:http://www.cnblogs.com/playing/archive/2011/03/23/1992030.html 在 Android 中使用 Activity, Service, ...
- Android学习之Broadcast初体验
•何为 Broadcast ? Broadcast 直译广播,接下来举个形象的例子来理解下 Broadcast: 上学的时候,每个班级都会有一个挂在墙上的大喇叭,用来广播一些通知,比如,开学要去搬书, ...
- 【转】 Pro Android学习笔记(九七):BroadcastReceiver(1):基础小例子
目录(?)[-] 基础小例子 发送Broadcast intent 运行情况 应用间的广播 文章转载只能用于非商业性质,且不能带有虚拟货币.积分.注册等附加条件.转载须注明出处:http://blog ...
- 【Android学习】《Android开发视频教程》第一季笔记
视频地址: http://study.163.com/course/courseMain.htm?courseId=207001 课时5 Activity基础概念 1.Android开发技术结构 ...
- Android学习之路——简易版微信为例(二)
1 概述 从这篇博文开始,正式进入简易版微信的开发.深入学习前,想谈谈个人对Android程序开发一些理解,不一定正确,只是自己的一点想法.Android程序开发不像我们在大学时候写C控制台程序那样, ...
- 我的Android学习之旅(转)
去年大概在七月份的时候误打误撞接触了一阵子Android,之后由于工作时间比较忙,无暇顾及,九月份的时候自己空闲的时间比较多,公司相对来说加班情况没以前严重.开启了个人的Android学习之旅,初衷是 ...
- 《Android学习指南》目录
源:<Android学习指南>目录 Android学习指南的内容分类: 分类 描述 0.学习Android必备的Java基础知识 没有Java基础的朋友,请不要先看Android的课程,这 ...
- Android学习资料总结
从事ASP.NET Web开发两年了,主要是做Web项目(ASP.NET WebForm和ASP.NET MVC),也做过C/S架构的企业内部系统,偶然接触Android,学艺不精,项目没做出什么,倒 ...
- 《Android学习指南》文件夹
转自:http://android.yaohuiji.com/about Android学习指南的内容分类: 分类 描写叙述 0.学习Android必备的Java基础知识 没有Java基础的朋友,请不 ...
随机推荐
- HDU1232 畅通工程 (并查集模板题)
畅通工程 Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submis ...
- Graph.js
Graph.js Graph.js A JavaScript library for rendering a graph of nodes
- error LNK2019
error LNK2019: 无法解析的外部符号 "public: virtual __thiscall Fruit::~Fruit(void)" (??1Fruit@@UAE@X ...
- Unity 编辑器扩展自定义窗体
这次看见Unity还可以自定义弹出窗体,让我很好奇.于是就去网上找文章看了看. 如果想自定义窗体需要把类放入Editor文件夹下面. 代码如下: using UnityEngine; using Un ...
- Unity 鼠标点击左右移动,人物跟随旋转
上代码: using UnityEngine; using System.Collections; public class Test : MonoBehaviour { private Vector ...
- 删除一个表中的重复数据同时保留第一次插入那一条以及sql优化
业务:一个表中有很多数据(id为自增主键),在这些数据中有个别数据出现了重复的数据. 目标:需要把这些重复数据删除同时保留第一次插入的那一条数据,还要保持其它的数据不受影响. 解题过程: 第一步:查出 ...
- 频繁模式挖掘apriori算法介绍及Java实现
频繁模式是频繁地出如今数据集中的模式(如项集.子序列或者子结构).比如.频繁地同一时候出如今交易数据集中的商品(如牛奶和面包)的集合是频繁项集. 一些基本概念 支持度:support(A=>B) ...
- Swift中的设计模式
设计模式(Design Pattern)是 对软件设计中普遍存在的各种问题,所提出的解决方案.这个术语是由埃里希·伽玛等人(Erich Gamma,Richard Helm,Ralph Johnson ...
- ES6的模块化
在之前的 javascript 中一直是没有模块系统的,前辈们为了解决这些问题,提出了各种规范, 最主要的有CommonJS和AMD两种.前者用于服务器,后者用于浏览器.而 ES6 中提供了简单的模块 ...
- tail
tail用于显示指定文件末尾内容,不指定文件时,作为输入信息进行处理.常用查看日志文件. -f 循环读取 -q 不显示处理信息 -v 显示详细的处理信息 -c<数目> 显示的字节数 -n& ...