Tuesday, August 26, 2014

Code snippet for Setting Alarm using fragements

           //Main Activity

            public static final String FRAGTAG = "RepeatingAlarmFragment";

            if (getSupportFragmentManager().findFragmentByTag(FRAGTAG) == null ) {
                        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
                        RepeatingAlarmFragment fragment = new RepeatingAlarmFragment();
                        transaction.add(fragment, FRAGTAG);
                        transaction.commit();
            }



            //RepeatingAlarmFragment - setting intent and alarm

            Intent intent = new Intent(getActivity(), MainActivity.class);
            intent.setAction(Intent.ACTION_MAIN);
            intent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
  
            PendingIntent pendingIntent = PendingIntent.getActivity(getActivity(), REQUEST_CODE,
                    intent, 0);

            int alarmType = AlarmManager.ELAPSED_REALTIME;
            final int FIFTEEN_SEC_MILLIS = 15000;

            AlarmManager alarmManager = (AlarmManager)
                    getActivity().getSystemService(getActivity().ALARM_SERVICE);

            alarmManager.setRepeating(alarmType, SystemClock.elapsedRealtime() + FIFTEEN_SEC_MILLIS,
                    FIFTEEN_SEC_MILLIS, pendingIntent);

No comments:

Post a Comment