今天分享一篇稍微带点技术的文章
纪念我攻克广播的艰辛之路
这里的广播,是指安卓四大组件——Receiver
刚开始接触广播,以为是FM,后来发现并不是哈哈哈
广播的内容不是很多,本文主要回顾两种注册方式在不同版本安卓系统上的兼容问题
1
静态注册
先介绍一下广播的静态注册:
案例:监听开机广播
第一步:监听
package com.example.myapplication;import android.content.BroadcastReceiver;import android.content.Context;import android.content.Intent;import android.util.Log;public class BroadcastTest_First extends BroadcastReceiver { public static final String TAG = "Boot_Receiver"; @Override public void onReceive(Context context, Intent intent) { String action = intent.getAction(); Log.d(TAG, "actions》》》》》》" + action); if (Intent.ACTION_BOOT_COMPLETED.equals(action)) { Toast.makeText(context, "开机完成", Toast.LENGTH_SHORT).show(); } }}
第二步 注册:
注册方式:在AndroidManifest里进行注册:
<receiver android:name=".BroadcastTest_First"> <intent-filter> <action android:name="android.intent.action.BOOT_COMPLETED" /> intent-filter>receiver>
<receiverandroid:name=".BroadcastTest_First">中的name是广播接收者
第三步 权限
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
这样就可以监听开机广播啦啦啦
2
动态注册
案例:监听电池电量变化
第一步:
//接收电量变化 IntentFilter intentFilter = new IntentFilter(); //设置频道 intentFilter.addAction(Intent.ACTION_BATTERY_CHANGED); BatteryLevelReceiver batteryLevelReceiver=new BatteryLevelReceiver(); //注册广播 this.registerReceiver(batteryLevelReceiver,intentFilter);
第二步:完成监听接收器类BatteryLevelReceiver:
private class BatteryLevelReceiver extends BroadcastReceiver{ @Override public void onReceive(Context context, Intent intent) { Log.d(TAG,intent.getIntExtra(BatteryManager.EXTRA_LEVEL, 0)+""); battery_level.setText("当前电量:"+intent.getIntExtra(BatteryManager.EXTRA_LEVEL, 0)); } }
这样就可以啦
附源代码:
Activity:
Battery.java源代码
package com.example.myapplication;import android.content.BroadcastReceiver;import android.content.Context;import android.content.Intent;import android.content.IntentFilter;import android.os.BatteryManager;import android.os.Bundle;import android.util.Log;import android.widget.TextView;import androidx.annotation.Nullable;import androidx.appcompat.app.AppCompatActivity;public class Battery extends AppCompatActivity { private static final String TAG ="Battery" ; private TextView battery_level; @Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.battery); battery_level = this.findViewById(R.id.battery_level); //接收电量变化 IntentFilter intentFilter = new IntentFilter(); //设置频道 intentFilter.addAction(Intent.ACTION_BATTERY_CHANGED); BatteryLevelReceiver batteryLevelReceiver=new BatteryLevelReceiver(); //注册广播 this.registerReceiver(batteryLevelReceiver,intentFilter); } /** * 创建广播接收者,继承自BroadcastReceiver */ private class BatteryLevelReceiver extends BroadcastReceiver{ @Override public void onReceive(Context context, Intent intent) { Log.d(TAG,intent.getIntExtra(BatteryManager.EXTRA_LEVEL, 0)+""); battery_level.setText("当前电量:"+intent.getIntExtra(BatteryManager.EXTRA_LEVEL, 0)); } }}
layout:
battery_level.xml源代码(比较简陋)
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <TextView android:id="@+id/battery_level" android:layout_width="match_parent" android:layout_height="wrap_content" android:textSize="40sp"/>LinearLayout>
屏幕截图:
3
踩坑:自定义广播
上面的案例 还在我掌握范围之内
自定义广播才是我坎坎坷坷绞尽脑汁的开始
案例描述:
发送广播 , 然后有三个类: BroadcastTest_First BroadcastTest_Second BroadcastTest_Third 进行监听接收。 answer 1: 最开始我的实现方法是: 第一步:发送广播Intent intent =new Intent();intent.setAction("com.example.myapplication");//发送广播sendBroadcast(intent);
第二步:写接收类:
BroadcastTest_First:
package com.example.myapplication;import android.content.BroadcastReceiver;import android.content.Context;import android.content.Intent;import android.util.Log;public class BroadcastTest_First extends BroadcastReceiver { private static final String TAG = "BroadcastTest_First"; private Object mPackage; @Override public void onReceive(Context context, Intent intent) { Log.d(TAG, "First我接受到了广播:::::::::"); }}
BroadcastTest_Second:
package com.example.myapplication;import android.content.BroadcastReceiver;import android.content.Context;import android.content.Intent;import android.util.Log;public class BroadcastTest_Second extends BroadcastReceiver { private static final String TAG = "BroadcastTest_Second"; private Object mPackage; @Override public void onReceive(Context context, Intent intent) { Log.d(TAG, "Second我接受到了广播:::::::::"); }}
BroadcastTest_Third:
package com.example.myapplication;import android.content.BroadcastReceiver;import android.content.Context;import android.content.Intent;import android.util.Log;public class BroadcastTest_Third extends BroadcastReceiver { private static final String TAG = "BroadcastTest_Third"; private Object mPackage; @Override public void onReceive(Context context, Intent intent) { Log.d(TAG, "Third我接受到了广播:::::::::"); }}
第三步:注册
<receiver android:name=".BroadcastTest_First"> <intent-filter> <action android:name="com.example.myapplication" /> <category android:name="android.intent.category.DEFAULT" /> intent-filter> receiver> <receiver android:name=".BroadcastTest_Second"> <intent-filter> <action android:name="com.example.myapplication" /> <category android:name="android.intent.category.DEFAULT" /> intent-filter> receiver> <receiver android:name=".BroadcastTest_Third"> <intent-filter> <action android:name="com.example.myapplication" /> <category android:name="android.intent.category.DEFAULT" /> intent-filter> receiver>
然后在Galaxy Nexus API 19模拟器上跑起来,成功了。
然后我试着在真机上跑,发现没有反应,于是就开始了填坑之旅。。。。。。
我谷歌了一下,发现也不止我自己遇到了这个问题,不过好像还挺好解决的。。。
因为安卓高版本对隐式广播进行了限制,因而无法实现
在搜索中,我发现了第一种解决方式:
将
intent.setAction("com.example.myapplication");
改为:
intent.setComponent(new ComponentName("com.example.myapplication", "com.example.myapplication.BroadCastTest_First"));
其中前一个参数为包名,后一个是接收器跑起来,成功了,但是
只有
BroadCastTest_First接收到了我尝试多加几个setComponent(),发现,这个方法只能调用一次,后面的调用会覆盖前面的,最终也没实现。所以还是失败了,于是我去逛了一圈CSDN只是逛了一圈也没找到解决方法。。。日常迷惑然后我去发帖子求助了所幸几分钟内“康师傅”回复了我的帖子哈哈哈哈也可能是我表述的问题,也可能是我知识储备的问题,感觉还是并没有太懂然后我又细细描述了我想实现的效果他告诉我既然静态注册受限为什么还要静态注册而不动态注册。是啊,我也想问为什么,瞬间怀疑自己是不是睡懵了,刚学过的东西就给忘记了。。。然后我尝试了一下该用动态注册:
answer2
:
//设置actionmIntent_first = new Intent("com.example.myapplication.first");mIntent_first.putExtra(Constance.ACTION_SEND_BROAD_CAST_CONTENT_KEY, "这是First广播");mIntent_second = new Intent("com.example.myapplication.second");mIntent_second.putExtra(Constance.ACTION_SEND_BROAD_CAST_CONTENT_KEY ,"这是Second广播");mIntent_third = new Intent("com.example.myapplication.third");mIntent_third.putExtra(Constance.ACTION_SEND_BROAD_CAST_CONTENT_KEY, "这是Third广播");/** *动态注册 */ IntentFilter intentFilter = new IntentFilter();//设置频道intentFilter.addAction(mIntent_first.getAction());intentFilter.addAction(mIntent_second.getAction());intentFilter.addAction(mIntent_third.getAction());mBroadcastTest_first = new BroadcastTest_First();mBroadcastTest_second = new BroadcastTest_Second();mBroadcastTest_third = new BroadcastTest_Third(); //注册广播this.registerReceiver(mBroadcastTest_first, intentFilter);this.registerReceiver(mBroadcastTest_second, intentFilter);this.registerReceiver(mBroadcastTest_third, intentFilter); //发送sendBroadcast(mIntent_first);sendBroadcast(mIntent_second);sendBroadcast(mIntent_third); Toast.makeText(this, "发送成功", Toast.LENGTH_SHORT).show();
对应的
public class BroadcastTest_First extends BroadcastReceiver { private static final String TAG = "BroadcastTest_First"; private Object mPackage; @Override public void onReceive(Context context, Intent intent) { if (intent.getAction().equals(Constance.ACTION_SEND_BROAD_CAST + ".first")) { Log.d(TAG, "First我接受到了广播:::::::::"+intent.getStringExtra(Constance.ACTION_SEND_BROAD_CAST_CONTENT_KEY)); } }}
public class BroadcastTest_Second extends BroadcastReceiver { private static final String TAG = "BroadcastTest_Second"; @Override public void onReceive(Context context, Intent intent) { if (intent.getAction().equals(Constance.ACTION_SEND_BROAD_CAST + ".second")) { Log.d(TAG, "Second我接受到了广播:::::::::" + intent.getStringExtra(Constance.ACTION_SEND_BROAD_CAST_CONTENT_KEY)); } }}
public class BroadcastTest_Third extends BroadcastReceiver { private static final String TAG = "BroadcastTest_Third"; @Override public void onReceive(Context context, Intent intent) { if (intent.getAction().equals(Constance.ACTION_SEND_BROAD_CAST + ".third")) { Log.d(TAG, "Third接受到了广播:::::::::"+intent.getStringExtra(Constance.ACTION_SEND_BROAD_CAST_CONTENT_KEY)); } }}
跑起来,成功啦!!!
瞬间开心
完
这是关于广播的笔记,虽然都是些低级问题
但困扰了我好久好久 。。。
不过开心的是问题已解决啦 哈哈哈哈
今天先到这里啦啦啦
时间有限,写的比较粗糙,下次改正嘻嘻嘻
第一次写这种文章,不喜勿喷,如有侵权请联系删除
特别感谢:“阳光沙滩”