0
I am porgramando for android for some 3 months, I’m still having some difficulties, I have an app that makes a requisicao(service) online using google firebase, and validates a date in string according to the validation, it creates a notification to the user. At first everything works well... but if I clean the app from the memory of the phone, and wait a few minutes it stops working, I can not see what error is, because if I take from the memory of the emulator, errors no longer appear in the lobby of android studio for me. I realized on my personal mobile phone, that the service does not stop running until I quit... but when I clean the app from memory, if you observe in the processes, it appears as "RESTARTING" and soon after the restarting, it stops working, it does it 2x and then the service stops running too.
This is the code of the service... which also starts with cell phone boot, or when the user asks in the app to use the service, I put validations not to start it if it is already running (I think it is all right)
public class ShowNotificationService extends Service {
public static AlarmManager alarmManager;
public static PendingIntent pendingIntent;
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public void onCreate() {
super.onCreate();
Calendar cal = Calendar.getInstance();
java.util.Date hora = cal.getTime();
System.out.println("####################"+hora);
boolean alarmeAtivo = (PendingIntent.getBroadcast(this, 22222222, new Intent(this, CriarNotificacao.class), PendingIntent.FLAG_NO_CREATE) == null);
if(alarmeAtivo){
System.out.println(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> TRUUUUUUUUUUUUUUUUE");
AgendarNotificacao(hora, 22222222, "NOVAS", "hora: "+cal.getTime());
}else{
System.out.println(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> FAAAAAAAAAAAAALSE");
}
}
public void AgendarNotificacao(java.util.Date data, int id, String titulo, String conteudo) {
System.out.println("------------------------------------------------------AGENDAR NOTIFICACAO");
// Obtém um novo calendário e define a data para a data da notificação
Calendar calendar = Calendar.getInstance();
calendar.setTime(data);
// Obtém um alarm manager
alarmManager = (AlarmManager) getApplicationContext().getSystemService(getBaseContext().ALARM_SERVICE);
// Prepare the intent which should be launched at the date
Intent intent = new Intent(this, CriarNotificacao.class);
intent.putExtra("id", String.valueOf(id));
intent.putExtra("titulo", titulo);
intent.putExtra("conteudo", conteudo);
// Obtém o pending intent
pendingIntent = PendingIntent.getBroadcast(getApplicationContext(), id, intent, PendingIntent.FLAG_UPDATE_CURRENT);
// Regista o alerta no sistema.
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),1800000, pendingIntent);
}
}
here is the part that validates the online date in firebase...
public class CriarNotificacao extends BroadcastReceiver {
SimpleDateFormat dateformat = new SimpleDateFormat("EE MMM dd HH:mm:ss z yyyy",Locale.ENGLISH);
//DateFormat f2 = DateFormat.getDateInstance(DateFormat.FULL, brasil);
Date dateAtual = new Date();
Date dataNotificacao= new Date();
@Override
public void onReceive(final Context context, final Intent paramIntent) {
Firebase firebase = LibraryClass.getFirebase().child("notificacao/");
firebase.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot tasksSnapshot) {
try {
dataNotificacao = dateformat.parse(tasksSnapshot.child("tempo").getValue().toString());
} catch (ParseException e) {
Toast.makeText(context,"Problema: "+e+" "+dataNotificacao,Toast.LENGTH_LONG).show();
System.out.println("ERROOOOOOOOOOOOOOOOOOOOO"+e+" "+dataNotificacao);
}
dataNotificacao = new Date(dataNotificacao.getTime() + 30 * 60 * 1000);
int diferenca = dateAtual.compareTo(dataNotificacao);
if (diferenca<0){
System.out.println("-------------------------------------CRIAR NOTIFICACAO");
Bundle extras = paramIntent.getExtras();
int id = Integer.parseInt(extras.getString("id"));
String titulo = extras.getString("titulo");
String conteudo = extras.getString("conteudo");
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context)
.setContentTitle(titulo)
.setContentText(tasksSnapshot.child("msg").getValue().toString())
.setAutoCancel(true);
TaskStackBuilder stackBuilder = TaskStackBuilder.create(context);
mBuilder.setSmallIcon(R.drawable.ic_merc);
Intent resultIntent = new Intent(context, MainActivity.class);
stackBuilder.addParentStack(MainActivity.class);
stackBuilder.addNextIntent(resultIntent);
PendingIntent resultPendingIntent =
stackBuilder.getPendingIntent(
0,
PendingIntent.FLAG_CANCEL_CURRENT
);
mBuilder.setContentIntent(resultPendingIntent);
NotificationManager mNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
Vibrator vibrator = (Vibrator) context.getSystemService(Context.VIBRATOR_SERVICE);
vibrator.vibrate(500);
mNotificationManager.notify(id, mBuilder.build());
}
}
@Override
public void onCancelled(FirebaseError firebaseError) {
Toast.makeText(context,"Falhou: " + firebaseError.getMessage(),Toast.LENGTH_LONG).show();
}
});
}
}
seems to clear from the error memory in this part Create notification.clas.. Firebase firebase = Libraryclass.getFirebase(). Child("notification/"); there is some Imitation for internet request Oor something like that?
– Techio