start Alarm Service by Class<?> - Android Android OS

Android examples for Android OS:Alarm Start

Description

start Alarm Service by Class<?>

Demo Code


//package com.java2s;
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.os.SystemClock;

public class Main {
    public static void startPollingService(Context context, int seconds,
            Class<?> cls, String action) {

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

        Intent intent = new Intent(context, cls);
        if (action != null && action.trim() != "") {
            intent.setAction(action);/*from   w w  w  .j  av a 2  s  . c om*/
        }
        PendingIntent pendingIntent = PendingIntent.getService(context, 0,
                intent, PendingIntent.FLAG_UPDATE_CURRENT);

        long triggerAtTime = SystemClock.elapsedRealtime();

        manager.setRepeating(AlarmManager.ELAPSED_REALTIME, triggerAtTime,
                seconds * 1000, pendingIntent);
    }
}

Related Tutorials