Первые 2 тревоги, обработанные AlarmManager в MainActivity, работают, как и ожидалось, однако две тревоги, обработанные AlarmManager в JobScheduler, должны срабатывать каждые 10 секунд, чего, похоже, нет. У меня нет ничего в logcat, чтобы указать, где я должен искать. Любые идеи о том, где я могу пойти не так?
MainActvity:
public class MainActivity extends AppCompatActivity {
private static final String TAG = "Notifications";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Calendar c = Calendar.getInstance();
Integer currentHour = c.get(Calendar.HOUR_OF_DAY);
Integer currentMinute = c.get(Calendar.MINUTE);
Integer currentSecond = c.get(Calendar.SECOND);
Toast.makeText(this, String.valueOf(currentSecond), Toast.LENGTH_SHORT).show();
setAlarm("Gym", currentHour,currentMinute,currentSecond + 3,1);
setAlarm("Swimming", currentHour,currentMinute,currentSecond + 6,2);
scheduleJob();
}
public void setAlarm(String activity, Integer hour, Integer minute, Integer second, Integer requestCode) {
Calendar currentTime = Calendar.getInstance();
Calendar c = Calendar.getInstance();
c.set(Calendar.HOUR_OF_DAY, hour);
c.set(Calendar.MINUTE, minute);
c.set(Calendar.SECOND, second);
if (c.after(currentTime)) {
AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(getApplicationContext(), AlarmReceiver.class);
intent.putExtra("Activity", activity);
PendingIntent pendingIntent = PendingIntent.getBroadcast(getApplicationContext(), requestCode, intent, 0);
alarmManager.setExact(AlarmManager.RTC_WAKEUP, c.getTimeInMillis(), pendingIntent);
// alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, c.getTimeInMillis(), AlarmManager.INTERVAL_DAY, pendingIntent);
}
}
public void scheduleJob() {
ComponentName componentName = new ComponentName(this, NotificationService.class);
JobInfo jobInfo = new JobInfo.Builder(123, componentName)
.setRequiresCharging(true)
.setRequiredNetworkType(JobInfo.NETWORK_TYPE_UNMETERED)
.setPersisted(true)
.setPeriodic(1000 * 10)
.build();
JobScheduler jobScheduler = (JobScheduler) getSystemService(JOB_SCHEDULER_SERVICE);
int resultCode = jobScheduler.schedule(jobInfo);
if (resultCode == JobScheduler.RESULT_SUCCESS) {
Log.d(TAG, "Job scheduled");
} else {
Log.d(TAG, "Job scheduling failed");
}
}
}
JobService:
public class NotificationService extends JobService {
@Override
public boolean onStartJob(JobParameters parameters) {
doBackgroundWork(parameters);
return true;
}
private void doBackgroundWork(final JobParameters parameters) {
new Thread(new Runnable() {
@Override
public void run() {
Calendar c = Calendar.getInstance();
Integer currentHour = c.get(Calendar.HOUR_OF_DAY);
Integer currentMinute = c.get(Calendar.MINUTE);
Integer currentSecond = c.get(Calendar.SECOND);
setAlarm("Gym", currentHour,currentMinute,currentSecond + 3,1);
setAlarm("Swimming", currentHour,currentMinute,currentSecond + 6,2);
jobFinished(parameters, false);
}
}).start();
}
@Override
public boolean onStopJob(JobParameters params) {
return false;
}
public void setAlarm(String activity, Integer hour, Integer minute, Integer second, Integer requestCode) {
Calendar currentTime = Calendar.getInstance();
Calendar c = Calendar.getInstance();
c.set(Calendar.HOUR_OF_DAY, hour);
c.set(Calendar.MINUTE, minute);
c.set(Calendar.SECOND, second);
if (c.after(currentTime)) {
AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(getApplicationContext(), AlarmReceiver.class);
intent.putExtra("Activity", activity);
PendingIntent pendingIntent = PendingIntent.getBroadcast(getApplicationContext(), requestCode, intent, 0);
alarmManager.setExact(AlarmManager.RTC_WAKEUP, c.getTimeInMillis(), pendingIntent);
// alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, c.getTimeInMillis(), AlarmManager.INTERVAL_DAY, pendingIntent);
}
}
}
BroadcastReceiver:
public class AlarmReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
String activity = intent.getExtras().getString("Activity");
NotificationHelper notificationHelper = new NotificationHelper(context);
NotificationCompat.Builder nb = notificationHelper.getChannelNotification(activity);
notificationHelper.getManager().notify(1, nb.build());
}
}
NotificationHelper :
public class NotificationHelper extends ContextWrapper {
public static final String channelID = "channelID";
public static final String channelName = "Channel";
private NotificationManager mManager;
public NotificationHelper(Context base) {
super(base);
createChannels();
}
private void createChannels() {
NotificationChannel channel = new NotificationChannel(channelID, channelName, NotificationManager.IMPORTANCE_DEFAULT);
channel.enableLights(true);
channel.enableVibration(true);
channel.setLightColor(R.color.royalBlue);
channel.setLockscreenVisibility(Notification.VISIBILITY_PRIVATE);
getManager().createNotificationChannel(channel);
}
public NotificationManager getManager() {
if (mManager == null) {
mManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
}
return mManager;
}
public NotificationCompat.Builder getChannelNotification(String activity) {
return new NotificationCompat.Builder(getApplicationContext(), channelID)
.setContentTitle(activity)
.setContentText("Come on")
.setSmallIcon(R.drawable.ic_event);
}
}
Манифест:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.app.notifications">
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver android:name="com.app.notifications.AlarmReceiver" />
<service
android:name="com.app.notifications.NotificationService"
android:permission="android.permission.BIND_JOB_SERVICE" />
</application>
</manifest>