Fire an alarm when device is boots

Fire an alarm when device is boots.

You can set repeating alarm when device is reboot

Here are the steps

1.Set the RECEIVE_BOOT_COMPLETED permission in your application’s manifest. This allows your app to receive the ACTION_BOOT_COMPLETED that is

<uses-permission android:name=”android.permission.RECEIVE_BOOT_COMPLETED”/>

2. set receiver class in manifest

<receiver android:name=”.AlarmReceiverOne”>
<intent-filter>
<action android:name=”android.intent.action.BOOT_COMPLETED”/>
</intent-filter>/>
</receiver>

3. Implement a broadcast receive class

 

public class AlarmReceiverOne extends BroadcastReceiver {
static int count=0;
@Override
public void onReceive(Context context, Intent intent) {

//set alarm code

 

}
}

4. put alarm code in this code alarm wake up the device to fire the alarm at approximately 2:30 p.m., and repeat after 15 minute

 

 

public class AlarmReceiverOne extends BroadcastReceiver {

@Override
public void onReceive(Context context, Intent intent) {

System.out.println(“—————-Alarm Receiver One Start Alarm once Again————-“);

Calendar cal = Calendar.getInstance();
cal.setTimeInMillis(System.currentTimeMillis());
cal.set(Calendar.HOUR_OF_DAY, 14);
cal.set(Calendar.MINUTE, 30);

Intent intent1 = new Intent(context, AlarmReceiver.class);
PendingIntent pintent2 = PendingIntent.getBroadcast(context, 0, intent1, 0);

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

alarm2.setInexactRepeating(AlarmManager.RTC_WAKEUP,cal.getTimeInMillis(),alarm2.INTERVAL_FIFTEEN_MINUTES, pintent2);

 

}
}

4. Alarm Receiver to receive broadcast to start another service

import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.support.v4.content.WakefulBroadcastReceiver;

/

public class AlarmReceiver extends WakefulBroadcastReceiver {

@Override public void onReceive(Context context, Intent intent) {

intent=new Intent(context,StartService.class);

startWakefulService(context, intent);

 

 

}
}

5. set manifest receiver class name

<receiver android:name=”.AlarmReceiver”>
If any suggestion or question please comment

Leave a Reply

Your email address will not be published. Required fields are marked *