Notification介绍

Notification是显示在手机状态栏的信息——手机状态栏位于手机屏幕的最上方,那里一般显示手机当前的网络状态、电池状态、时间等,Notification所代表的是一种具有全局效果的通知,程序一般通过NotificationManager服务来发送Notificaton。下面介绍如何实现:

(一)创建notification,与其相关的两个类:

(1)Notification(icon,tickerText, when)

  • Icon是图标,一般放在drawable

  •  tickerText是图标后面的内容,如:“Missed call from5554”

  • when显示图标什么时候创建的,如可以采用:System.currentTimeMillis()

    创建了notification后紧接着调用notification.setLatestEventInfo设置事件信息

    notification.setLatestEventInfo(context,contentTitle,contentText,contentIntent)

  •   context:一般是this指向当前对象
  •  contentTitle:相当于标题,即图中蓝色笔画的内容即Missedcalls
  • contentText:消息,即里面的内容,如图中的2missed calls

  • contentIntent是PendingIntent对象,所以下面要创建PendingIntent的对象。

    创建PendingIntent对象:

    PendingIntentcontentIntent=PendingIntent.getActivity(context,requestode,intent,flags)

  • context:this
  •  requestode:0
  •  intent:创建一个intent对象,即当用户点击该notification时,运行哪一个Activity
  • flags:FLAG_CANCEL_CURRENT如果PendingIntent已经存在,在生成一个新的之前,当前的被取消

           FLAG_NO_CREATE如果不存在PendingIntent,仅仅返回空而不会创建PendingIntent对象

          FLAG_UPDATE_CURRENT如果PendingIntent已经存在,先保持,可以被新的代替

    这里创建Intent对象就不多说了,比较简单

    (2)NotificationManager:将创建好的Notification在状态栏显示出来

    这个对象创建采用getSystemService(NOTIFICATION_SERVICE)实现的,然后采用

     manager.notify(id,notification)实现notification的发送。其中id可以定义为静态量,主要是用于标识用的,比如后面的notification的取消可以通过id寻找要取消的notification

    下面介绍下程序的实现:

    (1)创建一个类是继承IntentService的

    package com.syz.statusbar;
    
    import android.app.IntentService;
    import android.app.Notification;
    import android.app.NotificationManager;
    import android.app.PendingIntent;
    import android.content.Intent;
    import android.util.Log;
    
    public class StatusService extends IntentService {
    
    	private static final String TAG = "StatusService";
    	public StatusService() {
    		super("StatusService");
    		// TODO Auto-generated constructor stub
    	}
    	@Override
    	protected void onHandleIntent(Intent intent) {
    		Log.i(TAG, "onHandleIntent:开始下载....");
    		getnotification(false);                //主要用于模拟下载,开始传递是false,实现显                                                //示“下载中...”10秒后再传递true实现显示“下载完成”
    		try {
    			Thread.sleep(10000);
    			getnotification(true);
    		} catch (InterruptedException e) {
    			// TODO Auto-generated catch block
    			e.printStackTrace();
    		}
    	}
    	private void getnotification(boolean f) {
    Notification notification =new Notification(R.drawable.ic_launcher, "开始下载", System.currentTimeMillis());
    Intent intent=new Intent(this, MainActivity.class);
    PendingIntent contentIntent=PendingIntent.getActivity(this, 0, intent,PendingIntent.FLAG_UPDATE_CURRENT);
    if(!f)                                     
    {
    notification.setLatestEventInfo(this, "下载", "下载中。。。。", contentIntent);
    }
    else
    {
    notification.setLatestEventInfo(this, "结束", "下载完成", contentIntent);
    }
    NotificationManager manager=(NotificationManager) getSystemService(NOTIFICATION_SERVICE);
    		manager.notify(R.menu.activity_main, notification);
    	}
    
    }

    (2)创建一个Activity

    package com.syz.statusbar;
    
    import android.os.Bundle;
    import android.app.Activity;
    import android.app.NotificationManager;
    import android.content.Intent;
    import android.view.Menu;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.widget.Button;
    
    public class MainActivity extends Activity {
    private Button StartService=null;
    	@Override
    	protected void onCreate(Bundle savedInstanceState) {
    		super.onCreate(savedInstanceState);
    		setContentView(R.layout.activity_main);
    		StartService=(Button)findViewById(R.id.StartService);
    		StartService.setOnClickListener(listener);
    	}
    	private OnClickListener listener=new OnClickListener() {
    		
    		@Override
    		public void onClick(View v) {
    			Intent intent=new Intent();
    			intent.setClass(MainActivity.this, StatusService.class);
    			startService(intent);
    		}
    	};
    	@Override
    	protected void onStart() {
    		super.onStart();
    		NotificationManager manager=(NotificationManager)getSystemService(NOTIFICATION_SERVICE);//用于当点击notification时,调用指定的Activity
    		manager.cancel(R.menu.activity_main);                                             //同时取消notification
    }
    
    }
    
    
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值