Android应用图标在状态栏上显示,以及显示不同的图标

二、原理

其实很研究完后,才发现,很简单:

2.1 显示图标在状态栏上

[java]
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); 

  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,再通知即可。

[java]
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); 
    } 

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);
 }
}
以上就是源码,当应用的焦点变化时,状态栏上的图片也会跟着变化。


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值