如何在android状态栏中创建通知?

she*_*rif 5 notifications android

嗨,只是想分享我的android通知构建器,答案如下.

请分享任何变化.

she*_*rif 2

minimal usage :

NotificatorFacade nb = new NotificatorFacade(context);
nb.show(R.drawable.icon, "tickerText", new Date().getTime(), 
                 "contentTitle", "contentText", ERROR_NOTIFICATION_ID);
Run Code Online (Sandbox Code Playgroud)

source:

package my.tools.android.notification;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;

public class NotificatorBuilder {

    private final Context context;
    private Intent intent;
    private Integer flags;

    private Integer defaults;
    private Uri sound;

    public NotificatorBuilder(Context context) {
        this.context = context;

    }

    /**
     * sets the flags for Notification.defaults
     * 
     * @param defaults
     */
    public void setDefaults(int defaults) {
        this.defaults = defaults;
    }

    /**
     * displays the notification with the given parameters it sets
     * notification.flags|=Notification.FLAG_AUTO_CANCEL when intent (setIntent)
     * is null the setIntent functionality was not tested
     * 
     * @see http
     *      ://developer.android.com/guide/topics/ui/notifiers/notifications.
     *      html
     * @param iconDrawable the icon
     * @param tickerText 
     * @param when 
     * @param contentTitle
     * @param contentText
     * @param NOTIFICATION_ID this id is used for later identification 
     */

    public void show(int iconDrawable, CharSequence tickerText, long when,
            CharSequence contentTitle, CharSequence contentText,
            int NOTIFICATION_ID) {
        // Get a reference to the NotificationManager:
        String ns = Context.NOTIFICATION_SERVICE;
        NotificationManager mNotificationManager = (NotificationManager) context
                .getSystemService(ns);
        // Instantiate the Notification:
        Notification notification = new Notification(iconDrawable, tickerText,
                when);
        // Define the Notification's expanded message and Intent:

        if (sound == null) {

            notification.sound = sound;
        }
        if (flags != null) {
            notification.flags = flags;
        }
        if (defaults != null) {
            notification.defaults = defaults;
        }
        // if intent null create one and set the FLAG_AUTO_CANCEL flag EXTENDS
        // FLAGS!!!
        if (intent == null) {
            setIntent(new Intent(context, NotificatorBuilder.class));
            notification.flags |= Notification.FLAG_AUTO_CANCEL;
        }

        PendingIntent contentIntent = PendingIntent.getActivity(context, 0,
                intent, 0);

        notification.setLatestEventInfo(context, contentTitle, contentText,

        contentIntent);

        mNotificationManager.notify(NOTIFICATION_ID, notification);

    }

    /**
     * sets the flags for notification usage: NotificatorBuilder nb = new
     * NotificatorBuilder(context);
     * nb.setFlags(Notification.DEFAULT_VIBRATE|Notification.FLAG_INSISTENT);
     *
     * @参数标志
     */
    公共无效setFlags(int标志){
        this.flags = 标志;
    }

    /**
     * 设置意图
     *
     * PendingIntent contentIntent = PendingIntent.getActivity(上下文, 0,
     *意图,0); notification.setLatestEventInfo(上下文, contentTitle,
     * 内容文本,内容意图);此功能未经测试
     *
     * @参数意图
     */
    公共无效setIntent(意图意图){
        this.intent = 意图;
    }

    /**
     * 设置通知的声音尚未测试,但应该可以使用
     * 对于默认通知,请调用方法:setDefaults(Notification.DEFAULT_SOUND);
     * 用法:
     * 要在通知中使用不同的声音,请将 Uri 引用传递给
     * 声场。以下示例使用保存到的已知音频文件
     * 设备SD卡:
     * 通知.声音 =
     * Uri.parse("文件:///sdcard/notification/ringer.mp3");
     *
     * 在下一个示例中,音频文件是从内部选择的
     * MediaStore的ContentProvider: notification.sound =
     * Uri.withAppishedPath(Audio.Media.INTERNAL_CONTENT_URI, "6");
     *
     * @参数声音
     */
    公共无效setSound(Uri声音){
        this.sound = 声音;
    }

}