Я новичок в Android-разработчике, и у меня проблемы с моим приложением. Я создал пользовательский BroadcastReceiver
, но когда я запускаю свое приложение, это автоматически запускает мое уведомление. Я не хочу, чтобы это уведомление выполнялось до тех пор, пока не начнется расписание AlarmManager.Уведомление запускается после автоматической загрузки в Android
Мне действительно нужна помощь, потому что это приложение - моя курсовая работа.
Вот мои коды:
Уведомление Приемник:
public class NotificationReceiver extends BroadcastReceiver {
String cnh_vencida;
@Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals("ALARME_DISPAROU")) {
Uri uri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
cnh_vencida = context.getString(R.string.cnh_vencimento);
PendingIntent pi = PendingIntent.getActivity(context, 0, new Intent(context, CadastroUser.class), 0);
Notification notification = new NotificationCompat.Builder(context)
.setTicker("teste")
.setCategory(Notification.CATEGORY_ALARM)
.setSmallIcon(R.mipmap.car)
.setContentTitle("CarMaintenance")
.setContentText(cnh_vencida)
.setContentIntent(pi)
.setAutoCancel(true)
.setSound(uri)
.setPriority(Notification.PRIORITY_HIGH)
.setVisibility(Notification.VISIBILITY_PUBLIC)
.setVibrate(new long[]{1000, 1000, 1000, 1000, 1000})
.build();
NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0, notification);
}
}
}
Часть моего AndroidManifest
<receiver
android:name=".notification.AlarmReceiver"
android:enabled="false">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
<receiver
android:name=".notification.NotificationReceiver"
android:exported="false"
android:enabled="false">
<intent-filter>
<action android:name="ALARME_DISPAROU" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</receiver>
</application>
AlarmReceiver (если пользователь перезагружает устройство, этот класс будет перепланирует тревогу)
public class AlarmReceiver extends BroadcastReceiver {
String dataValidade;
Date date;
String CATEGORIA;
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals("android.intent.action.BOOT_COMPLETED")) {
recreateAlert(context);
}
}
public void recreateAlert(Context context) {
CadastroUserDAO cadastroUserDAO = new CadastroUserDAO(context);
List<CadastroUserModel> registros;
registros = cadastroUserDAO.listarTodos();
if (registros.size() > 0) {
Log.i(CATEGORIA, "Existe CNH Cadastrada");
for (int i = 0; i < registros.size(); i++) {
dataValidade = (registros.get(i).getVALIDADE_CNH());
}
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
try {
date = simpleDateFormat.parse(dataValidade);
} catch (ParseException e) {
e.printStackTrace();
}
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
calendar.add(calendar.DAY_OF_MONTH, -45);
AlarmManager alarmMgr = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
Intent alarmIntent = new Intent("ALARME_DISPAROU");
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, alarmIntent, 0);
alarmMgr.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), AlarmManager.INTERVAL_DAY * 5, pendingIntent);
}
}
}
Спасибо за помощь!
Помог ли мой ответ для вас? Подумайте о принятии, если да – Marat