android NotificationManager:通知栏

/***
 * 显示指定通知
 * 
 * @param strTitle
 *            通知内容
 * @param notification_id
 *            通知id
 */
public void showNotification(String strText, int notification_id) {
    // 得到NotificationManager
    Log.i("ANYCHAT", "showNotification");
    NotificationManager notificationManager = (NotificationManager) getSystemService(android.content.Context.NOTIFICATION_SERVICE);
    Notification notification = new Notification(R.drawable.ic_launcher, strText, System.currentTimeMillis());
    notification.flags |= Notification.FLAG_SHOW_LIGHTS;
    notification.flags |= Notification.FLAG_AUTO_CANCEL;  //通知被点击后,自动消失
    notification.defaults = Notification.DEFAULT_LIGHTS;
    notification.ledARGB = Color.BLUE;
    notification.ledOnMS = 100;
    notification.ledOffMS = 100;
    Intent notificationIntent = new Intent(BussinessCenter.mContext, BussinessCenter.mContext.getClass());
    notificationIntent.putExtra("action", 2);
    notificationIntent.setAction(Intent.ACTION_MAIN);
    notificationIntent.addCategory(Intent.CATEGORY_LAUNCHER);
    PendingIntent contentIntent = PendingIntent.getActivity(
            BussinessCenter.mContext, 0, notificationIntent,PendingIntent.FLAG_UPDATE_CURRENT);
    notification.setLatestEventInfo(this,this.getString(R.string.BACKING_RUNING), strText,contentIntent);
    notificationManager.notify(notification_id, notification);
}

在低版本中的代码:(来自于AnyChatCallCenter中BcakService类的里面),
但是,在版本里面已经被弃用,甚至hide起来了。

低于API Level 11版本,也就是Android 2.3.3以下的系统中,setLatestEventInfo()函数是唯一的实现方法。前面的有关属性设置这里就不再提了,网上资料很多。

在高版本中,可以用Builder和build()函数来配套的方便使用notification了。

Notification notification = new Notification.Builder(context)    
     .setAutoCancel(true)    
     .setContentTitle("title")    
     .setContentText("describe")    
     .setContentIntent(pendingIntent)    
     .setSmallIcon(R.drawable.ic_launcher)    
     .setWhen(System.currentTimeMillis())    
     .build();   

参考网文:
[Android下setLatestEventInfo警告、Handler警告、SimpleDateFormat警告]{http://www.piaoyi.org/mobile-app/Android-setLatestEventInfo-Handler-SimpleDateFormat.html}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值