Я реализовал некоторые уведомления, которые запускаются с указанием c даты и времени. Мой код работает, если телефон не перезагружается после установки приложения, но если я перезагружаю телефон, уведомление не срабатывает. Я перепробовал много методов, предложенных другими разработчиками, но ничего не изменилось. Можно ли автоматически перезапустить службу уведомлений при перезагрузке системы?
BootService. java
package com.example.is2_app.utility;
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.os.IBinder;
import androidx.annotation.NonNull;
import androidx.core.app.JobIntentService;
import java.util.Calendar;
import static java.util.Calendar.APRIL;
public class BootService extends JobIntentService {
@Override
public void onCreate() {
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
intent = new Intent(this, ReminderBroadcast.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, intent, 0);
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
calendar.set(Calendar.DAY_OF_WEEK_IN_MONTH, 1);
calendar.add(Calendar.DAY_OF_YEAR,-1);
calendar.set(Calendar.HOUR_OF_DAY, 23);
calendar.set(Calendar.MINUTE, 9);
calendar.set(Calendar.SECOND, 40);
if(calendar.getTimeInMillis()>System.currentTimeMillis()) {
alarmManager.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent);
}
return START_NOT_STICKY;
}
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
protected void onHandleWork(@NonNull Intent intent) {
}
}
ReminderBroadcast. java
package com.example.is2_app.utility;
import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import androidx.core.app.NotificationCompat;
import com.example.is2_app.MainActivity;
import com.example.is2_app.R;
public class ReminderBroadcast extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
showNotification(context);
}
void showNotification(Context context) {
String CHANNEL_ID = "your_name";// The id of the channel.
CharSequence name = context.getResources().getString(R.string.app_name);// The user-visible name of the channel.
NotificationCompat.Builder mBuilder;
Intent notificationIntent = new Intent(context, MainActivity.class);
long when = System.currentTimeMillis();
Bundle bundle = new Bundle();
notificationIntent.putExtras(bundle);
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_MULTIPLE_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
context.startService(notificationIntent);
PendingIntent contentIntent = PendingIntent.getActivity(context, 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
NotificationManager mNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
if (android.os.Build.VERSION.SDK_INT >= 26) {
NotificationChannel mChannel = new NotificationChannel(CHANNEL_ID, name, NotificationManager.IMPORTANCE_HIGH);
mNotificationManager.createNotificationChannel(mChannel);
mBuilder = new NotificationCompat.Builder(context)
.setSmallIcon(R.mipmap.ic_launcher_candida)
.setLights(Color.RED, 300, 300)
.setChannelId(CHANNEL_ID);
} else {
mBuilder = new NotificationCompat.Builder(context)
.setSmallIcon(R.mipmap.ic_launcher_candida)
.setPriority(Notification.PRIORITY_DEFAULT);
}
mBuilder.setContentTitle(context.getString(R.string.spazzatura));
mBuilder.setContentIntent(contentIntent);
mBuilder.setStyle(new NotificationCompat.BigTextStyle().bigText(context.getString(R.string.spazzatura_residuo)));
mBuilder.setContentText(context.getString(R.string.spazzatura_residuo));
mBuilder.setAutoCancel(true).setWhen(when);
mNotificationManager.notify(1, mBuilder.build());
}
}
AndroidManifest. xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.is2_app">
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<application
android:name=".global.Global"
android:allowBackup="true"
android:configChanges="locale|orientation|keyboardHidden|screenSize"
android:icon="@mipmap/ic_launcher_candida"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_candida_round"
android:supportsRtl="true"
android:theme="@style/AppTheme"
android:usesCleartextTraffic="true">
<service
android:name=".utility.BootService"
android:enabled="true"
android:exported="true" />
<activity
android:name=".MainActivity"
android:label="@string/app_name"
android:theme="@style/AppTheme.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver android:name=".utility.ReminderBroadcast"
android:permission = "android.permission.RECEIVE_BOOT_COMPLETED">
<intent-filter>
<action android:name="android.intent.action.ACTION_BOOT_COMPLETED" />
<category android:name="android.intent.category.DEFAULT" />
<action android:name="android.intent.action.QUICKBOOT_POWERON"/>
</intent-filter>
</receiver>
</application>
</manifest>