Ich bin neu für Android-Entwickler und ich habe ein Problem mit meiner App. Ich habe eine benutzerdefinierte BroadcastReceiver
erstellt, aber wenn ich meine App starte, wird meine Benachrichtigung automatisch ausgeführt. Ich möchte nicht, dass diese Benachrichtigung ausgeführt wird, bis mein AlarmManager-Zeitplan startet.Benachrichtigung ist Trigger nach dem Start automatisch in Android
Ich brauche wirklich Hilfe, denn diese App ist meine Hausarbeit.
Hier sind meine Codes:
Benachrichtigung Empfänger:
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);
}
}
}
Teil meiner 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 (wenn der Benutzer das Gerät neu startet, wird diese Klasse den Alarm Umplanungen)
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);
}
}
}
Danke für Ihre Hilfe!
Hat meine Antwort für Sie funktioniert? Überlegen Sie, ob Sie es akzeptieren, wenn ja – Marat