安卓(Android) 怎么通过NotificationCompat创建通知(Notification)

2019年10月8日23:09:18安卓(Android) 怎么通过NotificationCompat创建通知(Notification)已关闭评论 302 1288字阅读4分17秒

1.简介

安卓(Android) 怎么通过NotificationCompat创建通知(Notification)?——请看下文。

2.示例代码

/**
 * 版权所有 编程十万个怎么办(www.tah1986.com)
 */
import android.R;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.TaskStackBuilder;
import android.content.Context;
import android.content.Intent;
import android.support.v4.app.NotificationCompat;

public class Main {
  private static NotificationCompat.Builder mBuilder;
  private static int numMessages = 0;
  private static int notifyId = 1;

  public static void createNotification(Intent resultIntent, Context current, final Class<?> destination) {
    NotificationManager mNotificationManager = (NotificationManager) current
        .getSystemService(Context.NOTIFICATION_SERVICE);

    mBuilder = new NotificationCompat.Builder(current).setContentTitle("Hooooooooooooot")
        .setContentText("You've received a new hoot").setSmallIcon(R.drawable.ic_owl_white).setAutoCancel(true)
        .setNumber(numMessages++);

    if (numMessages > 1) {
      mBuilder.setContentText("You've received new hoots");
    }

    TaskStackBuilder stackBuilder = TaskStackBuilder.create(current);
    stackBuilder.addParentStack(destination);
    stackBuilder.addNextIntent(resultIntent);
    PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
    mBuilder.setContentIntent(resultPendingIntent);

    mNotificationManager.notify(notifyId, mBuilder.build());
  }
}

 
继续阅读
编程十万个怎么办