Self-consistent theory of localization. II. Localization near the band edges