Java tutorial
/* * PlugBuddy - the CrapLab SmartPlug companion app * * Copyright (C) 2017 Sven Gregori <sven@craplab.fi> * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal * in the Software without restriction, including without limitation the rights * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell * copies of the Software, and to permit persons to whom the Software is * furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice shall be included in all * copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN * THE SOFTWARE. */ package fi.craplab.plugbuddy; import android.app.Notification; import android.app.NotificationManager; import android.content.Context; import android.support.v4.app.NotificationCompat; /** * Notification handler to display the received plug state. */ public class PlugNotification { /** * The unique identifier for this type of notification. */ private static final String NOTIFICATION_TAG = "Plug"; public static void notify(final Context context, final int value) { final String title = "C\u0413ApL\u2200b PlugBuddy"; final String text = "Plug got " + ((value == 1) ? "plugged in. Yay plug!" : "unplugged. Bye bye."); final NotificationCompat.Builder builder = new NotificationCompat.Builder(context) .setDefaults(Notification.DEFAULT_ALL).setSmallIcon(R.mipmap.ic_launcher).setContentTitle(title) .setContentText(text).setPriority(NotificationCompat.PRIORITY_DEFAULT).setAutoCancel(true); final NotificationManager nm = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE); nm.notify(NOTIFICATION_TAG, 0, builder.build()); } public static void cancel(final Context context) { final NotificationManager nm = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE); nm.cancel(NOTIFICATION_TAG, 0); } }