Java 每天发出多次火灾通知
我想每天在特定时间发出5个通知(t1、t2、t3、t4、t5)。我必须每天从JSON内部获取这些时间变量,因为它们每天都是不同的值。即使Android应用程序处于后台,也必须触发通知 我的工人阶级Java 每天发出多次火灾通知,java,android,android-workmanager,Java,Android,Android Workmanager,我想每天在特定时间发出5个通知(t1、t2、t3、t4、t5)。我必须每天从JSON内部获取这些时间变量,因为它们每天都是不同的值。即使Android应用程序处于后台,也必须触发通知 我的工人阶级 public Result doWork() { sendNotification(); return Result.SUCCESS; } public void sendNotification() { NotificationManager notificationMan
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());
}
我的后勤活动
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获取数据,然后使用特定的计时器发出5个通知,或者我如何解决这个问题。我真的很感谢你抽出时间。谢谢大家!
====================================================================
一天后我也试过了,但没用。组合服务、广播接收器和AlarmManager
公共类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;
}
}
广播接收机
公共类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());
}
}WorkManager不保证在准确的时间运行您的工作人员。您需要将AlarmManager与一起使用 就我个人而言,我会研究在正确的时间使用从服务器发送到应用程序的选项。
这对用户的电池友好得多,而且由于电池优化,现代安卓系统对您的限制也小得多。WorkManager不能保证在准确的时间运行worker。您需要将AlarmManager与一起使用 就我个人而言,我会研究在正确的时间使用从服务器发送到应用程序的选项。
这对用户来说电池友好得多,而且由于电池优化,现代安卓系统的限制也小得多