BroadcastReceiver 案例
BroadcastReceiver 可以接收来自系统和应用的广播,他的生命周期非常简单,只是从对象开始调用他到运行onReceiver方法之后就结束了。要想使用BroadcastReceiver和使用Activity一样首先要继承他。
1:activity_main.xml

<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" > <EditText
android:id="@+id/et_info"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="输入广播消息"/> <Button
android:id="@+id/btn_send"
android:layout_below="@id/et_info"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Send"/> </RelativeLayout>
2:MainActivity.java
public class MainActivity extends Activity {
private EditText etInfo=null;
private Button btnSend=null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
etInfo=(EditText)findViewById(R.id.et_info);
btnSend=(Button)findViewById(R.id.btn_send);
btnSend.setOnClickListener(new OnClickListener(){
public void onClick(View view){
String info=etInfo.getText().toString();
Intent intent=new Intent();
//setAction()中的字符串的值与AndroidManifest.xml中 receiver->action中的值相同
intent.setAction("com.example.broadcast.MyBroadcastReceiverTest");
intent.putExtra("info", info);
sendBroadcast(intent);
}
});
}
}
3:MyBroadcastReceiver.java
public class MyBroadcastReceiver extends BroadcastReceiver{
private Context context=null;
@Override
public void onReceive(Context arg0, Intent arg1) {
this.context=arg0;
showNotification(arg1);
}
private void showNotification(Intent intent){
String info=intent.getExtras().getString("info");
NotificationManager nm=(NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
Notification n=new Notification(R.drawable.ic_launcher,info,System.currentTimeMillis());
PendingIntent pi=PendingIntent.getActivity(context, 0, new Intent(context,MainActivity.class), 0);
n.setLatestEventInfo(context, info, null, pi);
nm.notify(R.layout.activity_main,n);
}
}
4:AndroidManifest.xml
<receiver android:name="com.example.broadcast.MyBroadcastReceiver">
<intent-filter>
<action android:name="com.example.broadcast.MyBroadcastReceiverTest"/>
</intent-filter>
</receiver>
5:运行结果:

BroadcastReceiver 案例的更多相关文章
- Android实训案例(六)——四大组件之一BroadcastReceiver的基本使用,拨号,短信,SD卡,开机,应用安装卸载监听
Android实训案例(六)--四大组件之一BroadcastReceiver的基本使用,拨号,短信,SD卡,开机,应用安装卸载监听 Android中四大组件的使用时重中之重,我这个阶段也不奢望能把他 ...
- Service实时向Activity传递数据案例
转自 http://www.cnblogs.com/linjiqin/p/3147764.html 演示一个案例,需求如下:在Service组件中创建一个线程,该线程用来生产数值,每隔1秒数值自动加1 ...
- Android(java)学习笔记177:BroadcastReceiver之 应用程序安装和卸载 的广播接收者
国内的主流网络公司(比如网易.腾讯.百度等等),他们往往采用数据挖掘技术获取用户使用信息,从而采用靶向营销.比如电脑上,我们浏览网页的时候,往往会发现网页上会出现我们之前经常浏览内容的商 ...
- Android(java)学习笔记176:BroadcastReceiver之 短信发送的广播接收者
有时候,我们需要开发出来一个短信监听器,监听用户发送的短信记录,下面就是一个案例,这里同样需要使用广播机制. 下面同样是代码示例,MainActivity.java 和 activity_main. ...
- Android(java)学习笔记173:BroadcastReceiver之 静态注册 和 动态注册
1. 广播接受者>什么是广播.收音机.电台:对外发送信号.收音机:接收电台的信号. >在android系统里面,系统有很多重要的事件: 电池电量低,插入充电器,sd卡被移除,有电话打出去, ...
- Android(java)学习笔记245:ContentProvider使用(银行数据库创建和增删改查的案例)
1. Android的四大组件: (1)Activity 用户交互的UI界面 (2)Service 后台运行的服务 (3)BroadcastReceiver 广播接收者 (4)ContentPro ...
- Android BLE与终端通信(五)——Google API BLE4.0低功耗蓝牙文档解读之案例初探
Android BLE与终端通信(五)--Google API BLE4.0低功耗蓝牙文档解读之案例初探 算下来很久没有写BLE的博文了,上家的技术都快忘记了,所以赶紧读了一遍Google的API顺便 ...
- 笔记:BroadcastReceiver的运行过程
广播概述 广播用来在组件之间传递消息,可以是同进程或跨进程. 广播机制是基于发布订阅的事件驱动模型,使用上比Binder通信(跨进程接口回调)更低耦合.简单. ActivityManagerServi ...
- 服务 AIDL 定向tag IPC Parcelable 案例 MD
Markdown版本笔记 我的GitHub首页 我的博客 我的微信 我的邮箱 MyAndroidBlogs baiqiantao baiqiantao bqt20094 baiqiantao@sina ...
随机推荐
- linux内核--自旋锁的理解
http://blog.chinaunix.net/uid-20543672-id-3252604.html 自旋锁:如果内核配置为SMP系统,自旋锁就按SMP系统上的要求来实现真正的自旋等待,但是对 ...
- max key length is 1000 bytes
ALTER TABLE ad_keyword_brand ADD CONSTRAINT w1 UNIQUE (keyword,adasin,brand,page_position,country,ad ...
- SecureCRT连接vm中的ubuntu
如何使用SecureCRT连接ubuntu 用secureCRT连接Ubuntu是出现远程系统拒绝访问..经过一翻研究才知道Ubuntu上没有ssh.. 一下为连接过程. 1. 首先要明白什么是ssh ...
- Android Application对象必须掌握的七点
1:Application是什么? Application和Activity,Service一样,是android框架的一个系统组件,当android程序启动时系统会创建一个 ap ...
- POJ2230 Watchcow【欧拉回路】
Watchcow Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 6172Accepted: 2663 Special Judge ...
- 1033 - Merging Maps
Pictures taken from an airplane or satellite of an ar ea to be mapped are often of sufficiently high ...
- Android学习四、Android中的Adapter
一.Adapter的介绍 An Adapter object acts as a bridge between an AdapterView and the underlying data for t ...
- 如何让自己的Android程序永不被系统kill
一般来说,在Android系统中,当某进程较长时间不活动,或系统资源比较紧时,该进程可能被系统kill掉,以此来回收一些资源.Android系统会根据进程的优先级来选择性地杀死一些进程,优先级从高到低 ...
- word2vec浅析
本文是參考神经网络语言模型.word2vec相关论文和网上博客等资料整理的学习笔记.仅记录 自己的学习历程,欢迎拍砖. word2vec是2013年google提出的一种神经网络的语言模型,通过神经网 ...
- RPM包校验和提取
一.RPM包校验 [root@localhost Packages]# rpm -V 已安装的包名 #选项: # -V 校验指定RPM包中的文件(verify) [root@localho ...