net.eledge.android.europeana.service.receiver.BlogCheckerReceiver.java Source code

Java tutorial

Introduction

Here is the source code for net.eledge.android.europeana.service.receiver.BlogCheckerReceiver.java

Source

/*
 * Copyright (c) 2013-2015 eLedge.net and the original author or authors.
 * 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.
 */

package net.eledge.android.europeana.service.receiver;

import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.support.v4.content.WakefulBroadcastReceiver;

import net.eledge.android.europeana.service.BlogCheckerService;

public class BlogCheckerReceiver extends WakefulBroadcastReceiver {

    private AlarmManager alarmManager;
    private PendingIntent blogCheckerIntent;
    private static final long INTERVAL = AlarmManager.INTERVAL_HOUR * 2;

    @Override
    public void onReceive(Context context, Intent intent) {
        Intent service = new Intent(context, BlogCheckerService.class);
        startWakefulService(context, service);
    }

    public void enableBlogChecker(Context context) {
        alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
        Intent intent = new Intent(context, BlogCheckerReceiver.class);
        blogCheckerIntent = PendingIntent.getBroadcast(context, 0, intent, 0);

        alarmManager.setInexactRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, INTERVAL, INTERVAL,
                blogCheckerIntent);

        ComponentName receiver = new ComponentName(context, BootReceiver.class);
        PackageManager pm = context.getPackageManager();

        if (pm != null) {
            pm.setComponentEnabledSetting(receiver, PackageManager.COMPONENT_ENABLED_STATE_ENABLED,
                    PackageManager.DONT_KILL_APP);
        }
    }

    public void disableBlogChecker(Context context) {
        if (alarmManager != null) {
            alarmManager.cancel(blogCheckerIntent);
        }

        ComponentName receiver = new ComponentName(context, BootReceiver.class);
        PackageManager pm = context.getPackageManager();

        if (pm != null) {
            pm.setComponentEnabledSetting(receiver, PackageManager.COMPONENT_ENABLED_STATE_DISABLED,
                    PackageManager.DONT_KILL_APP);
        }
    }
}