安卓BroadcastReceiver组件使用系列1:普通广播的发送和接收
2016-03-08 14:47
381 查看
广播接收者BroadcastReceiver是安卓开发中的四大组件之一,下面我们来介绍一个普通广播的使用。广播的生命周期:onReceive方法执行开始到执行结束
注意:在广播里不能有对话框,也不能绑定service,也不能执行耗时操作。它是接收到一个信息之后来提醒用户。
整体思路:在xml'文件中放置一个Button控件,在这个Button的点击事件中,定义一个意图发送广播。定义一个MyReceiver类,继承BroadcastReceiver,在这个类的onReceive方法中弹出状态栏通知信息。注意需要再清单文件AndroidManifest.xml中声明这个receiver。这样就完成了普通广播的发送和接收。
activity_main.xml文件:
注意:在广播里不能有对话框,也不能绑定service,也不能执行耗时操作。它是接收到一个信息之后来提醒用户。
整体思路:在xml'文件中放置一个Button控件,在这个Button的点击事件中,定义一个意图发送广播。定义一个MyReceiver类,继承BroadcastReceiver,在这个类的onReceive方法中弹出状态栏通知信息。注意需要再清单文件AndroidManifest.xml中声明这个receiver。这样就完成了普通广播的发送和接收。
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" 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=".MainActivity" > <Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentTop="true" android:layout_centerHorizontal="true" android:layout_marginTop="53dp" android:text="发送广播" /> </RelativeLayout>MainActivity.java文件:
package com.example.android_broadcast_normal; //普通广播的使用 import android.os.Bundle; import android.app.Activity; import android.content.Intent; import android.view.Menu; import android.view.View; import android.widget.Button; public class MainActivity extends Activity { private Button button; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); button=(Button)findViewById(R.id.button1); button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View arg0) { // TODO Auto-generated method stub Intent intent=new Intent(MainActivity.this,MyReceiver.class); sendBroadcast(intent);//发送广播 } }); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } }MyReceiver.java文件:
package com.example.android_broadcast_normal; import android.annotation.SuppressLint; import android.app.Notification; import android.app.NotificationManager; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; //广播的生命周期: //onReceive方法执行开始到执行结束 //注意:在广播里不能有对话框,也不能绑定service,也不能执行耗时操作 //它是接收到一个信息之后来提醒用户 public class MyReceiver extends BroadcastReceiver { //不知道api16那个地方有没有问题 private NotificationManager manager; private Notification.Builder builder; @SuppressLint("NewApi") @Override public void onReceive(Context context, Intent intent) { // TODO Auto-generated method stub manager=(NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE); builder=new Notification.Builder(context); builder.setContentTitle("有短信来啦!"); builder.setContentText("晚上有时间吗?"); builder.setSmallIcon(R.drawable.ic_launcher); manager.notify(1001,builder.build()); } }
相关文章推荐
- 我对归并排序的理解
- 闲暇时光
- 帧接收流程
- 结构体字节对齐,pragma pack,__attribute__(packed)
- 我的新博客地址http://xxxbw.github.io/
- AndroidStudio Gradle项目中添加JNI生成文件(.so文件)
- JavaScript笔试题
- QPS/TPS等系统指标
- showModalDialog()子窗口刷新父窗口
- gulp相关
- iso 视频需要断点续传
- caffe: math_functions
- swift - use backslash to add the value in the string
- CodeForces - 630H Benches (组合数学)
- 关于IOS中通过图片实现动画效果
- IOS开发-NSNumber、NSValue、NSData的区别
- Tomcat配置SSL(单向认证)
- CentOS6 安装pgloader
- Linux配置
- 实现一个strcpy函数