麻豆小视频在线观看_中文黄色一级片_久久久成人精品_成片免费观看视频大全_午夜精品久久久久久久99热浪潮_成人一区二区三区四区

首頁 > 系統 > Android > 正文

Android應用圖標在狀態欄上顯示實現原理

2020-04-11 12:16:10
字體:
來源:轉載
供稿:網友
一、前方
在研究《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
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 国产免费一区二区三区网站免费 | 精品久久久久久久久久久下田 | 国产一级αv片免费观看 | 我爱我色成人网 | 少妇的肉体2无删减版 | 美女福利视频国产 | 黄片一级毛片 | 久久最新免费视频 | 九九热九九热 | 羞羞的视频在线观看 | 蜜桃网站在线 | a免费视频 | 精品少妇v888av| 97青青草视频 | 欧美日韩专区国产精品 | av影院在线播放 | 又黄又爽免费无遮挡在线观看 | 亚洲男人的天堂在线视频 | 黄色羞羞视频在线观看 | 高清av在线 | 久久久免费观看完整版 | 91成人在线免费 | 黄色片网站在线免费观看 | 国产一及毛片 | 神马视频我不卡 | 羞羞视频2023 | 免费观看一区二区三区视频 | 免费国产自久久久久三四区久久 | 久久精品亚洲一区二区 | 操你逼 | 色综合久久久久综合99 | 久久亚洲国产精品 | 91情侣在线偷精品国产 | 成人做爰高潮片免费视频韩国 | 一级免费大片 | 久久国产精品久久久久久电车 | 欧美××××黑人××性爽 | 欧洲精品久久久 | 97青青草视频 | 黄色网页在线观看 | 亚洲小视频网站 |