Android使用Broadcast实现无序静态与动态广播功能

来源:互联网 发布:python 绘制cdf图代码 编辑:程序博客网 时间:2024/06/05 03:43

利用sendBroadcast()实现发送广播,BroadcastReceiver 接收广播

实例Damo

效果:

发送者:


接收打印:



一个发送者,给两个接收者发送广播,一个静态获取一个动态获取

发送者:

xml文件中:

    <EditText        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:hint="请输入要发送的内容"        android:id="@+id/et_main_content"        />    <Button        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:text="发送"        android:onClick="send"        />
MainActivity中:

public class MainActivity extends AppCompatActivity {    private EditText et_main_content;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        //获取文本输入控件        et_main_content = (EditText) findViewById(R.id.et_main_content);    }    public void send(View view){        //获得发送的内容        String content=et_main_content.getText().toString();        //发送广播        Intent intent=new Intent();        //指定广播的名字        intent.setAction("com.example.g160628_android21_sender.Hug");        //指定广播的内容        intent.putExtra("content",content);        //发送无序广播        sendBroadcast(intent);    }}

接收者1号(静态获取):

在mainfest中,与activity控件同级添加

 <receiver android:name=".MyReceiver01">            <intent-filter>                <action android:name="com.example.g160628_android21_sender.Hug"></action>            </intent-filter>        </receiver>


实例化一个MyReceiver01.java

public class MyReceiver01 extends BroadcastReceiver {    @Override    public void onReceive(Context context, Intent intent) {        //获取广播的名字        String action=intent.getAction();        if("com.example.g160628_android21_sender.Hug".equals(action)){            String content=intent.getStringExtra("content");            Log.i("test","广播接受者1号:"+content);        }    }}

接收者2号(动态获取):

MainActivity中

public class MainActivity extends AppCompatActivity {    private MyReceiver02 myReceiver02;    private IntentFilter intentFilter;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        myReceiver02 = new MyReceiver02();        //实例化IntentFilter        intentFilter = new IntentFilter();        intentFilter.addAction("com.example.g160628_android21_sender.Hug");    }    @Override    protected void onResume() {        super.onResume();        //注册广播        registerReceiver(myReceiver02,intentFilter) ;    }    @Override    protected void onDestroy() {        super.onDestroy();        //取消注册 注销        unregisterReceiver(myReceiver02);    }}
MyReceiver02.java

public class MyReceiver02 extends BroadcastReceiver {    @Override    public void onReceive(Context context, Intent intent) {        //获取广播的名字        String action=intent.getAction();        if("com.example.g160628_android21_sender.Hug".equals(action)){            String content=intent.getStringExtra("content");            Log.i("test","广播接受者2号:"+content);        }    }}





阅读全文
0 0