开发者

How can I create a notification in the android status bar?

开发者 https://www.devze.com 2023-03-10 11:28 出处:网络
Hi just wanted to share my notification bui开发者_JAVA技巧lder for android the answer is below. Please share any changes.minimal usage:

Hi just wanted to share my notification bui开发者_JAVA技巧lder for android the answer is below.

Please share any changes.


minimal usage :

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

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);
     * 
     * @param flags
     */
    public void setFlags(int flags) {
        this.flags = flags;
    }

    /**
     * sets the intent for
     * 
     * PendingIntent contentIntent = PendingIntent.getActivity(context, 0,
     * intent, 0); notification.setLatestEventInfo(context, contentTitle,
     * contentText,contentIntent); this functionality was not tested
     * 
     * @param intent
     */
    public void setIntent(Intent intent) {
        this.intent = intent;
    }

    /**
     * sets the sound for the notification was not tested but should work usage
     * for default  notification call the method :setDefaults(Notification.DEFAULT_SOUND);
     * usage: 
     * To use a different sound with your notifications, pass a Uri reference to
     * the sound field. The following example uses a known audio file saved to
     * the device SD card: 
     * notification.sound =
     * Uri.parse("file:///sdcard/notification/ringer.mp3");
     * 
     * In the next example, the audio file is chosen from the internal
     * MediaStore's ContentProvider: notification.sound =
     * Uri.withAppendedPath(Audio.Media.INTERNAL_CONTENT_URI, "6");
     * 
     * @param sound
     */
    public void setSound(Uri sound) {
        this.sound = sound;
    }

}


0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号