发送自定义广播程序:

布局文件:

<?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(4) - 活动(Activity), 服务(Service), 广播(Bro
  2. Android(安卓)Socket 发送广播包的那些坑
  3. Android(安卓)Socket 发送广播包的那些坑
  4. Android(安卓)Studio 单刷《第一行代码》系列 07 —— Broadcast
  5. Andriod AOA协议通信总结
  6. Android系统中自带的图标&一些预定义样式&参考颜色值
  7. android之SMS
  8. Android系统中自带的图标&一些预定义样式&参考颜色值
  9. Android(安卓)知识总结

随机推荐

  1. 【Android(安卓)电量优化】JobScheduler
  2. 通过ua检测浏览页面的设备是phone还是tab
  3. android语音识别 android.speech 包分析
  4. Android设置Selector不同状态下颜色及图
  5. 解决Ubuntu报错 E: Unable to locate pac
  6. CSS:媒体查询-复习
  7. CSS:表格的常用样式-复习
  8. CSS:定位与定位元素-复习
  9. 淘宝首页框架
  10. 如何在Linux系统上刷抖音