[Android]自定义广播之标准广播
来源:互联网 发布:莫寒应援会的淘宝店 编辑:程序博客网 时间:2024/05/22 15:54
自定义广播允许开发人员在应用程序内进行发送自己定义的广播。
1、新建MyBroadcastReceiver类,继承于BroadcastReceiver,在onReceive方法中,接收到广播内容。
package com.example.broadcasttest1;import android.content.BroadcastReceiver;import android.content.Context;import android.content.Intent;import android.widget.Toast;public class MyBroadcastReceiver extends BroadcastReceiver{ @Override public void onReceive(Context arg0, Intent arg1) { // TODO Auto-generated method stub Toast.makeText(arg0, "received in MyBroadcastReceiver", Toast.LENGTH_SHORT).show(); }}
2、在activity_main.xml中,新建一个按钮。
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <Button android:id="@+id/button" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Send Broadcase" /></LinearLayout>
3、在MainActivity中,点击按钮事件中发送一条自定义广播
package com.example.broadcasttest1;import android.support.v7.app.ActionBarActivity;import android.support.v7.app.ActionBar;import android.support.v4.app.Fragment;import android.content.Intent;import android.os.Bundle;import android.view.LayoutInflater;import android.view.Menu;import android.view.MenuItem;import android.view.View;import android.view.ViewGroup;import android.widget.Button;import android.os.Build;public class MainActivity extends ActionBarActivity { private Button button; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); button = (Button)findViewById(R.id.button); button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View arg0) { // TODO Auto-generated method stub Intent intent = new Intent("com.example.broadcasttest1.**MY_BROADCAST**"); // 发送广播信息 sendBroadcast(intent); } }); }}
4、最后在AndroidManifest.xml 中,对广播接收器进行注册。
<receiver android:name=".MyBroadcastReceiver"> <intent-filter> <action android:name="com.example.broadcasttest1.**MY_BROADCAST**"/> </intent-filter> </receiver>
点击按钮,我们可以看到界面弹出一条信息:“received in MyBroadcastReceiver”。说明我们发送的自定义消息已经发送并且被收到。
0 0
- [Android]自定义广播之标准广播
- 发送自定义广播 之 标准广播
- Android之广播机制—自定义广播
- Android广播之发送自定义广播+本地广播(二)
- 标准广播,有序广播,本地广播和自定义广播
- 自定义标准广播
- Android 之 BroadcastReceiver自定义广播
- android之自定义有序广播
- 广播接收者之自定义广播
- 广播接收者之自定义广播
- 42.android广播-自定义广播
- android五大组件之广播机制---自定义广播
- android之旅16 自定义广播与无序有序广播
- Android之广播三(发送自定义广播)
- 【Android】安卓学习笔记之广播(三)发送标准广播和有序广播
- Android开发之发送自定义广播
- Android系列之自定义有序广播
- Android BroadcastReceiver之自定义无序、有序广播
- 为什么有人说富坚义博有着殿堂级的画功?他的画功体现在哪儿呢?
- iot 表索引dump《2》
- Ubuntu 16.04 64位 Sublime build3114 不支持搜狗输入法的解决方案
- VMware虚拟机中对ubuntu系统配置静态IP
- C++之_In_和_out_
- [Android]自定义广播之标准广播
- java框架之struts2数据类型转换(2)
- hdoj 1513 Palindrome(LCS)
- 前端构建:Less入了个门
- C# 图片缩略
- 解决ubuntu下载的速度很慢问题
- SPSS——描述性统计分析——描述
- 剑指offer系列-T10二进制中1的个数
- JPEGImageEncoder报错解决