注册广播BroadcastReceiver

一.动态注册广播
1.布局文件

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <Button
        android:id="@+id/register_button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="30dp"
        android:text="动态注册广播" />

    <Button
        android:id="@+id/jiechu_button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="30dp"
        android:text="解除广播" />

</LinearLayout>

1.mainactivity

public class MainActivity extends Activity implements OnClickListener {

    private Button registerButton, jiechuButton;

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        registerButton = (Button) findViewById(R.id.register_button1);
        jiechuButton = (Button) findViewById(R.id.jiechu_button2);

    }

    public void onClick(View v) {
    //创建过滤器对象,标明在触发拨号的时候才会触动该广播
        IntentFilter filter = new IntentFilter(
                "android.intent.action.PHONE_STATE");
    //设置优先级
        filter.setPriority(1000);

        switch (v.getId()) {
        case R.id.register_button1:// 动态注册广播
            /**
             * 参数一:要注册的广播接收者 参数二:当前广播的过滤器
             * */
            registerReceiver(new MyBroadcastReceiver(), filter);
            break;

        case R.id.jiechu_button2:// 解除广播
            unregisterReceiver(new MyBroadcastReceiver());
            break;

        }

    }

}

3.新建MyBroadcastReceiver 类继承BroadcastReceiver

public class MyBroadcastReceiver extends BroadcastReceiver {

    public void onReceive(Context context, Intent intent) {
        Log.i("myTag", "来电话了");
    }

}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Android Studio 中注册广播接收器(BroadcastReceiver)需要以下步骤: 1. 创建一个继承自 BroadcastReceiver 的类,这个类将处理接收到的广播消息。例如,你可以创建一个名为 MyBroadcastReceiver 的类。 ```java public class MyBroadcastReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { // 在这里处理接收到的广播消息 } } ``` 2. 在 AndroidManifest.xml 文件中声明广播接收器。在 `<application>` 标签内部添加如下代码: ```xml <receiver android:name=".MyBroadcastReceiver" android:enabled="true" android:exported="true"> <intent-filter> <action android:name="android.intent.action.ACTION_NAME" /> <!-- 添加其他需要匹配的 action --> </intent-filter> </receiver> ``` 其中,`android:name` 属性指定了广播接收器的类名,`.MyBroadcastReceiver` 表示当前包名下的 MyBroadcastReceiver 类。`android:enabled` 和 `android:exported` 属性分别用于启用和导出广播接收器。 3. 在你需要发送广播的地方,创建一个 Intent 对象,并使用 `sendBroadcast()` 方法发送广播。 ```java Intent intent = new Intent(); intent.setAction("android.intent.action.ACTION_NAME"); // 添加其他需要传递的数据 sendBroadcast(intent); ``` 其中,`"android.intent.action.ACTION_NAME"` 是你自定义的广播 action,用于匹配注册广播接收器。 这样,当发送的广播 action 与注册广播接收器的 action 匹配时,MyBroadcastReceiver 类中的 `onReceive()` 方法将被调用,你可以在该方法中处理接收到的广播消息。记得在不需要接收广播时,及时取消注册广播接收器。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值