Android應用表徵圖在狀態列上顯示,以及顯示不同的表徵圖

來源:互聯網
上載者:User

二、原理

其實很研究完後,才發現,很簡單:

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);
 }
}
以上就是源碼,當應用的焦點變化時,狀態列上的圖片也會跟著變化。


 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.