安卓(Android) 怎么生成通知(notification)

2019年10月9日22:59:07安卓(Android) 怎么生成通知(notification)已关闭评论 438 1568字阅读5分13秒

1.简介

安卓(Android) 怎么生成通知(notification)?——请看下文。

2.示例代码

/**
 * 版权所有 编程十万个怎么办(www.tah1986.com)
 */

 
import android.annotation.TargetApi;
import android.app.Notification;
import android.app.PendingIntent;
import android.content.Context;
import android.graphics.Bitmap;
import android.os.Build;
import android.os.Build.VERSION_CODES;

public class Main{
    public static final String TAG = LogUtil
            .getLogUtilsTag(Notification.class);
    @TargetApi(VERSION_CODES.HONEYCOMB)
    public static Notification buildNotification(Context context, int icon,
            int defaults, boolean onlyVibrate, String tickerText,
            String contentTitle, String contentText, Bitmap largeIcon,
            PendingIntent intent) {

        if (Build.VERSION.SDK_INT > VERSION_CODES.HONEYCOMB) {
            Notification.Builder builder = new Notification.Builder(context);
            builder.setLights(-16711936, 300, 1000).setSmallIcon(icon)
                    .setTicker(tickerText).setContentTitle(contentTitle)
                    .setContentText(contentText).setContentIntent(intent);

            if (onlyVibrate) {
                defaults &= Notification.DEFAULT_VIBRATE;
            }

            LogUtil.d(TAG, "defaults flag " + defaults);
            builder.setDefaults(defaults);
            if (largeIcon != null) {
                builder.setLargeIcon(largeIcon);
            }
            return builder.getNotification();
        }

        Notification notification = new Notification();
        notification.ledARGB = -16711936;
        notification.ledOnMS = 300;
        notification.ledOffMS = 1000;
        notification.flags = (Notification.FLAG_SHOW_LIGHTS | notification.flags);
        notification.icon = icon;
        notification.tickerText = tickerText;
        LogUtil.d(TAG, "defaults flag " + defaults);
        if (onlyVibrate) {
            defaults &= Notification.DEFAULT_VIBRATE;
        }
        notification.defaults = defaults;
        notification.setLatestEventInfo(context, contentTitle, contentText,
                intent);
        return notification;
    }
}

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