BroadCastReceiver广播基础

it2022-05-05  142

BroadCastReceiver广播

一:BroadCastReceiver介绍:广播作用以及机制项目中广播使用广播生命周期 二:如何实现广播静态广播注册动态广播注册 三:广播的分类无序广播发送(标准广播):sendBroadcast()有序广播发送:sendOrderBroadcast()粘性广播:sendStickyBroadcast() 四:系统广播

一:BroadCastReceiver介绍:

广播作用以及机制

其实BroadcastReceiver就是应用程序间的全局大喇叭,即通信的一个手段, 系统自己在很多时候都会发送广播,比如电量低或者充足,刚启动完,插入耳机,输入法改变等, 发生这些时间,系统都会发送广播,这个叫系统广播,每个APP都会收到,如果你想让你的应用在接收到 这个广播的时候做一些操作,比如:系统开机后,偷偷后台跑服务哈哈,这个时候你只需要为你的应用 注册一个用于监视开机的BroadcastReceiver,当接收到开机广播就做写偷偷摸摸的勾当~ 当然我们也可以自己发广播,比如:接到服务端推送信息,用户在别处登录,然后应该强制用户下线回到 登陆界面,并提示在别处登录当然,这些等下都会写一个简单的示例帮大家了解广播给我们带来的好处.

项目中广播使用

BroadCastReceiver广播接受者,安卓四大组件之一

广播三要素: (1)广播发送者 : 发送广播 (2)广播接收者(调频): 用于接收广播 (3)要处理的事情 :处理广播的相关信息, Intent有图对象 广播的使用场景: (1)同一APP下多个组件之间传递数据(Activity/Fragment/Service之间传递数据) (2)2个APP之间传递数据 技能get点: (1)自定义广播接受者 (2)使用广播接受者进行电话拦截和短信拦截和系统电量的变化

广播生命周期

静态注册和动态注册的区别:假如说Activity是接受者: 动态注册: (1)广播会跟Activity的生命周期的结束而结束; (2)自由的控制注册和取消,有很大的灵活性 静态注册: (1)广播不会跟随Activity的生命周期的结束而结束,一直存在,即使应用程序关闭,也会被唤醒接受广播 (2)全局的广播

二:如何实现广播

1,先创建一个广播,写一个类继承BroadcastReceiver即可

package com.example.day12; public class MyReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { } }

静态广播注册

在清单文件中注册广播就是静态的

<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.day12receiver"> <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true" android:theme="@style/AppTheme"> <activity android:name=".MainActivity"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <receiver android:name=".MyReceiver" android:enabled="true" android:exported="true"> <intent-filter> <action android:name="com.tiger"/> <action android:name="com.tigerkin"/> </intent-filter> </receiver> </application> </manifest>

MyReceiver代码

public class MyReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { String action = intent.getAction(); if (action.equals(BroadcasConst.ACTION_FEGN)){ Bundle bundle = intent.getExtras(); String message = bundle.getString("message"); Toast.makeText(context, message, Toast.LENGTH_SHORT).show(); }else if (action.equals(BroadcasConst.ACTION_FENG)){ Toast.makeText(context, "有序广播", Toast.LENGTH_SHORT).show(); } } }

常量类

//常量类 public class BroadcasConst { public static final String ACTION_FENG = "com.tiger"; public static final String ACTION_FEGN = "com.tigerkin"; }

Activity代码

/* 静态广播注册: (1)广播不会跟随Activity的生命周期的结束而结束,一直存在,即使应用程序关闭,也会被唤醒接受广播 (2)全局的广播 */ public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } public void send(View view) { int id = view.getId(); switch (id){ case R.id.ordered: Intent intent = new Intent(); intent.setAction(BroadcasConst.ACTION_FENG); //第一个是参数,第二个是权限名 sendOrderedBroadcast(intent,null); break; case R.id.unordered: Intent intent1 = new Intent(); intent1.setAction(BroadcasConst.ACTION_FEGN); Bundle bundle = new Bundle(); bundle.putString("message","人间值得"); intent1.putExtras(bundle); sendBroadcast(intent1); break; } } }

动态广播注册

动态不用在清单中设置 MyReceiver和常量类如上 Activity代码

/* 动态广播注册: (1)广播会跟Activity的生命周期的结束而结束; (2)自由的控制注册和取消,有很大的灵活性 */ public class MainActivity extends AppCompatActivity { private MyReceiver myReceiver; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //创建一个广播 myReceiver = new MyReceiver(); //添加广播过滤器 IntentFilter intentFilter = new IntentFilter(); //添加action intentFilter.addAction(BroadcastConst.ACTION_TIGER); //注册 registerReceiver(myReceiver,intentFilter); } public void sendId(View view) { int id = view.getId(); switch (id){ case R.id.ordered_id: //有序可以中断广播 Intent intent = new Intent(); intent.setAction(BroadcastConst.ACTION_TIGER); sendOrderedBroadcast(intent,null); break; case R.id.unordered_id: Intent intent1 = new Intent(); intent1.setAction(BroadcastConst.ACTION_TIGERKIN); intent1.putExtra("message","人间的四月天"); sendBroadcast(intent1); break; } } @Override protected void onDestroy() { super.onDestroy(); //注销广播 unregisterReceiver(myReceiver); } }

动态注册广播可以中断 第一个广播

public class MyReceiver extends BroadcastReceiver { private static final String TAG = "MyReceiver"; @Override public void onReceive(Context context, Intent intent) { //TODO 1:获取action String action = intent.getAction(); if(BroadcastConst.ACTION.equals(action)){ // Bundle extras = intent.getExtras(); // int msg = extras.getInt("msg"); Log.i(TAG, "onReceive: "); } } }

第二个广播

public class MyReceiver2 extends BroadcastReceiver { private static final String TAG = "MyReceiver2"; @Override public void onReceive(Context context, Intent intent) { String action = intent.getAction(); if(action.equals("com.feng.broad")){ Log.i(TAG, "onReceive: +++"); //判断是不是有序广播 if(isOrderedBroadcast()){ //中断一个广播 abortBroadcast(); } } } }

三:广播的分类

无序广播发送(标准广播):sendBroadcast()

Intent intent = new Intent(); intent.setAction("com.feng.broad"); Bundle bundle = new Bundle(); bundle.putInt("msg",123); intent.putExtras(bundle); sendBroadcast(intent);

有序广播发送:sendOrderBroadcast()

Intent intent1 = new Intent(); intent1.setAction("com.feng.broad"); //第一个参数是intent 二是权限名. sendOrderedBroadcast(intent1,null);

粘性广播:sendStickyBroadcast()

将之前广播发送方发送的消息存储起来,普通广播就不能接受之前发过的消息

四:系统广播

安卓常用系统广播 https://blog.csdn.net/cc_want/article/details/82344899 接收系统广播

系统在某些时候会发送相应的系统广播,下面我们就来让我们的APP接收系统广播, 接收之前,还需要为我们的APP注册广播接收器哦!而注册的方法又分为以下两种:动态与静态! 静态接收系统锁屏广播

public class ScreenReceiver extends BroadcastReceiver { private static final String TAG = "ScreenReceiver"; @Override public void onReceive(Context context, Intent intent) { Log.i(TAG, "onReceive: ------"); String action = intent.getAction(); if(Intent.ACTION_SCREEN_ON.equals(action)){ //不能用 Log.i(TAG, "onReceive: 亮了"); }else if(Intent.ACTION_SCREEN_OFF.equals(action)){//不能用 Log.i(TAG, "onReceive: 暗了"); }else if (Intent.ACTION_USER_PRESENT.equals(action)){ Log.i(TAG, "onReceive: 唤醒了"); }else if(Intent.ACTION_AIRPLANE_MODE_CHANGED.equals(action)){ Log.i(TAG, "onReceive: 飞行了"); } } }

清单文件中注册

<receiver android:name=".ScreenReceiver" android:enabled="true" android:exported="true"> <intent-filter android:priority="1000"> <action android:name="android.intent.action.SCREEN_ON"></action> <action android:name="android.intent.action.SCREEN_OFF"></action> <action android:name="android.intent.action.USER_PRESENT"></action> <action android:name="android.intent.action.AIRPLANE_MODE"></action> </intent-filter> </receiver>

使用注意事项 不要在广播里添加过多逻辑或者进行任何耗时操作,因为在广播中是不允许开辟线程的, 当onReceiver( )方法运行较长时间(超过10秒)还没有结束的话,那么程序会报错(ANR), 广播更多的时候扮演的是一个打开其他组件的角色,比如启动Service,Notification提示, Activity等! 自定义广播


最新回复(0)