本文主要是介绍自定义广播发送者和接收者,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
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.broadcast.xxx" ); //设置意图动作
sendBroadcast(intent); //对外发送广播
}
-----------------------------------------------------------------------------------------------------
<!--自定义广播接收者配置 -->
< receiver android:name = "com.itheima.receiver.MyReceiver" >
< intent-filter >
< action android:name = "com.itheima.broadcast.xxx" />
</ intent-filter >
</ receiver >
public class MyReceiver extends BroadcastReceiver {
/**
* 创建自定义广播接收者
*/
@Override
public void onReceive(Context context, Intent intent) {
System. out .println( "我是自定义广播接收者我接收到了自定义广播事件....." );
}
}
这篇关于自定义广播发送者和接收者的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!