以下是我如何在应用程序处于后台或甚至使用前台服务杀死时触发我的警报。
// Aware user about the foreground service
private fun setForeGroundNotification() {
val intent = Intent(this, AlarmForegroundService::class.java)
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
startForegroundService(intent)
} else {
startService(intent)
public void onCreate() {
super.onCreate();
final String CHANNELID = "Foreground Service ID";
NotificationChannel channel;
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
channel = new NotificationChannel(
CHANNELID,
CHANNELID,
NotificationManager.IMPORTANCE_LOW
getSystemService(NotificationManager.class).createNotificationChannel(channel);
Notification.Builder notification = new Notification.Builder(this, CHANNELID)
.setContentTitle("App is running in background to check alarm")
.setContentText("Checking Alarm..")
.setAutoCancel(false)
.setSmallIcon(R.mipmap.logo);
startForeground(1, notification.build());
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
int hours = intent.getIntExtra("hours", 0);
int minutes = intent.getIntExtra("minutes", 0);
//Firing alarm at selected wake up time
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY, hours);
calendar.set(Calendar.MINUTE, minutes);
//To prevent alarm trigger for past time.
if (calendar.before(Calendar.getInstance())) {
calendar.add(Calendar.DATE, 1);
Intent intent1 = new Intent(this, AlarmReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(
this,
intent1,
PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_IMMUTABLE
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), (1000 * 60 * 60 * 24), pendingIntent);
return super.onStartCommand(intent, flags, startId);
class AlarmReceiver : BroadcastReceiver() {
override fun onReceive(context: Context?, intent: Intent?) {
val intent = Intent(context, AlarmIntentService::class.java)
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
context!!.startForegroundService(intent)
} else {
context!!.startService(intent)
public class AlarmIntentService extends Service {
@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
@Override
public void onCreate() {
super.onCreate();
NotificationChannel channel = null;
int notificationId = 123;
long[] vibPattern = {1000, 1000, 1000, 1000};
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
channel = new NotificationChannel("channel2",
"Sleep Alarm",
NotificationManager.IMPORTANCE_HIGH);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
channel.setVibrationPattern(vibPattern);
NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
manager.createNotificationChannel(channel);
//Creating the notification object
NotificationCompat.Builder notification = new NotificationCompat.Builder(this, "channel2");
notification.setSmallIcon(R.drawable.ic_bell);
notification.setContentTitle("Wake up Alarm!!");
notification.setContentText("It's time to get up");
notification.setAutoCancel(true);
notification.setSound(null);
//notification.build().flags |= Notification.FLAG_AUTO_CANCEL;
//Action button handling
Intent onDismiss = new Intent(this, DismissReceiver.class);
onDismiss.putExtra("NOTIFICATION_ID", notificationId);
Intent onSnooze = new Intent(this, SnoozeReceiver.class);
SharedPreferences sh = getSharedPreferences("MySharedPref", Context.MODE_PRIVATE);
int snooze = sh.getInt("snooze", 0);
PendingIntent dismissPendingIntent = PendingIntent.getBroadcast(this, 1, onDismiss, PendingIntent.FLAG_IMMUTABLE);
if (snooze == 0) {
notification.setContentIntent(dismissPendingIntent);
notification.addAction(R.drawable.ic_cross, "Dismiss", dismissPendingIntent);
} else {
PendingIntent snoozePendingIntent = PendingIntent.getBroadcast(this, 1, onSnooze, PendingIntent.FLAG_IMMUTABLE);
notification.setContentIntent(dismissPendingIntent);
notification.setContentIntent(snoozePendingIntent);
notification.addAction(R.drawable.ic_cross, "Dismiss", dismissPendingIntent);
notification.addAction(R.drawable.ic_cross, "Snooze", snoozePendingIntent);
//To trigger the chosen alarm ringtone
Intent startIntent = new Intent(this, RingtonePlayingService.class);
this.startService(startIntent);