Notification :可以设置 icon message 或者声音播放等提示
NotificationManager :系统服务,执行或管理所有状态通知栏,不能够实例化: (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
具体实例:
/**获得mNotificationManager**/
mNotificationManager = (NotificationManager) getSystemService(ns);
/**定义notification****/
int icon = R.drawable.ic_launcher;
CharSequence tickerText = "Hello";
long when = System.currentTimeMillis();
notification = new Notification(icon, tickerText, when);
/***定义丰富的提示方式**/
notification.defaults |= Notification.DEFAULT_LIGHTS;
notification.ledARGB = 0xff00ff00;
notification.ledOnMS = 300;
notification.ledOffMS = 1000;
notification.flags |= Notification.FLAG_SHOW_LIGHTS;
/**定义notification 消息 和 pendingIntent****/
Context context = getApplicationContext();
CharSequence contentTitle = "My notification";
CharSequence contentText = "Hello World!";
Intent notificationIntent = new Intent(this, AndroidBroadcastActivity.class);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent);
/**内容View 定义消息***/
RemoteViews contentView = new RemoteViews(getPackageName(), R.layout.custom_notification);
contentView.setImageViewResource(R.id.image, R.drawable.ic_launcher);
contentView.setTextViewText(R.id.title, "Custom notification");
contentView.setTextViewText(R.id.text, "This is a custom layout");
notification.contentView = contentView;
//
//notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent);
notification.contentIntent = contentIntent;
//开启消息通知
mNotificationManager.notify(HELLO_ID, notification);