Android应用图标在状态栏上显示实现原理
程序员文章站
2023-11-12 10:26:46
一、前方 在研究《android类似360,qq管家那样的悬浮窗》突发奇想,想把应用的图标也显示到状态栏上,类似手机qq,而有消息来时改变状态栏上的图标显示。 二、原理 其...
一、前方
在研究《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
在研究《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