Example usage for android.content Context ALARM_SERVICE

List of usage examples for android.content Context ALARM_SERVICE

Introduction

In this page you can find the example usage for android.content Context ALARM_SERVICE.

Prototype

String ALARM_SERVICE

To view the source code for android.content Context ALARM_SERVICE.

Click Source Link

Document

Use with #getSystemService(String) to retrieve a android.app.AlarmManager for receiving intents at a time of your choosing.

Usage

From source file:com.turkiyedenemeleri.chronometer.ForegroundService.java

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    if (intent.getAction().equals(Constants.ACTION.STARTFOREGROUND_ACTION)) {

        PrefUtils mPreferences = new PrefUtils(this);
        long wakeUpTime = (mPreferences.getStartedTime(intent.getStringExtra("snavid")) + 1800) * 1000;
        /*/* w w  w .  j a  v a2s  .c o  m*/
        AlarmManager am = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
        Intent timerExpire = new Intent(this, TimerExpiredReceiver.class);
        PendingIntent sender = PendingIntent.getBroadcast(this, 0, timerExpire, PendingIntent.FLAG_CANCEL_CURRENT);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        am.setAlarmClock(new AlarmManager.AlarmClockInfo(wakeUpTime, sender), sender);
        } else {
        am.set(AlarmManager.RTC_WAKEUP, wakeUpTime, sender);
        }
        */
        Log.e("TAG", "4 bitince servisi durduracak, broadcast");

        Intent notificationIntent = new Intent(this, MainActivity.class);
        notificationIntent.setAction(Constants.ACTION.MAIN_ACTION);
        notificationIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
        notificationIntent.putExtra("yeni", "yeni");
        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);

        Log.e("TAG", "5 tklarsam ekran a");

        Bitmap icon = BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher);

        Notification notification = new NotificationCompat.Builder(this).setContentTitle("Snav Devam Ediyor")
                .setTicker("Snav Devam Ediyor").setContentText("Snav Devam Ediyor")
                .setSmallIcon(R.mipmap.ic_launcher)
                .setLargeIcon(Bitmap.createScaledBitmap(icon, 128, 128, false)).setContentIntent(pendingIntent)
                .setOngoing(true).build();

        startForeground(Constants.NOTIFICATION_ID.FOREGROUND_SERVICE, notification);
        Log.e("TAG", "6  Devam ediyor");

    } else if (intent.getAction().equals(Constants.ACTION.STOPFOREGROUND_ACTION)) {
        Log.i(LOG_TAG, "Received Stop Foreground Intent");

        Intent cancelIntent = new Intent(this, TimerExpiredReceiver.class);
        PendingIntent sender = PendingIntent.getBroadcast(this, 0, cancelIntent,
                PendingIntent.FLAG_CANCEL_CURRENT);
        AlarmManager am = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
        am.cancel(sender);
        Log.e("TAG", "9  Receiver pta");

        stopForeground(true);
        stopSelf();

        Log.e("TAG", "10 Notif ptal");
    }
    return START_REDELIVER_INTENT;
}

From source file:com.appsaur.tarucassist.AlarmReceiver.java

public void setAlarm(Context context, Calendar calendar, int ID) {
    mAlarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);

    // Put Reminder ID in Intent Extra
    Intent intent = new Intent(context, AlarmReceiver.class);
    intent.putExtra(BaseActivity.EXTRA_REMINDER_ID, Integer.toString(ID));
    mPendingIntent = PendingIntent.getBroadcast(context, ID, intent, PendingIntent.FLAG_CANCEL_CURRENT);

    // Calculate notification time
    Calendar c = Calendar.getInstance();
    long currentTime = c.getTimeInMillis();
    long diffTime = calendar.getTimeInMillis() - currentTime;

    // Start alarm using notification time
    mAlarmManager.set(AlarmManager.ELAPSED_REALTIME, SystemClock.elapsedRealtime() + diffTime, mPendingIntent);

    // Restart alarm if device is rebooted
    ComponentName receiver = new ComponentName(context, BootReceiver.class);
    PackageManager pm = context.getPackageManager();
    pm.setComponentEnabledSetting(receiver, PackageManager.COMPONENT_ENABLED_STATE_ENABLED,
            PackageManager.DONT_KILL_APP);
}

From source file:myblog.richard.vewe.launcher3.LauncherApplication.java

public void restart() {
    if (mIntent != null) {
        AlarmManager alarmManager;//ww w  .j  a v a  2  s  .  c o m
        alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
        alarmManager.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, 15000, mIntent);
        System.exit(2);
    }
}

From source file:com.sean.takeastand.alarmprocess.UnscheduledRepeatingAlarm.java

@Override
public void cancelAlarm() {
    PendingIntent pendingIntent = createPendingIntent(mContext);
    AlarmManager am = (AlarmManager) mContext.getSystemService(Context.ALARM_SERVICE);
    am.cancel(pendingIntent);/*from  w  w w  .ja  v a 2 s . c o m*/
    endAlarmService();
    Utils.setImageStatus(mContext, Constants.NO_ALARM_RUNNING);
}

From source file:com.ota.updates.utils.Utils.java

public static void scheduleNotification(Context context, boolean cancel) {
    AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
    Intent intent = new Intent(context, AppReceiver.class);
    intent.setAction(START_UPDATE_CHECK);
    int intentId = 1673;
    int intentFlag = PendingIntent.FLAG_UPDATE_CURRENT;

    if (cancel) {
        if (alarmManager != null) {
            if (DEBUGGING)
                Log.d(TAG, "Cancelling alarm");
            alarmManager.cancel(PendingIntent.getBroadcast(context, intentId, intent, intentFlag));
        }/*w ww . j  a va2 s.com*/
    } else {
        int requestedInterval;

        if (DEBUG_NOTIFICATIONS) {
            requestedInterval = 30000;
        } else {
            requestedInterval = Preferences.getBackgroundFrequency(context);
        }

        if (DEBUGGING)
            Log.d(TAG, "Setting alarm for " + requestedInterval + " seconds");
        Calendar calendar = Calendar.getInstance();
        long time = calendar.getTimeInMillis() + requestedInterval * 1000;
        alarmManager.set(AlarmManager.RTC_WAKEUP, time,
                PendingIntent.getBroadcast(context, intentId, intent, intentFlag));
    }
}

From source file:com.firsttry.mumbaiparking.helpers.MyAlarmManager.java

public void setAlarm(Context context) {
    SharedPreferences prefs = context.getSharedPreferences("Reminders", 0);

    Calendar cal = Calendar.getInstance();
    int currentDate = cal.get(cal.DATE);
    int currentMonth = cal.get(cal.MONTH);
    int currentYear = cal.get(cal.YEAR);

    int pucDate = prefs.getInt("PUC_DATE", 0);
    int pucMonth = prefs.getInt("PUC_MONTH", 0);
    int pucYear = prefs.getInt("PUC_YEAR", 0);

    int insuranceDate = prefs.getInt("INSURANCE_DATE", 0);
    int insuranceMonth = prefs.getInt("INSURANCE_MONTH", 0);
    int insuranceYear = prefs.getInt("INSURANCE_YEAR", 0);

    int licenseDate = prefs.getInt("LICENSE_DATE", 0);
    int licenseMonth = prefs.getInt("LICENSE_MONTH", 0);
    int licenseYear = prefs.getInt("LICENSE_YEAR", 0);

    AlarmManager manager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
    Intent i = new Intent(context, MyAlarmManager.class);

    Log.i("Dates", "PUC : " + pucDate + " " + pucMonth + " " + pucYear);
    Log.i("Dates", "Licence : " + licenseDate + " " + licenseMonth + " " + licenseYear);
    Log.i("Dates", "Insurance : " + insuranceDate + " " + insuranceMonth + " " + insuranceYear);
    Log.i("Dates", "current : " + currentDate + " " + currentMonth + " " + currentYear);

    if (pucDate != 0 && (pucMonth - currentMonth) == 0 && (pucYear - currentYear) == 0
            && (pucDate - currentDate) < 7 && (pucDate - currentDate) >= 0) {

        Log.i("setAlarm", "Inside if puc");
        i.putExtra("pucDays", (pucDate - currentDate));
    }//w  w w.j a  v a2 s.  co m

    if (licenseDate != 0 && (licenseMonth - currentMonth) == 0 && (licenseYear - currentYear) == 0
            && (licenseDate - currentDate) < 7 && (licenseDate - currentDate) >= 0) {
        Log.i("setAlarm", "Inside if license");
        i.putExtra("licenseDays", (licenseDate - currentDate));
    }

    if (insuranceDate != 0 && (insuranceMonth - currentMonth) == 0 && (insuranceYear - currentYear) == 0
            && (insuranceDate - currentDate) < 7 && (insuranceDate - currentDate) >= 0) {
        Log.i("setAlarm", "Inside if insurance");
        //i.putExtra("type", "Insurance");
        i.putExtra("insuranceDays", (insuranceDate - currentDate));
    }

    PendingIntent pi = PendingIntent.getBroadcast(context, 0, i, PendingIntent.FLAG_UPDATE_CURRENT);

    manager.setInexactRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), 1000 * 10, pi);
}

From source file:cn.studyjams.s2.sj0119.NForget.AlarmReceiver.java

public void setRepeatAlarm(Context context, Calendar calendar, int ID, long RepeatTime) {
    mAlarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);

    // Put Reminder ID in Intent Extra
    Intent intent = new Intent(context, AlarmReceiver.class);
    intent.putExtra(ReminderEditActivity.EXTRA_REMINDER_ID, Integer.toString(ID));
    mPendingIntent = PendingIntent.getBroadcast(context, ID, intent, PendingIntent.FLAG_CANCEL_CURRENT);

    // Calculate notification timein
    Calendar c = Calendar.getInstance();
    long currentTime = c.getTimeInMillis();
    long diffTime = calendar.getTimeInMillis() - currentTime;

    // Start alarm using initial notification time and repeat interval time
    mAlarmManager.setRepeating(AlarmManager.ELAPSED_REALTIME, SystemClock.elapsedRealtime() + diffTime,
            RepeatTime, mPendingIntent);

    // Restart alarm if device is rebooted
    ComponentName receiver = new ComponentName(context, BootReceiver.class);
    PackageManager pm = context.getPackageManager();
    pm.setComponentEnabledSetting(receiver, PackageManager.COMPONENT_ENABLED_STATE_ENABLED,
            PackageManager.DONT_KILL_APP);
}

From source file:com.chess.genesis.net.GenesisNotifier.java

private static void CancelWakup(final Context context) {
    final Intent intent = new Intent(context, GenesisAlarm.class);
    final PendingIntent pintent = PendingIntent.getBroadcast(context, 0, intent,
            PendingIntent.FLAG_UPDATE_CURRENT);

    final AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
    am.cancel(pintent);/*from   w w w  .j a va 2s  . com*/
}

From source file:be.benvd.mvforandroid.data.MVDataService.java

@Override
public void onCreate() {
    super.onCreate();
    prefs = PreferenceManager.getDefaultSharedPreferences(this);
    helper = new DatabaseHelper(this);
    alarm = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
    instance = this;

    Intent i = new Intent(this, OnAlarmReceiver.class);
    wakefulWorkIntent = PendingIntent.getBroadcast(this, 0, i, 0);
}

From source file:com.google.android.apps.muzei.sync.TaskQueueService.java

private void cancelArtworkDownloadRetries() {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        JobScheduler jobScheduler = (JobScheduler) getSystemService(Context.JOB_SCHEDULER_SERVICE);
        jobScheduler.cancel(LOAD_ARTWORK_JOB_ID);
    } else {/*w w  w .  j  a v a2s.  c  o  m*/
        AlarmManager am = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
        am.cancel(TaskQueueService.getArtworkDownloadRetryPendingIntent(this));
        SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(this);
        sp.edit().putInt(PREF_ARTWORK_DOWNLOAD_ATTEMPT, 0).commit();
    }
}