android BroadCastRecevier笔记
学习android的Broadcast,笔记记录于此。
BroadCastRecevier用于接受其他应用发送的广播.
BroadCastReceiver工作,需要2步。
创建Broadcast Recevier.
注册Braodcast Recevier.
参考链接
https://www.tutorialspoint.com/android/android_broadcast_receivers.htm
http://blog.csdn.net/mr_dsw/article/details/51394350
创建BroadcastReceiver
Android stuido创建project, 名称broadcastTest。
继承BroadcastReceiver,用于接收广播。
MyReceiver.java
package com.example.tony.broadcasttest;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.widget.Toast;
public class MyReceiver extends BroadcastReceiver {
// 接受到广播调用
@Override
public void onReceive(Context context, Intent intent) {
// TODO: This method is called when the BroadcastReceiver is receiving
// an Intent broadcast.
// throw new UnsupportedOperationException("Not yet implemented");
Log.i("receive", "onReceive");
Toast.makeText(context, "intent Detected", Toast.LENGTH_LONG).show();
}
}
注册receiver
使用标签注册broadcastReceiver。
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.tony.broadcasttest">
<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,并定义action
action name自己定义,但是发送广播的时候也要发送相同的广播,Receiver才能接收到 -->
<receiver
android:name=".MyReceiver"
android:enabled="true"
android:exported="true">
<intent-filter>
<action android:name="com.example.CUSTOM_INTENT" />
</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.tony.broadcasttest.MainActivity">
<Button
android:id="@+id/send_broadcast"
android:text="send broadcast"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</RelativeLayout>
MainActivity
按键按下,通过sendBroadcast()发送自己定义的广播。
MainActivity.java
package com.example.tony.broadcasttest;
import android.content.Intent;
import android.content.IntentFilter;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
public class MainActivity extends AppCompatActivity {
Button sendBroadcast;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
sendBroadcast = (Button) findViewById(R.id.send_broadcast);
// 按键按下,发送广播
sendBroadcast.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
broadcastIntent(v);
}
});
// 手动注册广播,在AndroidManifest.xml中注册了就不需要手动注册
/*
IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction("com.example.CUSTOM_INTENT");
registerReceiver(new MyReceiver(), intentFilter);
*/
}
public void broadcastIntent(View view) {
Intent intent = new Intent();
// 发送自己广播,名称自己定义,只要在AndroidManifest.xmL中注册,让Receive接受即可。
intent.setAction("com.example.CUSTOM_INTENT");
sendBroadcast(intent);
Log.i("sendBraodcast", "braodcastintent");
}
}
这个demo只是无序广播的demo。对于有序广播,后面再分析。
Tony Liu
2017-3-3, Shenzhen
android BroadCastRecevier笔记的更多相关文章
- android 应用笔记
android 应用笔记 android 应用笔记 小书匠 Android 综合教程 Android常用技巧 安卓系统架构 安卓源码开发 安卓驱动 Linux内核 安卓应用开发 Java 教程 tic ...
- Android 学习笔记之Volley(七)实现Json数据加载和解析...
学习内容: 1.使用Volley实现异步加载Json数据... Volley的第二大请求就是通过发送请求异步实现Json数据信息的加载,加载Json数据有两种方式,一种是通过获取Json对象,然后 ...
- Android开发笔记:打包数据库
对于数据比较多的控制一般会加入SQLite数据库进行数据存储,在打包时这些数据库是不自动打包到apk中的,如何创建数据库呢 方法1:将创建数据库的sql语句在SQLiteHelper继承类中实现,在第 ...
- Android学习笔记进阶之在图片上涂鸦(能清屏)
Android学习笔记进阶之在图片上涂鸦(能清屏) 2013-11-19 10:52 117人阅读 评论(0) 收藏 举报 HandWritingActivity.java package xiaos ...
- android学习笔记36——使用原始XML文件
XML文件 android中使用XML文件,需要开发者手动创建res/xml文件夹. 实例如下: book.xml==> <?xml version="1.0" enc ...
- Android学习笔记之JSON数据解析
转载:Android学习笔记44:JSON数据解析 JSON(JavaScript Object Notation)是一种轻量级的数据交换格式,采用完全独立于语言的文本格式,为Web应用开发提供了一种 ...
- Android开发笔记--hello world 和目录结构
原文:Android开发笔记--hello world 和目录结构 每接触一个新东西 都有一个hello world的例子. 1.新建项目 2.配置AVD AVD 没有要新建个,如果不能创建 运行SD ...
- udacity android 学习笔记: lesson 4 part b
udacity android 学习笔记: lesson 4 part b 作者:干货店打杂的 /titer1 /Archimedes 出处:https://code.csdn.net/titer1 ...
- 【转】Android开发笔记(序)写在前面的目录
原文:http://blog.csdn.net/aqi00/article/details/50012511 知识点分类 一方面写写自己走过的弯路掉进去的坑,避免以后再犯:另一方面希望通过分享自己的经 ...
随机推荐
- C#--类成员
数据成员: 字段: 常量: 函数成员(执行代码): 方法: 属性 构造函数 析构函数 运算符 索引 事件 可以被声明为Static的类成员是:
- EC20 minipcie版4g模块开发笔记
插在电脑上实验时若出现 AT+CREG? +CREG: 0,2 可能是usb口供电不足所致,换至主机箱后面usb口后问题解决,返回值+CREG: 0,1
- 限制 nuget 更新包的版本号
今天在搜索其它问题的时候,突然发现一个使用 nuget 的小技巧. 因为浏览器兼容性的问题,很多网站项目引用的 jQuery 组件版本需要保持在 2.0 以下,因为 2.0 以上需要现代浏览器的支持, ...
- 使用R语言分析股价波动
今天看的R语言.做个笔记. 使用R语言读取雅虎財经数据.分析微软公司(股票代码:MSFT)在2015年股价波动超过百分之十的日期. 然后通过检索新闻的方式,看看微软当天有什么新闻发生,导致股价波动. ...
- Java 并发
参考:http://www.cnblogs.com/dolphin0520/category/602384.html
- openvpn中tun和tap的区别
openvpn有dev tun和dev tap模式的区别,cookbook的解释是:A TUN device is used mostly for VPN tunnels where only IP- ...
- [na]双绞线线序+POE供电网线
0 重点-8根线的细节 传输数据线: 一般情况下会用1236(橙白.橙.绿白.绿)传输数据,1.2用于发送,3.6用于接收, 供电线: 45(蓝.蓝白)电源正极 78(棕白.棕)电源负极. 一 网线线 ...
- ajaxfileupload异步上传附件添加參数的方法
1.js文件 // JavaScript Document jQuery.extend({ createUploadIframe: function(id, uri) { //create frame ...
- 简单了解一下c编译过程
大一的时候,学习c语言,用的是VC6.0.用了1年多,到后来了解了Linux,知道了gcc编译器,开始使用gcc Hello.c -o a.out 这样的命令进行编译.后来又学了gcc的一些其他的命令 ...
- Ubuntu下如何解压缩zip,tar,tar.gz,tar.bz2文件
转自:http://wangli-5665.diandian.com/post/2011-08-18/4039228 这么多年来,数据压缩对我们来说是非常有用的.无论是在邮件中发送的图片用的zip文件 ...