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}

你可能感兴趣的:(琐碎知识点)