您的位置:首页 > 其它

自定义广播发送、接收

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>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: