2
私はAndroidの開発者には新しく、私のアプリには問題があります。カスタムBroadcastReceiver
を作成しましたが、アプリを起動すると自動的に通知が実行されます。私は、AlarmManagerのスケジュールが始まるまで、この通知を実行したくありません。Androidの起動時に通知が自動的に起動します
このアプリは私の論文であるため、本当に助けが必要です。
通知受信:ここ
は私のコードです私AndroidManifestの
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);
}
}
}
パート
<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