Example usage for android.app AlarmManager RTC

List of usage examples for android.app AlarmManager RTC

Introduction

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

Prototype

int RTC

To view the source code for android.app AlarmManager RTC.

Click Source Link

Document

Alarm time in System#currentTimeMillis System.currentTimeMillis() (wall clock time in UTC).

Usage

From source file:net.naonedbus.appwidget.HoraireWidgetProvider.java

/**
 * Programmer le rafraichissement des widgets.
 * //from  w w  w  .j  a  v a 2s  .c  o m
 * @param context
 * @param timestamp
 */
private synchronized void scheduleUpdate(final Context context, final long timestamp) {
    final long now = new DateTime().getMillis();
    final long triggerTimestamp = new DateTime(timestamp).plusMinutes(1).getMillis();

    if (sNextTimestamp < now || triggerTimestamp < sNextTimestamp) {
        sNextTimestamp = triggerTimestamp;

        final AlarmManager m = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
        if (sPendingIntent != null) {
            m.cancel(sPendingIntent);

            if (DBG)
                Log.i(LOG_TAG,
                        Integer.toHexString(hashCode()) + " - " + "\tAnnulation de la prcdente alarme.");
        }

        final Intent intent = new Intent(ACTION_APPWIDGET_UPDATE);
        sPendingIntent = PendingIntent.getBroadcast(context, 0, intent, 0);
        m.set(AlarmManager.RTC, sNextTimestamp, sPendingIntent);

        if (DBG)
            Log.i(LOG_TAG, Integer.toHexString(hashCode()) + " - " + "Programmation de l'alarme  : "
                    + new DateTime(sNextTimestamp).toString());
    }

}

From source file:com.jmstudios.redmoon.receiver.AutomaticFilterChangeReceiver.java

public static void scheduleNextAlarm(Context context, String time, Intent operation) {
    GregorianCalendar calendar = new GregorianCalendar();
    calendar.set(Calendar.HOUR_OF_DAY, Integer.parseInt(time.split(":")[0]));
    calendar.set(Calendar.MINUTE, Integer.parseInt(time.split(":")[1]));

    GregorianCalendar now = new GregorianCalendar();
    now.add(Calendar.SECOND, 1);//from w ww.  ja v a 2  s .  c o m
    if (calendar.before(now)) {
        calendar.add(Calendar.DATE, 1);
    }

    if (DEBUG)
        Log.i(TAG, "Scheduling alarm for " + calendar.toString());

    AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);

    PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, operation, 0);

    if (android.os.Build.VERSION.SDK_INT >= 19) {
        alarmManager.setExact(AlarmManager.RTC, calendar.getTimeInMillis(), pendingIntent);
    } else {
        alarmManager.set(AlarmManager.RTC, calendar.getTimeInMillis(), pendingIntent);
    }
}

From source file:org.jamienicol.episodes.AutoRefreshHelper.java

public void rescheduleAlarm() {
    NetworkStateReceiver.disable(context);

    final AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);

    final Intent intent = new Intent(context, AutoRefreshHelper.Service.class);
    final PendingIntent pendingIntent = PendingIntent.getService(context, 0, intent, 0);

    if (getAutoRefreshEnabled() && getAutoRefreshPeriod() != 0) {
        final long alarmTime = getPrevAutoRefreshTime() + getAutoRefreshPeriod();

        Log.i(TAG, String.format("Scheduling auto refresh alarm for %d.", alarmTime));

        alarmManager.set(AlarmManager.RTC, alarmTime, pendingIntent);
    } else {// w w w . j  a  va 2  s .c  o  m
        Log.i(TAG, "Cancelling auto refresh alarm.");

        alarmManager.cancel(pendingIntent);
    }
}

From source file:org.andicar.service.UpdateCheckService.java

private void setNextRun() {
    Calendar cal = Calendar.getInstance();
    if (cal.get(Calendar.HOUR_OF_DAY) >= 18)
        cal.add(Calendar.DAY_OF_YEAR, 1);
    cal.set(Calendar.HOUR_OF_DAY, 20);
    cal.set(Calendar.MINUTE, 0);/*from w  w w. ja  v  a 2s  .  c o m*/
    Intent i = new Intent(this, UpdateCheckService.class);
    PendingIntent pIntent = PendingIntent.getService(this, 0, i, PendingIntent.FLAG_UPDATE_CURRENT);
    AlarmManager am = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
    am.set(AlarmManager.RTC, cal.getTimeInMillis(), pIntent);
}

From source file:com.meiste.greg.ptw.WidgetProvider.java

@SuppressLint("NewApi")
private void setAlarm(final Context context) {
    /* No point setting alarm if no widgets */
    if (getInstalledWidgets(context).length == 0)
        return;/*from   w  w  w .j ava  2 s. c  o  m*/

    final long now = System.currentTimeMillis();
    long next = UPDATE_INTERVAL - (now % UPDATE_INTERVAL) - UPDATE_FUDGE;
    if (next <= 0) {
        next += UPDATE_INTERVAL;
    }

    final AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
        am.setExact(AlarmManager.RTC, now + next, getAlarmIntent(context));
    } else {
        am.set(AlarmManager.RTC, now + next, getAlarmIntent(context));
    }
}

From source file:com.watabou.noosa.Game.java

public void doRestart() {
    Intent i = instance().getBaseContext().getPackageManager()
            .getLaunchIntentForPackage(getBaseContext().getPackageName());
    i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);

    int piId = 123456;
    PendingIntent pi = PendingIntent.getActivity(getBaseContext(), piId, i, PendingIntent.FLAG_CANCEL_CURRENT);
    AlarmManager mgr = (AlarmManager) getBaseContext().getSystemService(ContextWrapper.ALARM_SERVICE);
    mgr.set(AlarmManager.RTC, System.currentTimeMillis() + 100, pi);
    shutdown();/*w  w w  . j  a  v a  2  s  .c  o  m*/
}

From source file:net.texh.cordovapluginstepcounter.StepCounterService.java

@Override
public boolean stopService(Intent intent) {
    Log.i(TAG, "- Received stop: " + intent);
    //Stop listening to events when stop() is called
    if (isRunning) {
        mSensorManager.unregisterListener(this);
        isRunning = false;/*from w w  w  . jav  a2  s.com*/
    }

    SharedPreferences sharedPref = getSharedPreferences(CordovaStepCounter.USER_DATA_PREF,
            Context.MODE_PRIVATE);
    Boolean pActive = CordovaStepCounter.getPedometerIsActive(sharedPref);
    if (pActive) {
        Log.i(TAG, "- Relaunch service in 500ms");
        //Autorelaunch the service
        //@TODO should test if stopService is called from killing app or from calling stop() method in CordovaStepCounter
        Intent newServiceIntent = new Intent(this, StepCounterService.class);
        AlarmManager aManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
        aManager.set(AlarmManager.RTC, java.lang.System.currentTimeMillis() + 500,
                PendingIntent.getService(this, 11, newServiceIntent, 0));
    } else {
        Log.i(TAG, "StepCounter stopped, will not relaunch service");
    }

    return super.stopService(intent);
}

From source file:com.tortel.deploytrack.service.NotificationService.java

@SuppressLint("NewApi")
private void showNotification() {
    // If there isnt an ID saved, shut down the service
    if (deploymentId == -1) {
        stopSelf();//from w  w  w  .  j a  va2 s . co m
        return;
    }
    if (DEBUG) {
        Toast.makeText(this, "NotificationService loading notification", Toast.LENGTH_SHORT).show();
    }

    // Load the Deployment object
    Deployment deployment = DatabaseManager.getInstance(this).getDeployment(deploymentId);
    SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);

    RemoteViews view = new RemoteViews(getPackageName(), R.layout.notification);
    view.setImageViewBitmap(R.id.notification_pie, WidgetProvider.getChartBitmap(deployment, SIZE));
    view.setTextViewText(R.id.notification_title, deployment.getName());

    view.setTextViewText(R.id.notification_main, getResources().getString(R.string.small_notification,
            deployment.getPercentage(), deployment.getCompleted(), deployment.getLength()));

    if (prefs.getBoolean(Prefs.KEY_HIDE_DATE, false)) {
        view.setViewVisibility(R.id.notification_daterange, View.GONE);
    } else {
        view.setTextViewText(R.id.notification_daterange, getResources().getString(R.string.date_range,
                deployment.getFormattedStart(), deployment.getFormattedEnd()));
    }

    NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
    builder.setContentTitle(deployment.getName());
    builder.setContentText(getResources().getString(R.string.small_notification, deployment.getPercentage(),
            deployment.getCompleted(), deployment.getLength()));
    builder.setOngoing(true);

    // Hide the time, its persistent
    builder.setWhen(0);

    builder.setSmallIcon(R.drawable.ic_notification);
    builder.setPriority(Integer.MAX_VALUE);

    Notification notification = builder.build();

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
        notification.bigContentView = view;
    }

    notificationManager.notify(NOTIFICATION_ID, notification);

    //Schedule an update at midnight
    AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
    DateTime now = new DateTime();
    DateTime tomorrow = new DateTime(now.plusDays(1)).withTimeAtStartOfDay();

    PendingIntent pending = PendingIntent.getBroadcast(getBaseContext(), 0, new Intent(UPDATE_INTENT),
            PendingIntent.FLAG_UPDATE_CURRENT);

    //Adding 100msec to make sure its triggered after midnight
    Log.d("Scheduling notification update for " + tomorrow.getMillis() + 100);

    if (Build.VERSION.SDK_INT > Build.VERSION_CODES.JELLY_BEAN_MR2) {
        alarmManager.setExact(AlarmManager.RTC, tomorrow.getMillis() + 100, pending);
    } else {
        alarmManager.set(AlarmManager.RTC, tomorrow.getMillis() + 100, pending);
    }
}

From source file:com.example.babycare.fragments.ExampleFragment.java

private void setAlarm(Context context, long second) {
    Log.i("TAG", "setAlarm()");
    AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);

    Intent Intent = new Intent(getActivity(), Temper_Show_Activity.class);
    PendingIntent pIntent = PendingIntent.getActivity(getActivity(), 0, Intent, 0);

    alarmManager.set(AlarmManager.RTC, System.currentTimeMillis(), pIntent);
    Log.i("TAG", "setAlarm()");
}

From source file:com.macleod2486.utnow.MainActivity.java

@Override
public void onStop() {
    //Start the service in a timely interval
    SharedPreferences shared = PreferenceManager.getDefaultSharedPreferences(this);

    if (shared.getBoolean("notification", false) && shared.getBoolean("notifCancel", true)) {
        //one second * 60 seconds in a minute * 5
        long fiveMinutes = 1000 * 60 * 5;

        //Start the alarm manager service
        SharedPreferences.Editor edit = shared.edit();
        Intent service = new Intent(getApplicationContext(), UTBroadcast.class);
        PendingIntent pendingService = PendingIntent.getBroadcast(getApplicationContext(), 0, service, 0);
        AlarmManager newsUpdate = (AlarmManager) getSystemService(ALARM_SERVICE);

        //Check for the update every 5 minutes
        newsUpdate.setRepeating(AlarmManager.RTC, System.currentTimeMillis(), fiveMinutes, pendingService);
        edit.putBoolean("notifCancel", false).commit();
        Log.i("UTService", "Alarm set " + shared.getBoolean("notification", true));
    }//from   ww w.j av a2  s  . co  m

    super.onStop();
}