现在的位置: 首页 > 综合 > 正文

消息篇一:Notification发送与取消

2018年02月08日 ⁄ 综合 ⁄ 共 2328字 ⁄ 字号 评论关闭
本文着重解决3个问题:
1 notification的推送
2 notification的取消
3 在系统下拉框中点击notification后,删除该notification。
otification.flags = Notification.FLAG_AUTO_CANCEL;

package com.example.sendbroadcasepackage;

import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;

 
public class SendBroadCastActivity extends Activity 
{
	private  NotificationManager m_notificationManager;
	private final int CLICK_SEND = 1;
	private final int CLICK_CLEAR = 2;
	
	private final int ID_NOTIFICATION1 = 1;
	
	private Button m_btSend,m_btClear;
	
	
    @Override
    public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_send_broad_cast);
        BindButtonListenerWithId(m_btSend,R.id.id_sendBroadCastButton,CLICK_SEND);
        BindButtonListenerWithId(m_btClear,R.id.id_clearBroadCastButton,CLICK_CLEAR);

    }
    
	
	private void BindButtonListenerWithId(Button button,int nID,int nTagID)
	{
		button = (Button)findViewById(nID);
		button.setOnClickListener(new ClickButtonListener());
		button.setTag(nTagID);
	}
	
	private final class ClickButtonListener implements View.OnClickListener
	{
		@Override
		public void onClick(View view)
		{
	        switch((Integer) view.getTag())
	        {
	        case CLICK_SEND:
	        	showNotification();
	        	break;
	        case CLICK_CLEAR:
	        	clearNotification();
	        	break;
	        }
		}
	}
    
    
	private void showNotification()
	{
		m_notificationManager = (NotificationManager)this.getSystemService(android.content.Context.NOTIFICATION_SERVICE);   
        Notification notification =new Notification(); 
        notification.icon = R.drawable.ic_launcher;                        
        notification.tickerText = "督导系统";
        notification.defaults = Notification.DEFAULT_SOUND;        //播放默认音乐
//        notification.flags = Notification.FLAG_AUTO_CANCEL;
        
        Intent notificationIntent = new Intent(SendBroadCastActivity.this, SendBroadCastActivity.class);	// 点击该通知后要跳转的Activity   
        PendingIntent contentItent = PendingIntent.getActivity(SendBroadCastActivity.this, 0, notificationIntent, 0);   

        notification.setLatestEventInfo(SendBroadCastActivity.this, "督导系统标题", "督导系统内容", contentItent);
        
//        m_notificationManager.notify(ID_NOTIFICATION1, notification);   
        m_notificationManager.notify("asdfasdf1", ID_NOTIFICATION1, notification);
    }
    
    //删除通知    
    private void clearNotification()
    { 
        Log.w("clearNotification", "督导系统");
        if(null != m_notificationManager)
        {
	        m_notificationManager.cancel("asdfasdf1",ID_NOTIFICATION1);
	        m_notificationManager = null;
        }
    }
}

抱歉!评论已关闭.