android添加通知到頂部工作列

來源:互聯網
上載者:User

[java]
public class NotificationtestActivity extends Activity { 
    private static final int ID = 1213; 
    private static final String KEY_COUNT="keyCount"; 
    private int count; 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
        super.onCreate(savedInstanceState); 
        setContentView(R.layout.main); 
        Intent intent=this.getIntent(); 
        count=intent.getIntExtra(KEY_COUNT,0);         
        
        this.setTitle("這是第"+Integer.toString(count)+"個"); 
         
        Button btn=(Button) this.findViewById(R.id.button1); 
        btn.setOnClickListener(new View.OnClickListener() { 
             
            @Override 
            public void onClick(View v) { 
                AddNotification(); 
                NotificationtestActivity.this.finish(); 
            } 
        }); 
    } 
    /**
    * 添加頂部通知
    * @author liuzhao
    */ 
    public void AddNotification(){ 
        count++; 
        //添加通知到頂部工作列 
        //獲得NotificationManager執行個體 
        String service = NOTIFICATION_SERVICE; 
        NotificationManager nm = (NotificationManager)getSystemService(service); 
        //執行個體化Notification 
        Notification n = new Notification(); 
        //設定顯示表徵圖 
        int icon = R.drawable.icon; 
        //設定提示資訊 
        String tickerText ="我的程式"; 
        //顯示時間 
        long when = System.currentTimeMillis(); 
          
        n.icon = icon; 
        n.tickerText = tickerText; 
        n.when = when; 
        //顯示在“進行中中” 
        //  n.flags = Notification.FLAG_ONGOING_EVENT; 
        n.flags|=Notification.FLAG_AUTO_CANCEL; //自動終止 
        //執行個體化Intent 
        Intent it = new Intent(this,NotificationtestActivity.class); 
        it.putExtra(KEY_COUNT, count); 
        /*********************
         *獲得PendingIntent  
         *FLAG_CANCEL_CURRENT:
         *      如果當前系統中已經存在一個相同的PendingIntent對象,
         *      那麼就將先將已有的PendingIntent取消,然後重建一個PendingIntent對象。 
         *FLAG_NO_CREATE:
         *      如果當前系統中不存在相同的PendingIntent對象,
         *      系統將不會建立該PendingIntent對象而是直接返回null。 
         *FLAG_ONE_SHOT:
         *      該PendingIntent只作用一次,
         *      如果該PendingIntent對象已經觸發過一次,
         *      那麼下次再擷取該PendingIntent並且再觸發時,
         *      系統將會返回一個SendIntentException,在使用這個標誌的時候一定要注意哦。 
         *FLAG_UPDATE_CURRENT:
         *      如果系統中已存在該PendingIntent對象,
         *      那麼系統將保留該PendingIntent對象,
         *      但是會使用新的Intent來更新之前PendingIntent中的Intent對象資料,
         *      例如更新Intent中的Extras。這個非常有用,
         *      例如之前提到的,我們需要在每次更新之後更新Intent中的Extras資料,
         *      達到在不同時機傳遞給MainActivity不同的參數,實現不同的效果。 
         *********************/ 
          
        PendingIntent pi = PendingIntent.getActivity(this, 0, it, PendingIntent.FLAG_UPDATE_CURRENT); 
         
        //設定事件資訊,顯示在拉開的裡面 
        n.setLatestEventInfo(NotificationtestActivity.this,"我的軟體"+Integer.toString(count), "我的軟體正在運行……", pi); 
      www.2cto.com
        //發出通知 
        nm.notify(ID,n); 
    } 
} 

聯繫我們

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