Skip to content

Instantly share code, notes, and snippets.

@Bramengton
Created February 16, 2018 21:58
Show Gist options
  • Save Bramengton/c48a0256c265197371c7d7529e65be40 to your computer and use it in GitHub Desktop.
Save Bramengton/c48a0256c265197371c7d7529e65be40 to your computer and use it in GitHub Desktop.
Services Notification
пример старта запущенной службы на переднем плане:
public class MyActivity extends Activity{
private void commandStart() {
if (!mServiceIsStarted) {
moveToStartedState();
return;
}
if (mExecutor == null) {
// Start Executor task in Background Thread.
}
}
}
Вот код создания постоянного уведомления в версиях
до Android O
@TargetApi(25)
public static class PreO {
public static void createNotification(Service context) {
// Create Pending Intents.
PendingIntent piLaunchMainActivity =
getLaunchActivityPI(context);
PendingIntent piStopService = getStopServicePI(context);
// Action to stop the service.
NotificationCompat.Action stopAction =
new NotificationCompat.Action.Builder(
STOP_ACTION_ICON,
getNotificationStopActionText(context),
piStopService)
.build();
// Create a notification.
Notification mNotification =
new NotificationCompat.Builder(context)
.setContentTitle(getNotificationTitle(context))
.setContentText(getNotificationContent(context))
.setSmallIcon(SMALL_ICON)
.setContentIntent(piLaunchMainActivity)
.addAction(stopAction)
.setStyle(new NotificationCompat.BigTextStyle())
.build();
context.startForeground(
ONGOING_NOTIFICATION_ID, mNotification);
}
}
в Android O, через NotificationChannel
@TargetApi(26)
public static class O {
public static final String CHANNEL_ID =
String.valueOf(getRandomNumber());
public static void createNotification(Service context) {
String channelId = createChannel(context);
Notification notification =
buildNotification(context, channelId);
context.startForeground(
ONGOING_NOTIFICATION_ID, notification);
}
private static Notification buildNotification(
Service context, String channelId) {
// Create Pending Intents.
PendingIntent piLaunchMainActivity =
getLaunchActivityPI(context);
PendingIntent piStopService =
getStopServicePI(context);
// Action to stop the service.
Notification.Action stopAction =
new Notification.Action.Builder(
STOP_ACTION_ICON,
getNotificationStopActionText(context),
piStopService)
.build();
// Create a notification.
return new Notification.Builder(context, channelId)
.setContentTitle(getNotificationTitle(context))
.setContentText(getNotificationContent(context))
.setSmallIcon(SMALL_ICON)
.setContentIntent(piLaunchMainActivity)
.setActions(stopAction)
.setStyle(new Notification.BigTextStyle())
.build();
}
@NonNull
private static String createChannel(Service ctx) {
// Create a channel.
NotificationManager notificationManager =
(NotificationManager)
ctx.getSystemService(Context.NOTIFICATION_SERVICE);
CharSequence channelName = "Playback channel";
int importance = NotificationManager.IMPORTANCE_DEFAULT;
NotificationChannel notificationChannel =
new NotificationChannel(
CHANNEL_ID, channelName, importance);
notificationManager.createNotificationChannel(
notificationChannel);
return CHANNEL_ID;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment