安卓动态广播

开启监听广播

 //IntentFilter  意图筛选 筛选我需要接收哪些广播
        IntentFilter intentFilter=new IntentFilter();
        //com.skypan.updatehe和dongzuo2   接收广播的名字
        intentFilter.addAction("com.skypan.update");
        intentFilter.addAction("dongzuo2");
        //开始接收广播
        LocalBroadcastManager.getInstance(this).registerReceiver(myBroadcast,intentFilter);

处理广播

//广播类
    private  class MyBroadcast extends BroadcastReceiver{
        //接受广播要干啥
        @Override
        public void onReceive(Context context, Intent intent) {
//            intent.getAction  获取动作
            switch (intent.getAction()){
                case "com.skypan.update":
                    String name = intent.getStringExtra("name");
                    ToastUtil.showMsg(BroadActivity.this,"我是com.skypan.update在BroadActivity----"+name);
                    t1.setText(name);
                    break;
                case "dongzuo2":
                    String name1 = intent.getStringExtra("name");
                    ToastUtil.showMsg(BroadActivity.this,"我是dongzuo2在BroadActivity---"+name1);
                    t1.setText(name1);
                    break;
            }
        }
    }

页面销毁广播


    @Override
    protected void onDestroy() {
        super.onDestroy();
        LocalBroadcastManager.getInstance(this).unregisterReceiver(myBroadcast);//销毁广播
    }

发送广播(带参数)

 Intent intent=new Intent("com.skypan.update");
                intent.putExtra("name","我是广播");
                LocalBroadcastManager.getInstance(Broad2Avtivity.this).sendBroadcast(intent);

示例1

package com.example.myapplicationstyud2;
import androidx.appcompat.app.AppCompatActivity;
import androidx.localbroadcastmanager.content.LocalBroadcastManager;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import com.example.textview.R;
import com.example.textview.Util.ToastUtil;


public class BroadActivity extends AppCompatActivity {

    private TextView t1;

    private MyBroadcast myBroadcast;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_broad);
        t1=(TextView) findViewById(R.id.t1);
        myBroadcast=new MyBroadcast();
        //IntentFilter  意图筛选 筛选我需要接收哪些广播
        IntentFilter intentFilter=new IntentFilter();
        //com.skypan.updatehe和dongzuo2   接收广播的名字
        intentFilter.addAction("com.skypan.update");
        intentFilter.addAction("dongzuo2");
        //开始接收广播
        LocalBroadcastManager.getInstance(this).registerReceiver(myBroadcast,intentFilter);
    }
    public void clickOn(View v){
        switch (v.getId()){
            case R.id.b1:
                Intent intent=new Intent(this,Broad2Avtivity.class);
                startActivity(intent);
                break;
            case R.id.b2:
                break;
            case R.id.b3:
                break;
        }
    }

    //广播类
    private  class MyBroadcast extends BroadcastReceiver{
        //接受广播要干啥
        @Override
        public void onReceive(Context context, Intent intent) {
//            intent.getAction  获取动作
            switch (intent.getAction()){
                case "com.skypan.update":
                    String name = intent.getStringExtra("name");
                    ToastUtil.showMsg(BroadActivity.this,"我是com.skypan.update在BroadActivity----"+name);
                    t1.setText(name);
                    break;
                case "dongzuo2":
                    String name1 = intent.getStringExtra("name");
                    ToastUtil.showMsg(BroadActivity.this,"我是dongzuo2在BroadActivity---"+name1);
                    t1.setText(name1);
                    break;
            }
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        LocalBroadcastManager.getInstance(this).unregisterReceiver(myBroadcast);//销毁广播
    }
}

相关发送代码上面有

示例2  监听网络变化

public class MainActivity extends AppCompatActivity {
    private IntentFilter intentFilter;
    private NetworkChangeReceiver networkChangeReceiver;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        //动态接受网络变化的广播接收器
        intentFilter = new IntentFilter();
        intentFilter.addAction("android.net.conn.CONNECTIVITY_CHANGE");

        networkChangeReceiver = new NetworkChangeReceiver();
        registerReceiver(networkChangeReceiver, intentFilter);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();

        //取消动态网络变化广播接收器的注册
        unregisterReceiver(networkChangeReceiver);
    }


    //自定义接受网络变化的广播接收器
    class NetworkChangeReceiver extends BroadcastReceiver {
        @Override
        public void onReceive(Context context, Intent intent) {
            ConnectivityManager connectionManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
            NetworkInfo networkInfo = connectionManager.getActiveNetworkInfo();

            if (networkInfo != null && networkInfo.isAvailable()) {
                Toast.makeText(context, "network is available", Toast.LENGTH_SHORT).show();
            } else {
                Toast.makeText(context, "network is unavailable", Toast.LENGTH_SHORT).show();
            }
        }
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

飞飞翼

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值