Я хочу запускать 5 уведомлений каждый день в указанное c время (t1, t2, t3, t4, t5). Эти переменные времени я должен брать изнутри JSON каждый день, потому что каждый день они имеют разные значения. Уведомление должно быть запущено, даже если приложение Android находится в фоновом режиме.
MyWorker Class
public Result doWork() {
sendNotification();
return Result.SUCCESS;
}
public void sendNotification() {
NotificationManager notificationManager = (NotificationManager) getApplicationContext().getSystemService(Context.NOTIFICATION_SERVICE);
//If on Oreo then notification required a notification channel.
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
NotificationChannel channel = new NotificationChannel("default", "Default", NotificationManager.IMPORTANCE_DEFAULT);
notificationManager.createNotificationChannel(channel);
}
NotificationCompat.Builder notification = new NotificationCompat.Builder(getApplicationContext(), "default")
.setContentTitle("Hello")
.setContentText("there")
.setSmallIcon(R.mipmap.ic_launcher);
notificationManager.notify(1, notification.build());
}
My LoginActivity
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
Window w = getWindow();
w.addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
w.addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
}
btnLogin = findViewById(R.id.login_button);
mWorkManager = WorkManager.getInstance();
mRequest2 = new PeriodicWorkRequest.Builder(MyWorker.class,15, TimeUnit.MINUTES).build();
btnLogin.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mWorkManager.enqueue(mRequest2);
}
});
}
Мне нужна помощь, чтобы уточнить. Должен ли я использовать двух рабочих, один для получения данных из json каждый день в определенное время c и после этого для запуска 5 уведомлений с указанным таймером c для каждого или как я могу решить это. Я очень ценю ваше время. Спасибо!
============================================== ===========================
Я тоже пробовал это через день, но не работает. Комбинированный сервис, BroadcastReceiver и AlarmManager.
publi c Класс MyService расширяет сервис {
@Override
public void onCreate() {
super.onCreate();
}
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
Intent intent1 = new Intent(this, ShortTimeEntryReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 1, intent1,
PendingIntent.FLAG_UPDATE_CURRENT);
Calendar c = Calendar.getInstance();
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, c.getTimeInMillis(),AlarmManager.INTERVAL_FIFTEEN_MINUTES,
pendingIntent);
return START_STICKY;
}
}
и BroadcastReceiver
publi c Класс ShortTimeEntryReceiver расширяет BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
sendNotification(context);
context.startService(new Intent(context,MyService.class));
}
public void sendNotification(Context context) {
NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
NotificationChannel channel = new NotificationChannel("default", "Default", NotificationManager.IMPORTANCE_DEFAULT);
notificationManager.createNotificationChannel(channel);
}
NotificationCompat.Builder notification = new NotificationCompat.Builder(context, "default")
.setContentTitle("Hello")
.setContentText("there")
.setSmallIcon(R.mipmap.ic_launcher);
notificationManager.notify(1, notification.build());
}
}