Android定义一个不消失的悬停通知栏

private NotificationManager manager = null;
private Notification.Builder builder = null;
private void showNotification(Context context) {
    manager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
    builder = new Notification.Builder(context);
    builder.setShowWhen(false)
            .setSmallIcon(R.drawable.ic_power_low)
            .setLargeIcon(BitmapFactory.decodeResource(context.getResources(), R.drawable.ic_power_low))
            .setContentTitle("自定义标题")
            .setContentText("自定义内容")
            .setDefaults(NotificationCompat.FLAG_ONGOING_EVENT)
            .setPriority(Notification.PRIORITY_MAX);
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {//SDK版本>=21才能设置悬挂式通知栏
        builder.setCategory(String.valueOf(Notification.FLAG_ONGOING_EVENT))
                .setVisibility(Notification.VISIBILITY_PUBLIC)
                .setColor(context.getResources().getColor(R.color.salmon));
        Intent intent = new Intent();
        PendingIntent pi = PendingIntent.getBroadcast(this, 0, intent, 0);
        builder.setFullScreenIntent(pi, true);
        manager.notify(null, 0, builder.build());
    }
}
private void cancelNotification() {
    if (manager != null) {
        manager.cancel(0);
    }
}

你可能感兴趣的:(Android)