我想每天在特定时间(t1,t2,t3,t4,t5)发出5条通知。我必须每天从JSON内部获取这些时间变量,因为它们每天都是不同的值。即使Android应用程序在后台,也必须触发该通知。
MyWorker类别
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条通知,或者我该如何解决。非常感谢您的宝贵时间。谢谢!
================================================ ====================
我也经过一天的尝试,但无法正常工作。组合服务,BroadcastReceiver和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;
}
}
和BroadcastReceiver
公共类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与setExact()
一起使用。
个人,我会研究在适当的时候使用从服务器发送到应用程序的setExact()
的选项。这对用户而言更加省电,并且由于电池优化,您对现代Android的约束也更少了。