com.licenta.android.licenseapp.SchedulingService.java Source code

Java tutorial

Introduction

Here is the source code for com.licenta.android.licenseapp.SchedulingService.java

Source

package com.licenta.android.licenseapp;

import android.app.IntentService;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.media.RingtoneManager;
import android.net.Uri;
import android.preference.PreferenceManager;
import android.support.v4.app.NotificationCompat;

import com.licenta.android.licenseapp.activity.MainTabActivity;
import com.licenta.android.licenseapp.alarm.AlarmReceiver;
import com.licenta.android.licenseapp.util.Constants;

/**
 * Created by oana.ilovan on 29.12.2015.
 */
public class SchedulingService extends IntentService {

    private NotificationManager mNotificationManager;

    public SchedulingService() {
        super("SchedulingService");
    }

    @Override
    protected void onHandleIntent(Intent intent) {
        sendNotification(getString(R.string.msg));

        AlarmReceiver.completeWakefulIntent(intent);
    }

    private void sendNotification(String msg) {
        mNotificationManager = (NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE);

        Intent intentDismiss = new Intent(this, MainTabActivity.class);
        intentDismiss.putExtra(Constants.KEY_NOTIFICATION_ID, Constants.ALARM_ID);
        intentDismiss.putExtra(Constants.KEY_DISMISS_ALARM, true);
        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intentDismiss,
                PendingIntent.FLAG_UPDATE_CURRENT);

        Uri alarmSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_ALARM);

        NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this)
                .setSmallIcon(R.mipmap.ic_launcher).setContentTitle("HellO")
                .setStyle(new NotificationCompat.BigTextStyle().bigText(msg)).setContentText(msg)
                .setCategory(Notification.CATEGORY_ALARM).setSound(alarmSound);
        SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
        mBuilder.setSound(Uri.parse(prefs.getString("alarm_ringtone", "")));
        if (prefs.getBoolean("alarm_vibrate", false))
            mBuilder.setVibrate(new long[] { 1000, 1000 });

        mBuilder.addAction(0, getString(R.string.check_in), pendingIntent);
        mBuilder.addAction(R.drawable.ic_stat_action_alarm_off_notif, getString(R.string.dismiss_alarm),
                pendingIntent);
        //mBuilder.setContentIntent(contentIntent);
        mNotificationManager.notify(Constants.ALARM_ID, mBuilder.build());
    }

}