首页 > 代码库 > 了解BroadcastRecever

了解BroadcastRecever

广播分类:

  标准广播(Normal broadcasts):完全异步执行的广播,接收没有先后顺序,效率高,无法被接收器被拦截。

  有序广播(Ordered broadcasts) :同步执行的广播,有先后顺序,广播可以被接收器拦截或修改。

 

接收系统广播:

  动态注册监听网络变化:(只能在程序启动后才能接收到广播,程序结束,广播取消)

    1.再activity中注册广播并处理接收后的逻辑

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.com.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 connectivityManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
            NetworkInfo networkInfo = connectivityManager.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();
            }

        }
    }
}

 



    2.还要添加访问网络状态的权限:

    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>


 静态注册实现开机启动:(程序结束后还能正常接收广播)
  1.创建广播接收类:
    
public class BootCompleteReceiver extends BroadcastReceiver {
    public BootCompleteReceiver() {
    }

    @Override
    public void onReceive(Context context, Intent intent) {
        
    }
}

 

   2.在Manifest中静态注册:

 
   <receiver
            android:name=".BootCompleteReceiver"
            android:enabled="true"
            android:exported="true">
            <intent-filter>
                <action android:name="android.intent.action.BOOT_COMPLETED"/>
            </intent-filter>
        </receiver>
    enable:是否启用广播接收器
    exported:是否允许接收器接收本程序以外的广播
    添加监听启动完成广播权限:
      <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
 

 

 

 

发送自定义广播:

  发送标准广播

    1.首先定义广播接收器


public class MyBroadcastReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        

  }
}

 


   2.在Manifest中注册:
  
   
<receiver
            android:name=".MyBroadcastReceiver"
            android:enabled="true"
            android:exported="true">
            <intent-filter>
                <action android:name="com.example.broadcast.MY_BROADCAST"/>
            </intent-filter>
        </receiver>

 

  3.发送广播:
    
public void onClick(View v) {
        if(v.getId() == R.id.send){
            Intent intent = new Intent("com.example.broadcast.MY_BROADCAST");
            sendBroadcast(intent);
        }
    }

 

 

  发送有序广播

    1.在注册广播接收器时给intent-filter设置优先级属性,数值越大,优先级越高

    

<receiver
            android:name=".BootCompleteReceiver"
            android:enabled="true"
            android:exported="true">
            <intent-filter
                android:priority="100">
               <action android:name="android.intent.action.BOOT_COMPLETED"/>
            </intent-filter>
        </receiver>

   2.发送有序广播:
    
    Intent intent = new Intent("com.example.broadcast.MY_BROADCAST");
           sendOrderedBroadcast(intent,null);//只需要修改发送方法
  3.接收到广播时进行拦截或者修改处理:
    
public class BootCompleteReceiver extends BroadcastReceiver {
    public BootCompleteReceiver() {
    }

    @Override
    public void onReceive(Context context, Intent intent) {
        abortBroadcast();//拦截广播
        setResult(int code,Stirng data, Bundle extras);//修改结果传递给下一个广播
        //获取上一个广播传来的数据
        getResultCode();
        getResultData();
        getResultExtras(boolean makeMap);
    }
}

  

   4.终结广播(不管是否拦截,都会最终执行)

    Intent intent = new Intent("com.example.broadcast.MY_BROADCAST");

    sendOrderedBroadcast(intent,null,new BootCompleteReceiver(),new Handler(),0,null,null);




使用本地广播:
  本地广播只能在应用内部传递

  1.创建广播接收器

  class NetworkChangeReceiver extends BroadcastReceiver{

        @Override
        public void onReceive(Context context, Intent intent) {
           
            }

        }
    }

 


  2.获取本地广播管理类:

    LocalBroadcastManager localBroadcastManager = LocalBroadcastManager.getInstance(this);

  3.注册广播接收器

    localBroadcastManager.registerReceiver(networkChangeReceiver,intentFilter);

  4.发送广播:

    localBroadcastManager.sendBroadcast(intent);

  5.注销广播

   localBroadcastManager.unregisterReceiver(networkChangeReceiver);

 

 

 


  

 

 

 


  



  

 

  

了解BroadcastRecever