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

Показать описание
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() {
}
}
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() {
}
}
Комментарии