townley.stuart.app.android.trekkinly.AlertClass.java Source code

Java tutorial

Introduction

Here is the source code for townley.stuart.app.android.trekkinly.AlertClass.java

Source

package townley.stuart.app.android.trekkinly;

import android.app.AlarmManager;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Handler;
import android.support.v4.app.NotificationCompat;
import android.widget.Toast;

import itemclass_all.NotificationItemClass;

/**
 * Copyright 2015 Stuart Lachlan Townley
    
 Licensed under the Apache License, Version 2.0 (the "License");
 you may not use this file except in compliance with the License.
 You may obtain a copy of the License at
    
 http://www.apache.org/licenses/LICENSE-2.0
    
 *Unless required by applicable law or agreed to in writing, software
 distributed under the License is distributed on an "AS IS" BASIS,
 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 See the License for the specific language governing permissions and
 limitations under the License.
 */

public class AlertClass extends BroadcastReceiver {

    @Override
    public void onReceive(final Context context, Intent intent) {

        NotificationItemClass notificationItemClass = new NotificationItemClass();
        DataBaseHelper db = new DataBaseHelper(context);
        db.getWritableDatabase();
        db.getNotification(notificationItemClass);

        if (db.notification <= System.currentTimeMillis()) {
            createNotification(context, "Something is missing!", "Click here for more details!", "Alert");

            ComponentName receiver = new ComponentName(context, AlertClass.class);
            PackageManager pm = context.getPackageManager();
            pm.setComponentEnabledSetting(receiver, PackageManager.COMPONENT_ENABLED_STATE_DISABLED,
                    PackageManager.DONT_KILL_APP);

            intent = new Intent(context, AlertClass.class);
            AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
            alarmManager.cancel(PendingIntent.getBroadcast(context.getApplicationContext(), 1, intent,
                    PendingIntent.FLAG_UPDATE_CURRENT));
        }

        else if (db.notification > System.currentTimeMillis()) {
            intent = new Intent(context, AlertClass.class);
            AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
            alarmManager.set(AlarmManager.RTC_WAKEUP, db.notification, PendingIntent
                    .getBroadcast(context.getApplicationContext(), 1, intent, PendingIntent.FLAG_UPDATE_CURRENT));
        }
    }

    private void createNotification(Context context, String title, String text, String alert) {
        PendingIntent pendingIntent = PendingIntent.getActivity(context, 0,
                new Intent(context, MainActivityTrekkly.class), 0);
        NotificationCompat.Builder notification = new NotificationCompat.Builder(context).setTicker(alert)
                .setSmallIcon(R.drawable.ic_launcher).setContentTitle(title).setContentText(text)
                .setDefaults(Notification.DEFAULT_SOUND).setAutoCancel(true);

        notification.setContentIntent(pendingIntent);

        NotificationManager notificationManager = (NotificationManager) context
                .getSystemService(Context.NOTIFICATION_SERVICE);
        notificationManager.notify(1, notification.build());

    }
}