Android(安卓)5.0状态栏通知图标的实现
16lz
2021-01-26
Android 5.0状态栏通知图标的实现
我之前的博客文章中有一片是介绍了关于Android5.0 下拉通知栏快捷开关的添加,文章牵扯到一个知识点就是Android 5.0状态栏通知图标的实现。那么今天就简单的介绍一下这个功能是如何实现的。
想要了解我之前相关的博客可以打开:
Android5.0 下拉通知栏快捷开关的添加(必看)
Android 5.0状态栏通知图标的实现,说简单点也就是利用广播,然后在SystemUi相关的地方处理就行了。我们这里暂且就之前的博客内容为例:
首先:我们需要在打开和关闭FM的地方发送广播
if(on) {//FM打开 Log.i("lyj_redio","power is on"); Settings.System.putInt(MainActivity.this.getContentResolver(), Settings.System.FM_SYSTEMUI, 0); mFMTxService.powerOn(); //M:lyj 发送广播 Intent intent = new Intent("fm_send"); intent.putExtra("state", true); MainActivity.this.sendBroadcast(intent); }else {//FM关闭 Settings.System.putInt(MainActivity.this.getContentResolver(), Settings.System.FM_SYSTEMUI, 1); mFMTxService.powerOff(); Intent intent = new Intent("fm_send"); intent.putExtra("state", false); MainActivity.this.sendBroadcast(intent); }
发送广播的地方处理了,接下来就是在SystemUi中接收广播了。
打开如下路径:frameworks/base/packages/SystemUI/src/com/android/systemui/statusbar/phone/PhoneStatusBarPolicy.java
在这个java文件里首先定义一个String值
private static final String SLOT_FM_SEND = "fm_send";
然后注册,监听广播还有实现图片显示:
//注册广播 private BroadcastReceiver mIntentReceiver = new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { String action = intent.getAction(); .... if(action.equals(SLOT_FM_SEND)) { updateFmSend(intent); } .... } };//状态栏图片的处理 private final void updateFmSend(Intent intent) { boolean state = intent.getBooleanExtra("state", true); Log.i("lyj_system", "FMstate = "+state); if (state) {//开关打开的情况下 mService.setIcon(SLOT_FM_SEND, R.drawable.stat_sys_headset_with_mic, 0, null);//打开FM图标 mService.setIconVisibility(SLOT_FM_SEND, true); }else { mService.setIconVisibility(SLOT_FM_SEND, false); } }//监听广播 public PhoneStatusBarPolicy(Context context, CastController cast, HotspotController hotspot) { mContext = context; mCast = cast; mHotspot = hotspot; mService = (StatusBarManager)context.getSystemService(Context.STATUS_BAR_SERVICE); // listen for broadcasts IntentFilter filter = new IntentFilter(); //M:lyj add filter.addAction(SLOT_FM_SEND); .... }
这里看一下updateFmSend 这个方法,主要就是判断广播过来的State的值。代码很简单,状态值为true就打开图标,否则关闭图标。
OK!结合之前的博客文章看,相信会对你有所帮助。
更多相关文章
- react-native-vector-icons使用
- Android(安卓)在一个APP内打开另一个APP
- Android百度地图点聚合聚合点的样式修改-自定义聚合点样式
- Android使用矢量图(SVG, VectorDrawable)实践篇
- 《Google!Android2手机应用程序设计入门》笔记(1)
- Android(安卓)四大组件你都知道吗
- 实战Android读取USB数据到手机自带存储中
- 第十五章 Android的广播机制和BroadCast Receiver
- xposed hook框架的使用(一)