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!结合之前的博客文章看,相信会对你有所帮助。

更多相关文章

  1. react-native-vector-icons使用
  2. Android(安卓)在一个APP内打开另一个APP
  3. Android百度地图点聚合聚合点的样式修改-自定义聚合点样式
  4. Android使用矢量图(SVG, VectorDrawable)实践篇
  5. 《Google!Android2手机应用程序设计入门》笔记(1)
  6. Android(安卓)四大组件你都知道吗
  7. 实战Android读取USB数据到手机自带存储中
  8. 第十五章 Android的广播机制和BroadCast Receiver
  9. xposed hook框架的使用(一)

随机推荐

  1. 0326作业-仿京东布局
  2. 如何修改Safari浏览器网页字体大小?
  3. 怎样才能选择一个好的运维服务商?
  4. 选择模型真用对了吗? 扒一扒75篇顶级外刊
  5. 练习2-14 求奇数分之一序列前N项和 (15分
  6. Kubernetes 部署Redis主从服务(StatefulS
  7. 函数-对象、嵌套、名称空间与作用域
  8. EGG NETWORK阿凡提超级公链 EFT流量通证
  9. Frost & Sullivan:“Xilinx引领自动驾驶技
  10. 中国唯一入选 Forrester 领导者象限,阿里