display Stopped Notification - Android Android OS

Android examples for Android OS:Notification

Description

display Stopped Notification

Demo Code


import android.app.Notification;
import android.app.NotificationManager;
import android.app.Service;
import android.content.Context;
import android.graphics.BitmapFactory;
import java.text.DateFormat;
import java.util.Date;

public class Main{
    public static void displayStoppedNotification(Context con) {
        NotificationManager mNotifyMgr = (NotificationManager) con
                .getSystemService(Context.NOTIFICATION_SERVICE);
        if (AlarmUtils.isAlarmSet(con)) {
            long time = AlarmUtils.getTriggerTime(con);
            String timeString = DateFormat
                    .getTimeInstance(DateFormat.SHORT).format(
                            new Date(time));
            String title = con/*from   w w  w .j ava  2s  .c o m*/
                    .getString(R.string.notification_title_waiting);
            String text = con.getString(R.string.notification_waiting,
                    timeString);

            Notification.Builder builder = getNotificationBuilder(con);
            builder.setWhen(time);
            builder.setContentTitle(title);
            builder.setContentText(text);

            builder = addDismissAlarmButton(con, builder);

            mNotifyMgr
                    .notify(AlarmService.NOTIFICATION_ID, builder.build());
        } else {
            mNotifyMgr.cancel(AlarmService.NOTIFICATION_ID);
        }
    }
    private static Notification.Builder getNotificationBuilder(Context con) {
        return new Notification.Builder(con)
                .setVisibility(Notification.VISIBILITY_PUBLIC)
                .setCategory(Notification.CATEGORY_ALARM)
                .setLargeIcon(
                        BitmapFactory.decodeResource(con.getResources(),
                                R.mipmap.ic_launcher))
                .setSmallIcon(R.mipmap.ic_alarm_note);
    }
    private static Notification.Builder addDismissAlarmButton(Context con,
            Notification.Builder builder) {
        return builder.addAction(R.drawable.ic_alarm_off, con
                .getString(R.string.notification_dismiss_alarm),
                AlarmService.getPendingStateChangeIntent(con,
                        AlarmService.StateChange.STOP_RINGING));
    }
}

Related Tutorials