天天看點

Android NotificationsAndroid Notifications

Android Notifications

    一條通知消息是顯示于你用于正常UI之外的地方。你可以通過點選它來檢視通知詳情。另外,NotificationCompat.Builder是用于低版本建構的方式,Android 3.0以上使用Notification.Builder來建構。通知顯示有兩種視圖樣式,1種是普通視圖,1種是大視圖(Android4.1以上才行)。

NotificationManager manager = (NotificationManager)getSystemService(NOTIFICATION_SERVICE); Notification.Builder builder = new Notification.Builder(this); builder.setTicker(""); builder.setSmallIcon(R.drawable.xxx); builder.setContentTitle(""); builder.setContentText(""); Intent intent = new Intent(Activity.this, Activity.class); PendingIntent pendingIntent = PendingIntent.getActivity(Activity.this, 1, intent, PendingIntent.FLAG_ONE_SHOT); builder.setContentIntent(pendingIntent); manager.notify(id, builder.build());

如果需要添加進度條需要進行如下操作: new Thread(new Runnable{

    builder.setProgress(max,  cur, fasle);     manager.notify(id, builder.build()); }).start();

builder.setProgress(0, 0, false);//取消進度條 manager.notify(id, builder.build());

自定義通知:

Notification.Builder builder = new Notification.Builder(Activity.this); RemoteViews remoteViews= new RemoteViews(getPackageName(), R.layout.xxx); builder.setTicker(""); builder.setContentTitle(""); builder.setContentText(""); builder.setSmallIcon(R.drawable.xxx);

remoteViews.setTextViewText(R.id.xxx, ""); remoteViews.setImageViewResource(R.id.xxx, R,drawable.xxx);

Intent intent = new Intent(Activity.this, Activity.class); PendingIntent pendingIntent = PendingIntent.getActivity(Activity.this, 1, intent, PendingIntent.FLAG_ONE_SHOT); builder.setContentIntent(pendingIntent); builder.setContent(remoteViews); manager.notify(id, builder.build());