У меня есть IntentService extends IntentService
:
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Toast.makeText(this, "service starting", Toast.LENGTH_SHORT).show();
String NOTIFICATION_CHANNEL_ID = "channel";
long pattern[] = {0, 50, 50};
NotificationManager mNotificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel notificationChannel = new NotificationChannel(NOTIFICATION_CHANNEL_ID, "Your Notifications",
NotificationManager.IMPORTANCE_HIGH);
notificationChannel.setDescription("");
notificationChannel.enableLights(true);
notificationChannel.setLightColor(Color.RED);
notificationChannel.setVibrationPattern(pattern);
notificationChannel.enableVibration(true);
mNotificationManager.createNotificationChannel(notificationChannel);
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel channel = mNotificationManager.getNotificationChannel(NOTIFICATION_CHANNEL_ID);
channel.canBypassDnd();
}
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this, NOTIFICATION_CHANNEL_ID);
notificationBuilder.setAutoCancel(true)
.setContentTitle(getString(R.string.app_name))
.setContentText("test text")
.setDefaults(Notification.DEFAULT_ALL)
.setWhen(System.currentTimeMillis())
.setSmallIcon(R.drawable.logonotification)
.setAutoCancel(true);
Intent test = new Intent(this, MainActivity.class);
TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
stackBuilder.addNextIntent(test);
PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(
0,
PendingIntent.FLAG_UPDATE_CURRENT
);
notificationBuilder.setContentIntent(resultPendingIntent);
mNotificationManager.notify(1000, notificationBuilder.build());
return super.onStartCommand(intent,flags,startId);
}
И у меня есть код для запуска этой службы в определенное время:
Intent notify = new Intent(v.getContext(), IntentService.class);
AlarmManager alarmManager = (AlarmManager) v.getContext().getSystemService(ALARM_SERVICE);
PendingIntent pendingIntent = PendingIntent.getService(v.getContext(), 0, notify, 0);
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY, 15);
calendar.set(Calendar.MINUTE, 42);
calendar.set(Calendar.SECOND, 0);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), 24*60*60*1000 , pendingIntent);
Но мне нужно немного этого,Мне нужен сервис для запуска НЕМЕДЛЕННО и уведомлений о TIMING .
Как мне запустить сервис БЕЗ lanching Notification?Мне нужно это для запуска уведомления, когда приложение закрыто.