22 广播静态创建代码案例

结构:
22 广播静态创建代码案例
文章图片

清单文件AndroidManifest.xml:


MainActivity.java
package com.qf.day22_broadcastreceiver_demo1; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.view.View; public class MainActivity extends Activity {@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); }//发送广播 public void MySendClick(View v){//Intent intent = new Intent(MainActivity.this, MyBroadCastReceiver01.class); Intent intent = new Intent(); intent.setAction("com.qq.weixin"); intent.putExtra("str", "疯狗咬人"); //发送普通的广播 sendBroadcast(intent); }}

MyBroadCastReceiver01.java
package com.qf.day22_broadcastreceiver_demo1; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.widget.Toast; public class MyBroadCastReceiver01 extends BroadcastReceiver{/** * 就一个生命周期普通广播10s */ @Override public void onReceive(Context context, Intent intent) { // TODO Auto-generated method stub}}

MyBroadCastReceiver02.java
MyBroadCastReceiver03.java
与上一个代码一样
【22 广播静态创建代码案例】结果:三个广播接受者 根据priority大小先后弹吐司

    推荐阅读