create repeating alarm service for 5 minute ,use below code in your MainActivity.class or where you want to start repeating alarm.
public static PendingIntent pendingIntentfiftyfive;
public static AlarmManager alarmManagerfiftyfive;
Intent intentser1 = new Intent(mContext, FiveMinuteService.class);
pendingIntentfiftyfive = PendingIntent.getService(mContext, 1, intentser1, 0);
alarmManagerfiftyfive = (AlarmManager)mContext.getSystemService(Context.ALARM_SERVICE);
Calendar calendarser1 = Calendar.getInstance();
calendarser1.setTimeInMillis(System.currentTimeMillis());
calendarser1.add(Calendar.MINUTE, 5);
alarmManagerfiftyfive.setRepeating(AlarmManager.RTC_WAKEUP, calendarser1.getTimeInMillis(),5 * 60 * 1000, pendingIntentfiftyfive);
create service name FiveMinuteService
public class FiveMinuteService extends Service{
@Override
public void onStart(Intent intent, int startId) {
// TODO Auto-generated method stub
super.onStart(intent, startId);
// try your code here
// put your toast here
}
@Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return null;
// try you code here
}
}
now ,to cancel repeating alarm use below code
alarmManagerfiftyfive.cancel(pendingIntentfiftyfive);
now ,to cancel repeating alarm from other activity use below code
here MainActivity is your activity where you put your repeating alarm code.
MainActivity.alarmManagerfiftyfive.cancel(MainActivity.pendingIntentfiftyfive);
public static PendingIntent pendingIntentfiftyfive;
public static AlarmManager alarmManagerfiftyfive;
Intent intentser1 = new Intent(mContext, FiveMinuteService.class);
pendingIntentfiftyfive = PendingIntent.getService(mContext, 1, intentser1, 0);
alarmManagerfiftyfive = (AlarmManager)mContext.getSystemService(Context.ALARM_SERVICE);
Calendar calendarser1 = Calendar.getInstance();
calendarser1.setTimeInMillis(System.currentTimeMillis());
calendarser1.add(Calendar.MINUTE, 5);
alarmManagerfiftyfive.setRepeating(AlarmManager.RTC_WAKEUP, calendarser1.getTimeInMillis(),5 * 60 * 1000, pendingIntentfiftyfive);
create service name FiveMinuteService
public class FiveMinuteService extends Service{
@Override
public void onStart(Intent intent, int startId) {
// TODO Auto-generated method stub
super.onStart(intent, startId);
// try your code here
// put your toast here
}
@Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return null;
// try you code here
}
}
now ,to cancel repeating alarm use below code
alarmManagerfiftyfive.cancel(pendingIntentfiftyfive);
now ,to cancel repeating alarm from other activity use below code
here MainActivity is your activity where you put your repeating alarm code.
MainActivity.alarmManagerfiftyfive.cancel(MainActivity.pendingIntentfiftyfive);
No comments:
Post a Comment
Comments