一、简介

二、代码
1.xml
(1)activity_main.xml

 <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<Button
android:id="@+id/sendButton"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="发送消息给Receiver"
/>
</LinearLayout>

(2)AndroidManifest.xml.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.broadcast"
android:versionCode="1"
android:versionName="1.0" > <uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="21" /> <application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver android:name=".TestReceiver">
<intent-filter>
<action android:name="android.intent.action.EDIT"/>
</intent-filter>
</receiver>
</application>
<uses-permission android:name="android.permission.RECEIVE_SMS"/>
</manifest>

2.java
(1)MainActivity.java

 package com.broadcast;

 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 {
/** Called when the activity is first created. */
private Button sendButton;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
sendButton = (Button)findViewById(R.id.sendButton);
sendButton.setOnClickListener(new BroadcastListener());
}
class BroadcastListener implements OnClickListener{ @Override
public void onClick(View v) {
Intent intent = new Intent();
intent.setAction(Intent.ACTION_EDIT);//要与在AndroidManifest.xml设置值一致
MainActivity.this.sendBroadcast(intent);
} }
}

(2)TestReceiver.java

 package com.broadcast;

 import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent; public class TestReceiver extends BroadcastReceiver{ public TestReceiver(){
System.out.println("TestReceiver");
}
@Override
public void onReceive(Context context, Intent intent) {
System.out.println("onReceive");
}
}

ANDROID_MARS学习笔记_S01原始版_012_广播机制一的更多相关文章

  1. ANDROID_MARS学习笔记_S01原始版_013_广播机制二

    一.代码1.xml(1)main.xml <?xml version="1.0" encoding="utf-8"?> <LinearLayo ...

  2. ANDROID_MARS学习笔记_S01原始版_023_MP3PLAYER005_用广播BroacastReciever实现后台播放不更新歌词

    一.代码流程1.自定义一个AppConstant.LRC_MESSAGE_ACTION字符串表示广播"更新歌词" 2.在PlayerActivity的onResume()注册Bro ...

  3. ANDROID_MARS学习笔记_S01原始版_005_RadioGroup\CheckBox\Toast

    一.代码 1.xml(1)radio.xml <?xml version="1.0" encoding="utf-8"?> <LinearLa ...

  4. ANDROID_MARS学习笔记_S01原始版_004_TableLayout

    1.xml <?xml version="1.0" encoding="utf-8"?> <TableLayout xmlns:android ...

  5. ANDROID_MARS学习笔记_S01原始版_003_对话框

    1.AndroidManifest.xml <?xml version="1.0" encoding="utf-8"?> <manifest ...

  6. ANDROID_MARS学习笔记_S01原始版_002_实现计算乘积及menu应用

    一.代码 1.xml(1)activity_main.xml <RelativeLayout xmlns:android="http://schemas.android.com/apk ...

  7. ANDROID_MARS学习笔记_S01原始版_001_Intent

    一.Intent简介 二.代码 1.activity_main.xml <RelativeLayout xmlns:android="http://schemas.android.co ...

  8. ANDROID_MARS学习笔记_S01原始版_023_MP3PLAYER004_同步显示歌词

    一.流程分析 1.点击播放按钮,会根据lrc名调用LrcProcessor的process()分析歌词文件,得到时间队列和歌词队列 2.new一个hander,把时间队列和歌词队列传给自定义的线程类U ...

  9. ANDROID_MARS学习笔记_S01原始版_023_MP3PLAYER003_播放mp3

    一.简介 1.在onListItemClick中实现点击条目时,跳转到PlayerActivity,mp3info通过Intent传给PlayerActivity 2.PlayerActivity通过 ...

随机推荐

  1. Android——四种AterDialog

    本经验将分别介绍Android里面的四种AlertDialog分别是:显示带中立,取消,确定的提示框. 获取带列表的对话框按钮.获取带单选列表的对话框按.获取显示带多选项的对话框.此经验介绍获取显示带 ...

  2. android事件分发介绍

        Android事件分发 事件分发3个步骤 dispatchTouchEvent(event)派发 onInterceptTouchEvent(event)拦截 onTouchEvent(eve ...

  3. php文件上传限制

    PHP默认的上传限定是最大2M,想上传超过此设定的文件,需要调整PHP.apache等的一些参数.下面,我们简要介绍一下PHP文件上传涉及到的一些参数: file_uploads :是否允许通过HTT ...

  4. vs: 编译: 计算机中丢失mspdb100.dll

    在vs 2010中, 将Microsoft Visual Studio 10.0\VC\bin目录配置在path后(甚至添加lib以及include)后, 报如题错误. 是由于Microsoft Vi ...

  5. Microsoft SQL Server 获得本地帮助方法

    微软的自带的帮助文档不管是对于开发人员还是DBA都是相当的重要.一般在有网络的状况下可以直接访问 http://msdn.microsoft.com/query/dev10.query?appId=D ...

  6. Extjs搜索域使用

    要在使用的panel在预先加载搜索域类requires : ["Ext.ux.form.SearchField"],

  7. FusionChart 导出图片 功能实现(转载)

    FusionChart 导出图片 功能实现(转载) http://www.cnblogs.com/jiagoushi/archive/2013/02/05/2893468.html 题目:精美Fusi ...

  8. wamp优化

    友情链接:IT狂人博客 转载请注明作者:浮沉雄鹰 和本文链接:http://www.cnblogs.com/xby1993/p/3342085.html 一.修改php.ini, 修改上传文件大小限制 ...

  9. 阅读 Linux 内核源码

    阅读Linux Kernel Source Code 假如你在Linux系统下面阅读Linux内核源代码,那么需要准备一些工具. ①Linux的内核源码 内核源码的下载地址:Index of /pub ...

  10. How to hide an entry in the Add/Remove Programs applet?

    Original link: http://www.winhelponline.com/articles/15/1/How-to-hide-an-entry-in-the-AddRemove-Prog ...