Android应用图标在状态栏上显示实现原理

 更新时间:2013年06月03日 17:18:25   作者:  
Android应用图标在状态栏上显示,以及显示不同的图标,其实很研究完后,才发现,很简单,具体实现如下,感兴趣的朋友可以参考下哈
一、前方
在研究《Android类似360,QQ管家那样的悬浮窗》突发奇想,想把应用的图标也显示到状态栏上,类似手机QQ,而有消息来时改变状态栏上的图标显示。

二、原理
其实很研究完后,才发现,很简单:
2.1 显示图标在状态栏上
复制代码 代码如下:

NotificationManager nm = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
Notification n = new Notification(
resId, "Floats Start!", System.currentTimeMillis());

// 将此通知放到通知栏的"Ongoing"即"正在运行"组中
n.flags |= Notification.FLAG_ONGOING_EVENT;
// 表明在点击了通知栏中的"清除通知"后,此通知不清除,
// 经常与FLAG_ONGOING_EVENT一起使用
n.flags |= Notification.FLAG_NO_CLEAR;

PendingIntent pi = PendingIntent.getActivity(this, 0, getIntent(), 0);
n.contentIntent = pi;
n.setLatestEventInfo(this, "FloatsWindow", "start!", pi);
nm.notify(NOTIFICATION_ID_ICON, n);

2.2 修改图标的显示
不用cancel这个通知,只需传入不同的resId,再通知即可。
复制代码 代码如下:

package com.chris.floats.window;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.WindowManager;
import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
public class MainActivity extends Activity {
private static WindowManager mWindowMgr = null;
private WindowManager.LayoutParams mWindowMgrParams = null;
private static FloatsWindowView mFloatsWindowView = null;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}

@Override
protected void onStop() {
super.onStop();
deleteIconToStatusbar();
}
/*
* 显示应用主界面时,去除悬浮层
* 修改状态栏上的图标
*/
@Override
public void onWindowFocusChanged(boolean hasFocus) {
if(hasFocus){
if(mFloatsWindowView != null){
mWindowMgr.removeView(mFloatsWindowView);
mFloatsWindowView = null;
}
addIconToStatusbar(R.drawable.a0);
}else{
getWindowLayout();
addIconToStatusbar(R.drawable.ic_launcher);
}
}
private void initParams(){
DisplayMetrics dm = getResources().getDisplayMetrics();
mWindowMgrParams.x = dm.widthPixels - 136;
mWindowMgrParams.y = 300;
mWindowMgrParams.width = 136;
mWindowMgrParams.height = 136;
}
private void getWindowLayout(){
if(mFloatsWindowView == null){
mWindowMgr = (WindowManager)getBaseContext().getSystemService(Context.WINDOW_SERVICE);
mWindowMgrParams = new WindowManager.LayoutParams();

/*
* 2003 在指悬浮在所有界面之上
* (4.0+系统中,在下拉菜单下面,而在2.3中,在上拉菜单之上)
*/
mWindowMgrParams.type = 2003;
mWindowMgrParams.format = 1;

/*
* 代码实际是wmParams.flags |= FLAG_NOT_FOCUSABLE;
* 40的由来是wmParams的默认属性(32)+ FLAG_NOT_FOCUSABLE(8)
*/
mWindowMgrParams.flags = 40;
mWindowMgrParams.gravity = Gravity.LEFT | Gravity.TOP;
initParams();

mFloatsWindowView = new FloatsWindowView(this);
mWindowMgr.addView(mFloatsWindowView, mWindowMgrParams);
}
}

private final static int NOTIFICATION_ID_ICON = 0x10000;
/*
* 如果没有从状态栏中删除ICON,且继续调用addIconToStatusbar,
* 则不会有任何变化。除了:
* 如果,将notification中的resId设置不同的图标,则会显示不同
* 的图标
*/
private void addIconToStatusbar(int resId){
NotificationManager nm = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
Notification n = new Notification(
resId, "Floats Start!", System.currentTimeMillis());

// 将此通知放到通知栏的"Ongoing"即"正在运行"组中
n.flags |= Notification.FLAG_ONGOING_EVENT;
// 表明在点击了通知栏中的"清除通知"后,此通知不清除,
// 经常与FLAG_ONGOING_EVENT一起使用
n.flags |= Notification.FLAG_NO_CLEAR;

PendingIntent pi = PendingIntent.getActivity(this, 0, getIntent(), 0);
n.contentIntent = pi;
n.setLatestEventInfo(this, "FloatsWindow", "start!", pi);
nm.notify(NOTIFICATION_ID_ICON, n);
}

private void deleteIconToStatusbar(){
NotificationManager nm = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
nm.cancel(NOTIFICATION_ID_ICON);
}
}

以上就是源码,当应用的焦点变化时,状态栏上的图片也会跟着变化。
源码下载地址:http://download.csdn.net/detail/qingye_love/5506825

相关文章

  • listview的上滑下滑监听,上下滑监听隐藏顶部选项栏的实例

    listview的上滑下滑监听,上下滑监听隐藏顶部选项栏的实例

    下面小编就为大家分享一篇listview的上滑下滑监听,上下滑监听隐藏顶部选项栏的实例,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2018-01-01
  • flutter实现磨砂玻璃效果实例详解

    flutter实现磨砂玻璃效果实例详解

    这篇文章主要为大家介绍了flutter实现磨砂玻璃效果实例详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2022-11-11
  • Android10填坑适配指南(实际经验代码)

    Android10填坑适配指南(实际经验代码)

    这篇文章主要介绍了Android10填坑适配指南(实际经验代码),文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2019-11-11
  • Android实现头像上传功能

    Android实现头像上传功能

    这篇文章主要为大家详细介绍了Android实现头像上传功能,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2018-12-12
  • Android NDK开发入门

    Android NDK开发入门

    本文主要对NDK产生的背景、使用NDK原因、NDK简介、NDK开发环境的搭建、如何运行NDK提供的事例demo等进行了详细的介绍。具有很好的参考价值,需要的朋友一起来看下吧
    2016-12-12
  • Android Flutter绘制有趣的 loading加载动画

    Android Flutter绘制有趣的 loading加载动画

    在网络速度较慢的场景,一个有趣的加载会提高用户的耐心和对 App 的好感。本篇我们利用Flutter 的 PathMetric来玩几个有趣的 loading 效果,感兴趣的可以动手尝试一下
    2022-07-07
  • Android开发之基本控件和四种布局方式详解

    Android开发之基本控件和四种布局方式详解

    这篇文章主要介绍了Android开发之基本控件和四种布局方式详解的相关资料,非常不错具有参考借鉴价值,需要的朋友可以参考下
    2016-06-06
  • android 使用虚拟机安装apk(图文教程)

    android 使用虚拟机安装apk(图文教程)

    android 使用虚拟机安装apk对一些新手朋友会很陌生,今天教大家使用使用虚拟机安装apk文件,步骤很详细,有需要的朋友可以参考下
    2012-12-12
  • 基于界面适配华为手机的虚拟按键的解决方法

    基于界面适配华为手机的虚拟按键的解决方法

    下面小编就为大家分享一篇基于界面适配华为手机的虚拟按键的解决方法,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2018-01-01
  • Android控件之CheckBox、RadioButton用法实例分析

    Android控件之CheckBox、RadioButton用法实例分析

    这篇文章主要介绍了Android控件之CheckBox、RadioButton用法,以实例形式较为详细的分析了CheckBox和RadioButton实现复选按钮及单选按钮功能的相关技巧,具有一定参考借鉴价值,需要的朋友可以参考下
    2015-09-09

最新评论