Android 狀態列通知

來源:互聯網
上載者:User

對於android開發人員來說,總是希望他們開發的程式可以被使用者經常使用,所以最希望的就是在使用者可得見的地方直接進入,呵呵。。。狀態列通知便是這樣的地方,像QQ或是360或是其他IM軟體。

那麼如何?狀態列通知呢?

簡單的一個方法搞定:

    //訊息管理器   
    private  NotificationManager n_NotificationManager;   
    private   PendingIntent   n_PendingIntent;   
    //通知對象   
    private  Notification   n_Notification;
    
    public void showNotification(int dwrid)
    {
        //初始化訊息管理器     
        n_NotificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);   
           
          
        //設定點擊通知時顯示內容的介面   
           n_PendingIntent = PendingIntent.getActivity(ICityActivity.this, 0, getIntent(), 0);   
           
           
        //通知對象   
        n_Notification = new Notification();  
        
           
        //設定通知在狀態列顯示的表徵圖   
        n_Notification.icon = dwrid;   
        
        
        //當我們點擊通知時顯示的內容   
        n_Notification.tickerText = "阿基米正在啟動宇宙級裝備,請稍後.....";   
        
        
        //通知時發出預設的聲音   
        n_Notification.defaults = Notification.DEFAULT_SOUND;   
        
        
        //設定通知顯示的參數   
        n_Notification.setLatestEventInfo(ICityActivity.this, "阿基米", "阿基米,給您帶來無限的快樂", n_PendingIntent);   
        
        
        //可以理解為執行這個通知   
        n_NotificationManager.notify(0, n_Notification);
        
    }

在Activity啟動時調用它,並給一個表徵圖的id

記住在程式退出時:

    protected void onDestroy() {     

 
        //清除它
        n_NotificationManager.cancelAll();
        super.onDestroy();     

 
    }

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.