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 知识点分类 一方面写写自己走过的弯路掉进去的坑,避免以后再犯:另一方面希望通过分享自己的经 ...
随机推荐
- VLOG丨树莓派Raspberry Pi 3安装PLEX并挂载USB硬盘打造最牛的微型家庭影音服务器2018
视频介绍 树莓派3安装目前最流行的PLEX服务器,实现既能最大限度降低功耗,也能随时随地观看分享影片. 一.在树莓派下安装PLEX媒体服务器 1.在终端,将你的树莓派更新至最新 sudo apt up ...
- PHP Client for Mysql Binlog
PHP解析MySQL Binlog,依赖于mysql-replication-listener库 详见:https://github.com/bullsoft/php-binlog Install M ...
- SpringBoot进阶
一.表单验证 二.AOP处理请求 AOP是一种编程范式.与语言无关,是一种程序设计思想. 面向过程到面向对象.换个角度看世界,换个姿势处理问题. 2.1AOP实例-http请求 MAVEN添加依赖:o ...
- 如何学习Linux
为啥我们要学习Linux 技术的价值不在于这个技术有多么高超,而在于技术本身给我们带来什么价值,所以很多时候我们学习一个技术,不能盲目学,是为了使用这个技术,知道这个技术的使用场景,知道这个技术带来的 ...
- Qt多个信号连接到一个槽,在槽中识别信号的发送者方法(实验 可行)
Qt是通过信号和槽的机制进行事件传递的,当有多个不同类型.或相同类型的物件的发送信号都通过一个槽来处理的时候,需要在槽中识别出这些信号然后做相应的处理. 例如: 在一个界面中有16个按钮(QPushB ...
- Android Gradle 引用本地 AAR 的几种方式
折衷方案: 1.方式2 - 不完美解决办法2 2.再使用"自定义Gradle代码"来减轻重复设置的问题. 自定义Gradle代码如下: repositories { flatDir ...
- [svc][op]vim常用命令汇总
vim常用命令汇总: 定位 本行第一个字符 ctrl+$ 本行最后一个字符 0gg 文章首行 ctrl+G 文章行尾 u 撤销(Undo) 删除 D 从当前位置删除到行尾 ("d$" ...
- rpm -e 包名 卸载安装的二进制包
rpm -e --nodeps nc-.el6.x86_64 #--nodeps 不包含依赖包,直接删除rpm包
- JIT IR,C2
A Simple Graph-Based Intermediate Representation http://www.oracle.com/technetwork/java/javase/tech/ ...
- ny14 会场安排问题
会场安排问题 时间限制:3000 ms | 内存限制:65535 KB 难度:4 描述 学校的小礼堂每天都会有许多活动,有时间这些活动的计划时间会发生冲突,需要选择出一些活动进行举办.小刘的工 ...