发送自定义广播程序:

布局文件:

<?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:onClick="send"            android:text="点击发送广播"            android:layout_width="match_parent"            android:layout_height="wrap_content"/></LinearLayout>

activity.java:

import android.app.Activity;import android.content.Intent;import android.os.Bundle;import android.view.View;public class MyActivity extends Activity {    @Override    public void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.main);    }    public void send(View view) {        //创建一个意图(装载广播事件)        Intent intent = new Intent();        intent.setAction("com.wuyou.something");        //发送无序广播        sendBroadcast(intent);    }}

接收自定义广播程序:

在AndroidManifest.xml中配置广播接收者:

        <receiver android:name=".MyBroadcastReceiver">            <intent-filter>                <action android:name="com.wuyou.something"></action>            </intent-filter>        </receiver>
import android.content.BroadcastReceiver;import android.content.Context;import android.content.Intent;import android.widget.Toast;/** * Created by Heyiyong on 14-1-4 上午10:59. */public class MyBroadcastReceiver extends BroadcastReceiver {    public void onReceive(Context context, Intent intent) {        Toast.makeText(context, "收到自定义的广播", 1).show();    }}

更多相关文章

  1. Android 在程序界面上显示图片
  2. android之应用程序退到android桌面的实现
  3. android 安装和卸载程序
  4. android 笔记 --- Android应用程序的权限列表
  5. 2、创建android应用程序
  6. Android 安装卸载程序
  7. Android上的Adobe AIR应用程序运行环境发布了!
  8. android通过USB使用真机调试程序

随机推荐

  1. 数组函数的练习
  2. 搞懂作用域与闭包
  3. Python_学习之基础数据类型
  4. Python_学习之虚拟环境的搭建
  5. 2021-04-05:给两个长度分别为M和N的整型数
  6. 工具需用好,阅读源码没烦恼
  7. 用 Hypothesis 来自动化单元测试
  8. Python_学习之流程控制
  9. Python_学习之运算符
  10. 课程学习记录之python迭代器和生成器