自定义广播发送、接收
2015-06-21 20:37
309 查看
//发送自定义广播
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
/**
* 发送广播事件 (消息)
* @param view
*/
public void click(View view){
Intent intent = new Intent();
//自定义一个广播动作。
intent.setAction("com.itheima.sender.jiuminga");
//大吼一声 把消息发出去了。 无序广播
sendBroadcast(intent);
//发送有序广播
//sendOrderedBroadcast(intent, receiverPermission);
}
}
//xml文件
<activity
android:name="com.itheima.sender.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>
//广播接收
public class PoliceReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(context, "我是警察,我收到了消息", 1).show();
}
}
//xml文件
<receiver android:name="com.itheima.police.PoliceReceiver">
<intent-filter >
<action android:name="com.itheima.sender.jiuminga"/>
</intent-filter>
</receiver>
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
/**
* 发送广播事件 (消息)
* @param view
*/
public void click(View view){
Intent intent = new Intent();
//自定义一个广播动作。
intent.setAction("com.itheima.sender.jiuminga");
//大吼一声 把消息发出去了。 无序广播
sendBroadcast(intent);
//发送有序广播
//sendOrderedBroadcast(intent, receiverPermission);
}
}
//xml文件
<activity
android:name="com.itheima.sender.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>
//广播接收
public class PoliceReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(context, "我是警察,我收到了消息", 1).show();
}
}
//xml文件
<receiver android:name="com.itheima.police.PoliceReceiver">
<intent-filter >
<action android:name="com.itheima.sender.jiuminga"/>
</intent-filter>
</receiver>
相关文章推荐
- Java 两大数相乘
- C++ const
- fastjson
- Android OpenGL ES(一)----必备知识
- SVN小试牛刀
- C++对ASCII文件的读写操作
- HDU 3549 Flow Problem
- TCP三次握手和四次挥手协议
- Spring MVC基本原理
- 操作系统如何把程序从磁盘加载到内存并创建进程的?
- BZOJ1707 : [Usaco2007 Nov]tanning分配防晒霜
- 数据结构——改进的冒泡排序(c++)
- grep命令参数和使用方法
- BZOJ 2101: [Usaco2010 Dec]Treasure Chest 藏宝箱( dp )
- 《一个程序猿的生命周期》读后感
- vps添加swap
- java中的几种内部类
- java学习笔记1
- 读后感
- 第十三周项目1-动物这样叫(2)