Android應(yīng)用圖標(biāo)在狀態(tài)欄上顯示實(shí)現(xiàn)原理
在研究《Android類似360,QQ管家那樣的懸浮窗》突發(fā)奇想,想把應(yīng)用的圖標(biāo)也顯示到狀態(tài)欄上,類似手機(jī)QQ,而有消息來(lái)時(shí)改變狀態(tài)欄上的圖標(biāo)顯示。
二、原理
其實(shí)很研究完后,才發(fā)現(xiàn),很簡(jiǎn)單:
2.1 顯示圖標(biāo)在狀態(tài)欄上
NotificationManager nm = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
Notification n = new Notification(
resId, "Floats Start!", System.currentTimeMillis());
// 將此通知放到通知欄的"Ongoing"即"正在運(yùn)行"組中
n.flags |= Notification.FLAG_ONGOING_EVENT;
// 表明在點(diǎn)擊了通知欄中的"清除通知"后,此通知不清除,
// 經(jīng)常與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 修改圖標(biāo)的顯示
不用cancel這個(gè)通知,只需傳入不同的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();
}
/*
* 顯示應(yīng)用主界面時(shí),去除懸浮層
* 修改狀態(tài)欄上的圖標(biāo)
*/
@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+系統(tǒng)中,在下拉菜單下面,而在2.3中,在上拉菜單之上)
*/
mWindowMgrParams.type = 2003;
mWindowMgrParams.format = 1;
/*
* 代碼實(shí)際是wmParams.flags |= FLAG_NOT_FOCUSABLE;
* 40的由來(lái)是wmParams的默認(rèn)屬性(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;
/*
* 如果沒(méi)有從狀態(tài)欄中刪除ICON,且繼續(xù)調(diào)用addIconToStatusbar,
* 則不會(huì)有任何變化。除了:
* 如果,將notification中的resId設(shè)置不同的圖標(biāo),則會(huì)顯示不同
* 的圖標(biāo)
*/
private void addIconToStatusbar(int resId){
NotificationManager nm = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
Notification n = new Notification(
resId, "Floats Start!", System.currentTimeMillis());
// 將此通知放到通知欄的"Ongoing"即"正在運(yùn)行"組中
n.flags |= Notification.FLAG_ONGOING_EVENT;
// 表明在點(diǎn)擊了通知欄中的"清除通知"后,此通知不清除,
// 經(jīng)常與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);
}
}
以上就是源碼,當(dāng)應(yīng)用的焦點(diǎn)變化時(shí),狀態(tài)欄上的圖片也會(huì)跟著變化。
源碼下載地址:http://download.csdn.net/detail/qingye_love/5506825
相關(guān)文章
Android用StaticLayout實(shí)現(xiàn)文字轉(zhuǎn)化為圖片效果(類似長(zhǎng)微博發(fā)送)
這篇文章主要給大家介紹了關(guān)于Android利用StaticLayout實(shí)現(xiàn)文字轉(zhuǎn)化為圖片效果,實(shí)現(xiàn)的效果類似我們常見(jiàn)的長(zhǎng)微博效果,文中給出了詳細(xì)的示例代碼供大家參考學(xué)習(xí),需要的朋友們下面來(lái)一起看看吧。2017-08-08Android ReboundScrollView仿IOS拖拽回彈效果
這篇文章主要為大家詳細(xì)介紹了Android ReboundScrollView仿IOS拖拽回彈效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-11-11在Android中動(dòng)態(tài)添加Panel框架的實(shí)現(xiàn)代碼
項(xiàng)目經(jīng)常會(huì)有這種需求,就是想動(dòng)態(tài)的在某個(gè)界面上添加一個(gè)Panel。比如,有一個(gè)按鈕,點(diǎn)擊后會(huì)顯示下拉菜單式的界面。這種需求,就屬于動(dòng)態(tài)添加一個(gè)Panel。需求多了,就要研究是否可以抽象出通用的框架代碼,以方便開(kāi)發(fā),所以就有了以下內(nèi)容2013-05-05Flutter實(shí)現(xiàn)編寫(xiě)富文本Text的示例代碼
這篇文章主要為大家詳細(xì)介紹了如何通過(guò)Flutter實(shí)現(xiàn)編寫(xiě)富文本Text,文中的示例代碼講解詳細(xì),具有一定的借鑒價(jià)值,需要的可以參考一下2022-11-11android textview設(shè)置字體的行距和字間距
這篇文章主要介紹了android textview設(shè)置字體的行距和字間距的方法,非常簡(jiǎn)單實(shí)用,有需要的小伙伴可以參考下2016-05-05解決Android studio xml界面無(wú)法預(yù)覽問(wèn)題
這篇文章主要介紹了解決Android studio xml界面無(wú)法預(yù)覽問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-03-03Android SharedPreferences四種操作模式使用詳解
這篇文章主要介紹了Android SharedPreferences四種操作模式使用詳解的相關(guān)資料,這里介紹了獲取Android SharedPreferences的兩種方法及比較,和操作模式的介紹,需要的朋友可以參考下2017-07-07Android TextView 去掉自適應(yīng)默認(rèn)的fontpadding的實(shí)現(xiàn)方法
這篇文章主要介紹了Android TextView 去掉自適應(yīng)默認(rèn)的fontpadding的實(shí)現(xiàn)方法的相關(guān)資料,希望通過(guò)本文大家能夠掌握這部分內(nèi)容,需要的朋友可以參考下2017-09-09Android 自定義Dialog去除title導(dǎo)航欄的解決方法
今天小編就為大家分享一篇Android 自定義Dialog去除title導(dǎo)航欄的解決方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-07-07