Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;

import java.util.Calendar;

import android.app.AlarmManager;

import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;

public class Main {
    public static void setTimerReceiver(Context context, int repeat_time, Class<?> class_name) {
        AlarmManager alarm = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
        Intent intentReceiver = new Intent(context, class_name);
        PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, intentReceiver,
                PendingIntent.FLAG_CANCEL_CURRENT);
        Calendar cal = Calendar.getInstance();
        cal.add(Calendar.SECOND, repeat_time / 1000);

        // InexactRepeating allows Android to optimize the energy consumption
        alarm.setInexactRepeating(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), repeat_time, pendingIntent);
    }
}