Example usage for android.app AlarmManager setInexactRepeating

List of usage examples for android.app AlarmManager setInexactRepeating

Introduction

In this page you can find the example usage for android.app AlarmManager setInexactRepeating.

Prototype

public void setInexactRepeating(@AlarmType int type, long triggerAtMillis, long intervalMillis,
        PendingIntent operation) 

Source Link

Document

Schedule a repeating alarm that has inexact trigger time requirements; for example, an alarm that repeats every hour, but not necessarily at the top of every hour.

Usage

From source file:eu.faircode.netguard.ServiceSinkhole.java

@Override
public void onCreate() {
    Log.i(TAG, "Create version=" + Util.getSelfVersionName(this) + "/" + Util.getSelfVersionCode(this));

    SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);

    // Native init
    jni_init();/*from  w w  w.j a va2  s  .c  om*/
    boolean pcap = prefs.getBoolean("pcap", false);
    setPcap(pcap, this);

    prefs.registerOnSharedPreferenceChangeListener(this);

    Util.setTheme(this);
    super.onCreate();

    HandlerThread commandThread = new HandlerThread(getString(R.string.app_name) + " command",
            Process.THREAD_PRIORITY_FOREGROUND);
    HandlerThread logThread = new HandlerThread(getString(R.string.app_name) + " log",
            Process.THREAD_PRIORITY_BACKGROUND);
    HandlerThread statsThread = new HandlerThread(getString(R.string.app_name) + " stats",
            Process.THREAD_PRIORITY_BACKGROUND);
    commandThread.start();
    logThread.start();
    statsThread.start();

    commandLooper = commandThread.getLooper();
    logLooper = logThread.getLooper();
    statsLooper = statsThread.getLooper();

    commandHandler = new CommandHandler(commandLooper);
    logHandler = new LogHandler(logLooper);
    statsHandler = new StatsHandler(statsLooper);

    // Listen for power save mode
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP && !Util.isPlayStoreInstall(this)) {
        PowerManager pm = (PowerManager) getSystemService(POWER_SERVICE);
        powersaving = pm.isPowerSaveMode();
        IntentFilter ifPower = new IntentFilter();
        ifPower.addAction(PowerManager.ACTION_POWER_SAVE_MODE_CHANGED);
        registerReceiver(powerSaveReceiver, ifPower);
        registeredPowerSave = true;
    }

    // Listen for user switches
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
        IntentFilter ifUser = new IntentFilter();
        ifUser.addAction(Intent.ACTION_USER_BACKGROUND);
        ifUser.addAction(Intent.ACTION_USER_FOREGROUND);
        registerReceiver(userReceiver, ifUser);
        registeredUser = true;
    }

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
        // Listen for idle mode state changes
        IntentFilter ifIdle = new IntentFilter();
        ifIdle.addAction(PowerManager.ACTION_DEVICE_IDLE_MODE_CHANGED);
        registerReceiver(idleStateReceiver, ifIdle);
        registeredIdleState = true;
    }

    // Listen for connectivity updates
    IntentFilter ifConnectivity = new IntentFilter();
    ifConnectivity.addAction(ConnectivityManager.CONNECTIVITY_ACTION);
    registerReceiver(connectivityChangedReceiver, ifConnectivity);
    registeredConnectivityChanged = true;

    // Listen for added applications
    IntentFilter ifPackage = new IntentFilter();
    ifPackage.addAction(Intent.ACTION_PACKAGE_ADDED);
    ifPackage.addAction(Intent.ACTION_PACKAGE_REMOVED);
    ifPackage.addDataScheme("package");
    registerReceiver(packageChangedReceiver, ifPackage);
    registeredPackageChanged = true;

    // Setup house holding
    Intent alarmIntent = new Intent(this, ServiceSinkhole.class);
    alarmIntent.setAction(ACTION_HOUSE_HOLDING);
    PendingIntent pi = PendingIntent.getService(this, 0, alarmIntent, PendingIntent.FLAG_UPDATE_CURRENT);

    AlarmManager am = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
    am.setInexactRepeating(AlarmManager.RTC, SystemClock.elapsedRealtime() + 60 * 1000,
            AlarmManager.INTERVAL_HALF_DAY, pi);
}

From source file:com.codename1.impl.android.AndroidImplementation.java

public void scheduleLocalNotification(LocalNotification notif, long firstTime, int repeat) {

    final Intent notificationIntent = new Intent(getContext(), LocalNotificationPublisher.class);
    notificationIntent.setAction(getContext().getApplicationInfo().packageName + "." + notif.getId());
    notificationIntent.putExtra(LocalNotificationPublisher.NOTIFICATION, createBundleFromNotification(notif));

    Intent contentIntent = new Intent();
    if (getActivity() != null) {
        contentIntent.setComponent(getActivity().getComponentName());
    }/*from  w w w. j  a v  a 2  s  . c o  m*/
    contentIntent.putExtra("LocalNotificationID", notif.getId());

    if (BACKGROUND_FETCH_NOTIFICATION_ID.equals(notif.getId()) && getBackgroundFetchListener() != null) {
        Context context = AndroidNativeUtil.getContext();

        Intent intent = new Intent(context, BackgroundFetchHandler.class);
        //there is an bug that causes this to not to workhttps://code.google.com/p/android/issues/detail?id=81812
        //intent.putExtra("backgroundClass", getBackgroundLocationListener().getName());
        //an ugly workaround to the putExtra bug 
        intent.setData(
                Uri.parse("http://codenameone.com/a?" + getBackgroundFetchListener().getClass().getName()));
        PendingIntent pendingIntent = PendingIntent.getService(context, 0, intent,
                PendingIntent.FLAG_UPDATE_CURRENT);
        notificationIntent.putExtra(LocalNotificationPublisher.BACKGROUND_FETCH_INTENT, pendingIntent);

    } else {
        contentIntent.setData(
                Uri.parse("http://codenameone.com/a?LocalNotificationID=" + Uri.encode(notif.getId())));
    }
    PendingIntent pendingContentIntent = PendingIntent.getActivity(getContext(), 0, contentIntent,
            PendingIntent.FLAG_UPDATE_CURRENT);

    notificationIntent.putExtra(LocalNotificationPublisher.NOTIFICATION_INTENT, pendingContentIntent);

    PendingIntent pendingIntent = PendingIntent.getBroadcast(getContext(), 0, notificationIntent,
            PendingIntent.FLAG_UPDATE_CURRENT);

    AlarmManager alarmManager = (AlarmManager) getContext().getSystemService(Context.ALARM_SERVICE);
    if (BACKGROUND_FETCH_NOTIFICATION_ID.equals(notif.getId())) {
        alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, firstTime,
                getPreferredBackgroundFetchInterval() * 1000, pendingIntent);
    } else {
        if (repeat == LocalNotification.REPEAT_NONE) {
            alarmManager.set(AlarmManager.RTC_WAKEUP, firstTime, pendingIntent);

        } else if (repeat == LocalNotification.REPEAT_MINUTE) {

            alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, firstTime, 60 * 1000, pendingIntent);

        } else if (repeat == LocalNotification.REPEAT_HOUR) {

            alarmManager.setInexactRepeating(AlarmManager.RTC_WAKEUP, firstTime,
                    AlarmManager.INTERVAL_HALF_HOUR, pendingIntent);

        } else if (repeat == LocalNotification.REPEAT_DAY) {

            alarmManager.setInexactRepeating(AlarmManager.RTC_WAKEUP, firstTime, AlarmManager.INTERVAL_DAY,
                    pendingIntent);

        } else if (repeat == LocalNotification.REPEAT_WEEK) {

            alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, firstTime, AlarmManager.INTERVAL_DAY * 7,
                    pendingIntent);

        }
    }
}