天天看点

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());