zoukankan      html  css  js  c++  java
  • 广播基础

    public class MyActivity extends Activity {
    private MySelfReceiver1 receiver1;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.fragment_main);
    //在界面注册一个广播,在配置文件注册两个接受者,设置优先级
    //推荐使用
    receiver1=new MySelfReceiver1();
    IntentFilter filter=new IntentFilter("com.ruicaiedu.day1128_MySelf.MySelfReceiver1");
    registerReceiver(receiver1, filter);
    }

    @Override
    protected void onDestroy() {
    unregisterReceiver(receiver1);
    super.onDestroy();
    }
    //这只发送了广播,没有进行管理
    public void bReceiver1(View v) {// 第一个按钮的点击方法
    // 发送广播
    // Intent intent = new Intent(MyActivity.this,
    // MySelfReceiver1.class);//指定接收者
    // sendOrderedBroadcast(intent, null);
    //第三种,触发广播
    Intent intent=new Intent("com.ruicaiedu.day1228_Myself.MySelfReceiver1");
    sendOrderedBroadcast(intent, null);
    }

    public void bReceiver2(View v) {// 第二个按钮的点击方法
    // 发送广播
    // Intent intent = new Intent(MyActivity.this,
    // MySelfReceiver2.class);
    // sendOrderedBroadcast(intent, null);

    //第三种,触发广播
    Intent intent=new Intent("com.ruicaiedu.day1228_Myself.MySelfReceiver1");
    sendOrderedBroadcast(intent, null);
    }

    }

    配置文件如下:

    <!--注册一个自定义光播 -->
    <receiver android:name="com.ruicaiedu.day1228_Myself.MySelfReceiver1">接收者类名 
    <intent-filter >
    <action android:name="com.ruicaiedu.day1228_Myself.MySelfReceiver1"/>光播类名
    </intent-filter>
    </receiver>

    <!--注册一个自定义光播 -->
    <receiver android:name="com.ruicaiedu.day1228_Myself.MySelfReceiver2">
    <intent-filter android:priority="1000">
    <action android:name="com.ruicaiedu.day1228_Myself.MySelfReceiver1"/>
    </intent-filter>
    </receiver>

  • 相关阅读:
    python 协程
    python 打印乘法表
    python 线程池
    python 队列
    开发react 应用最好用的脚手架 create-react-app
    React面试题
    修改了背景透明度
    低门槛彻底理解JavaScript中的深拷贝和浅拷贝
    Web Worker 使用教程
    Vue2 实现时空穿梭框功能模块
  • 原文地址:https://www.cnblogs.com/wangfeng520/p/5082210.html
Copyright © 2011-2022 走看看