Android 四大组件之 BroadcastReceiver
0 简介
1 普通广播
1.1 自定义广播 MyReceiver.java
package com.broadcastreceiver;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.widget.Toast;
public class MyReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
// TODO 自动生成的方法存根
Toast.makeText(context, intent.getAction()+intent.getStringExtra("message"), Toast.LENGTH_SHORT).show();
}
}
1.2 注册广播
<receiver android:name="com.broadcastreceiver.MyReceiver">
<intent-filter >
<action android:name="michael.FIRSTRECEIVER"/>
</intent-filter>
</receiver>
1.3 发送广播
package com.broadcastreceiver;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); Button btn_send=(Button)findViewById(R.id.btn_send);
btn_send.setOnClickListener(new OnClickListener() { @Override
public void onClick(View v) {
Intent intent=new Intent();
intent.setAction("michael.FIRSTRECEIVER");
intent.putExtra("message", "我是神奇的广播消息!");
sendBroadcast(intent);
}
});
}
}
2 有序广播
2.1 在前一节的基础上再新建个 广播 YourReceiver.java
package com.broadcastreceiver;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.widget.Toast;
public class YourReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Bundle bundle=getResultExtras(true);
String first=bundle.getString("first");
Toast.makeText(context, "上一个接收器转播的数据为:"+first, Toast.LENGTH_LONG).show();
}
}
2.2 代码注册广播
package com.broadcastreceiver;
import android.app.Activity;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class MainActivity extends Activity {
YourReceiver yourreceiver=null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); yourreceiver=new YourReceiver();
Button btn_send=(Button)findViewById(R.id.btn_send);
btn_send.setOnClickListener(new OnClickListener() { @Override
public void onClick(View v) {
Intent intent=new Intent();
intent.setAction("michael.FIRSTRECEIVER");
intent.putExtra("message", "我是神奇的广播消息!");
sendBroadcast(intent);
}
}); //代码动态注册广播
IntentFilter filter=new IntentFilter();
filter.addAction("michael.FIRSTRECEIVER");
filter.setPriority(10);
registerReceiver(yourreceiver, filter);
}
@Override
protected void onDestroy() {
//程序退出前取消注册广播
unregisterReceiver(yourreceiver);
super.onDestroy();
}
}
2.3 接收优先级
Android 四大组件之 BroadcastReceiver的更多相关文章
- [置顶] Android四大组件之BroadcastReceiver
Android四大组件之BroadcastReceiver Broadcast Receiver 广播接收器,是一种负责接收广播消息并对消息做出响应的组件,和Service一样并不提供与用户交互的UI ...
- Android 四大组件之“ BroadcastReceiver ”
前言 Android四大组件重要性已经不言而喻了,今天谈谈的是Android中的广播机制.在我们上学的时候,每个班级的教室里都会装有一个喇叭,这些喇叭都是接入到学校的广播室的,一旦有什么重要的通知,就 ...
- Android四大组件:BroadcastReceiver 介绍
介绍 BroadcastReceiver 即广播组件,是 Android 的四大组件之一.用于监听和接收广播消息,并做出响应.有以下一些应用: 不同组件之间的通信(应用内或不同应用之间). 多线程之间 ...
- Android四大组件之BroadcastReceiver
什么是BroadcastReceiver? BroadcastReceiver也就是“广播接收者”的意思,顾名思义,它就是用来接收来自系统和应用中的广播. 在Android系统中,广播体现在方方面面, ...
- Android四大组件之—— BroadcastReceiver的使用
BroadcastReceiver又名广播接收者.既然它用于接收广播,那一定就有人负责发送. Android系统中的广播: 在现实生活中,我们都知道广播是什么,用来做什么.例如公园里的广播,主要通知游 ...
- Android四大组件之一“广播”
前言 Android四大组件重要性已经不言而喻了,今天谈谈的是Android中的广播机制.在我们上学的时候,每个班级的教室里都会装有一个喇叭,这些喇叭都是接入到学校的广播室的,一旦有什么重要的通知,就 ...
- Android 四大组件之" ContentProvider "
前言 ContentProvider作为Android的四大组件之一,是属于需要掌握的基础知识,可能在我们的应用中,对于Activity和Service这两个组件用的很常见,了解的也很多,但是对Con ...
- 【Android开发日记】之入门篇(六)——Android四大组件之Broadcast Receiver
广播接受者是作为系统的监听者存在着的,它可以监听系统或系统中其他应用发生的事件来做出响应.如设备开机时,应用要检查数据的变化状况,此时就可以通过广播来把消息通知给用户.又如网络状态改变时,电量变化时都 ...
- Android实训案例(六)——四大组件之一BroadcastReceiver的基本使用,拨号,短信,SD卡,开机,应用安装卸载监听
Android实训案例(六)--四大组件之一BroadcastReceiver的基本使用,拨号,短信,SD卡,开机,应用安装卸载监听 Android中四大组件的使用时重中之重,我这个阶段也不奢望能把他 ...
随机推荐
- DOS 选择跳转实现、dos + bcp 双击导入和导出数据
DOS 选择跳转实现.dos + bcp 双击导入和导出数据 option.bat @echo off :Start2 cls goto Start :Start title Frequently U ...
- E514:write error(file system full?)
vi编辑某文件,保存时报错,提示:E514: write error (file system full?)---写入错误,磁盘满了? 查看磁盘空间:df -h根目录磁盘空间已满,used%100. ...
- Webstrom 使用过程中遇到的问题以及解决方案
作为一个前端开发,我用过webstorm和sublime两个编辑器.sublime小巧轻便,不耗内存.但是webstorm具有以下特点,让我难以舍弃. webstorm优点 点击一个函数名,它会跳到函 ...
- Installing the Eclipse Plugin
Installing the Eclipse Plugin Android offers a custom plugin for the Eclipse IDE, called Android Dev ...
- PHP学习笔记十八【构造函数】
<?php class Person{ public $name; public $age; //定义构造函数 function 空格__construct 构造方法没有返回值,对象自动调用 p ...
- cocos2d-x 2.2.6中c++通过JNI与java互调
1.HelloCpp.java /**************************************************************************** Copyri ...
- nginx配置文件(反向代理+集群+动静分离)
1.nginx纯反向代理配置(nginx.conf): #user nobody;worker_processes 4;error_log logs/error.log info;pid logs/n ...
- 省市联动JQ封装比较简洁调用的方法
前言 因为省市联动的需求在每个项目几乎存在,所以本人也对此在web页面通过封装比较简洁的JQ方法循环判断调用调用后台获取数据再绑定到Select表单上.如果对代码有什么疑问或者更好办法可以在评论区留言 ...
- K - Ignatius and the Princess IV
Description "OK, you are not too bad, em... But you can never pass the next test." ...
- HttpURLConnection详解
HttpURLConnection详解 07. 五 / J2EE / 没有评论 HttpURLConnection类的作用是通过HTTP协议向服务器发送请求,并可以获取服务器发回的数据. Http ...