How to create foreground services and notification in your Android App?

preview_player
Показать описание
This video shows the steps to create and start your service as a foreground service in your Android App. It also shows the steps to create a notification for your App in the Android phone notification bar.

This video uses the service class created in the previous video: "How to create Service class to run a custom alarm clock in your Android?"
It is recommended to watch this previous video to understand how to create an alarm clock using a service class.

Source Code:

public class MyService extends Service {

private Integer alarmHour;
private Integer alarmMinute;
private Ringtone ringtone;
private Timer t = new Timer();

private static final String CHANNEL_ID = "MyNotificationChannelID";

@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {

try {

Notification notification = new NotificationCompat.Builder(this, CHANNEL_ID )
.setContentTitle("My Alarm clock")
.setContentIntent(pendingIntent)
.build();

startForeground(1, notification);

NotificationChannel notificationChannel = new NotificationChannel(CHANNEL_ID, "My Alarm clock Service", NotificationManager.IMPORTANCE_DEFAULT);

}
catch (Exception e){
}

@Override
public void run() {
}
else {
}

}
}, 0, 2000);

}

@Override
public void onDestroy() {
}
}
Рекомендации по теме
Комментарии
Автор

What is the difference between stopForeground() and stopSelf() ?? Do we need to call both of these methods to stop a foreground service ?? As of now i'm only using stopSelf() and it is working.

pradeepkumarreddykondreddy
Автор

I loved the tutorial. I have a question tho. How can we start an activity along with displaying a notification when the alarm goes off? I want my phone to wake up and display that activity too.

jyotikasawlani
Автор

Sir Thank you this video helped me a lot!

hurairaFurqan
Автор

Hi there, i watched your alarm tutorials and they were all really great, i am new to android studio so you've been really helpful. But there is a problem i have. The line you wrote at 12:16, requires API level 26. And when i was going to change my minimum API level to that i realized it means only %6 of the devices would be able to use the app. What should i do? Does this really mean %94 of the devices won't be able to use this feature if i use this line?

bugragundogan
Автор

Thanks for your amazing videos
I learned now how to make an Alarm
But can you next teach us how to make a foreground service that run a countdown timer ?

syrian
Автор

when creating the Intent in the mainActivity to pass into the serviceCaller, how do I pass intent if I'm calling serviceCaller from a fragment?

harchitb
Автор

Very nice videos

I would like to give a small suggestion
It's difficult to view code from mobile screen so plz increase text size of code from android studio
N try to post videos in high resolution

prathameshjadhav
Автор

thanks for this video

keep it up bro

batmantutorial
Автор

Hi, This work on api 23 and later but in api 22 and earlier it crashes the app when alarm(any work) triggered and only running in foreground. How can we achieve this in API 22 and earlier devices?

tohedul
Автор

I am creating a music player app and for notification part, i have followed your steps, but still notification is not coming?

edge
Автор

hi sir g good work keep it up ....i want to make video about how to diables another phone vibration from own phone with sms functionality sir g so helpful videos for anyone that videos must try sir g.

rashidullah
Автор

Can u please help if you want am pm in your notification wht to write

vidhiparakhiya
Автор

does it work on android 8, because i tried but nothing happened

mohammadsorour
Автор

will the alarm work after you restart the mobile

ahsanraza
Автор

sir how can i add dismiss and snooze button in this? pls help !!

ajayprajapati-nyts
Автор

Does not work if i leave the app! The service gets destroyed very quick :(((

lennard