Hello. I'm making an android app that notifies me when I have to turn in a school assignment, and if it's the day of the assignment or a day before, I want to be notified at certain times.
The home screen is where the service starts, where I click on "Tasks" to go to Task Activity.
public class DashboardActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.dashboard_activity);
startService(new Intent(this, ShowNotificationService.class));
}
}
But I would like the service to be started when the phone is switched on (like Whatsapp shows message notifications when we turn on the phone).
Code of showNotificationService.class
public class ShowNotificationService extends Service {
private EducInDAO dao;
private DatabaseHelper helper;
private Context context;
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public void onCreate() {
super.onCreate();
helper = new DatabaseHelper(this);
try {
tarefas();
} catch (ParseException e) {
e.printStackTrace();
}
}
public void tarefas() throws ParseException {
SQLiteDatabase db = helper.getReadableDatabase();
Cursor cursor = db.rawQuery("SELECT * FROM tarefas", null);
if (cursor == null) {
} else {
while(cursor.moveToNext()) {
SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
Date dataEntrega = new Date(cursor.getLong(2));
Date dateNow = new Date();
String dataEntregaFormatada = sdf.format(dataEntrega);
String dateNowFormatada = sdf.format(dataEntrega);
GregorianCalendar calendar = new GregorianCalendar();
int hora = calendar.get(Calendar.HOUR_OF_DAY);
if ((dateNowFormatada.compareTo(dataEntregaFormatada) == 0) || (dateNowFormatada.compareTo(dataEntregaFormatada) == 1)) {
int id = cursor.getInt(0);
String nomeMateria = cursor.getString(1);
String title = "Eae, já fez?";
if ((dateNowFormatada.compareTo(dataEntregaFormatada) == 0)) {
if ((hora == 0 || hora == 7 || hora == 12 || hora == 23)) {
if ((hora == 0 || hora == 7 || hora == 12)) {
String contentText = "Você tem tarefa de " + nomeMateria + " para hoje!";
Vibrator v = (Vibrator) this.getSystemService(Context.VIBRATOR_SERVICE);
v.vibrate(1000);
showNotification(title, contentText, id, 1);
}
}
} else if ((dateNowFormatada.compareTo(dataEntregaFormatada) == 1)) {
if ((hora == 15 || hora == 18 || hora == 22)) {
String contentText = "Você tem tarefa de " + nomeMateria + " para amanhã!";
Vibrator v = (Vibrator) this.getSystemService(Context.VIBRATOR_SERVICE);
v.vibrate(1000);
showNotification(title, contentText, id, 1);
}
}
}
}
}
cursor.close();
}
public void showNotification(String title, String content, int id, int tipoNotificacao) {
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this)
.setContentTitle(title)
.setContentText(content)
.setAutoCancel(true);;
TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
if (tipoNotificacao == 1) {
mBuilder.setSmallIcon(R.drawable.homework);
Intent resultIntent = new Intent(this, TarefasActivity.class);
resultIntent.putExtra(Constantes.TAREFA_ID, String.valueOf(id));
stackBuilder.addParentStack(TarefasActivity.class);
stackBuilder.addNextIntent(resultIntent);
}
PendingIntent resultPendingIntent =
stackBuilder.getPendingIntent(
0,
PendingIntent.FLAG_CANCEL_CURRENT
);
mBuilder.setContentIntent(resultPendingIntent);
NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(id, mBuilder.build());
}
}
But what happens is that it shows the notifications, for example, at 10 pm but if I take the notification, it appears again! So, it's take that shows again: /
How to solve?