I was having the same problem and after a long time looking for the answer and testing I managed using AlarmManager
and BroadcastReceiver
, what I needed:
I have a web system where someone registers notifications in the database, and an API that returns a json with the requests, I needed something to run with the app open or closed checking for new messages and notifying the user.
Man AndroidManifest.xml
<receiver
android:name=".Servico.BootReciever"
android:enabled="true"
android:exported="true">
<intent-filter>
<action android:name="ALARME_DISPARADO2"/>
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</receiver>
In my onCreate
of Activity
boolean alarmeativo = (PendingIntent.getBroadcast( this, 0, new Intent( "ALARME_DISPARADO2" ), PendingIntent.FLAG_NO_CREATE ) == null);
if(alarmeativo) {
Intent intent = new Intent( "ALARME_DISPARADO2" );
PendingIntent p = PendingIntent.getBroadcast( this, 0, intent, 0 );
Calendar c = Calendar.getInstance();
c.setTimeInMillis( System.currentTimeMillis() );
c.add( Calendar.SECOND, 3 );
AlarmManager alarm = (AlarmManager) getSystemService( ALARM_SERVICE );
alarm.set( AlarmManager.RTC_WAKEUP, c.getTimeInMillis(), p );
}
I check that the alarm is already created, assign the Intent
which will be executed referencing my BroadcastReceiver
, set a time to start and set the alarm RTC_WAKEUP
to perform even with the blogged device, done in BroadcastReceiver
beginning a Task, because I need to post on a web page, using recursiveness to continue running.
Man BootReciever.java
public class BootReciever extends BroadcastReceiver {
public static Usuario us;
public static Notifi not;
public static NotifiSetor notSet;
public static Agenda notAg;
public static boolean Iniciar = true;
public static boolean saida;
int x = 0;
@Override
public void onReceive(Context context, Intent intent) {
Iniciar = false;
Usuario u = new Usuario( context );
u.Select();
us = u;
Notifi n = new Notifi( context );
not = n;
NotifiSetor ns = new NotifiSetor(context );
notSet = ns;
Agenda ag = new Agenda( context );
notAg = ag;
NotificationTask us = new NotificationTask();
us.execute(context);
}
public boolean isOnline(Context context) {
boolean conectado;
try {
ConnectivityManager conectivtyManager = (ConnectivityManager) context.getSystemService( Context.CONNECTIVITY_SERVICE );
if (conectivtyManager.getActiveNetworkInfo() != null
&& conectivtyManager.getActiveNetworkInfo().isAvailable()
&& conectivtyManager.getActiveNetworkInfo().isConnected()) {
conectado = true;
} else {
conectado = false;
}
} catch (Exception e) {
conectado = false;
}
return conectado;
}
private class NotificationTask extends AsyncTask<Context, Void, Context> {
@Override
protected Context doInBackground(Context... ct) {
saida = false;
try {
Agenda a = new Agenda(notAg.getCt());
List<Agenda> list = a.SelectExibir();
if(list != null) {
for (int x = 0; x < list.size(); x++) {
long date1 = System.currentTimeMillis();
SimpleDateFormat sdf = new SimpleDateFormat("HH:mm");
String dateString = sdf.format(date1);
if(list.get(x).getExibir().equals(dateString)) {
Calendar cal = Calendar.getInstance( TimeZone.getTimeZone("GMT"));
Date currentLocalTime = cal.getTime();
DateFormat date = new SimpleDateFormat("dd-MM-yyyy");
date.setTimeZone(TimeZone.getTimeZone("GMT"));
String localTime = date.format(currentLocalTime);
NewMessageNotification.notify(ct[0], list.get(x).getTitulo(), list.get(x).getMensagem(), list.get(x).getID());
a.setID(list.get(x).getID());
a.setUltimo_Exib(localTime);
a.UpdateExibir();
}
}
}
} catch (Exception e) {
}
if(isOnline(ct[0])) {
//Log.d("",String.valueOf( x++ ));
// notificaçoes agendadas
try {
//buscar na api
// buscar no webservice
String json = "json a ser enviando para API";
HttpService servi = new HttpService();
String retorno = servi.post(json);
try {
JSONObject jsonObj = new JSONObject(retorno);
JSONArray array = jsonObj.getJSONArray("Res");
if (jsonObj.getBoolean("Response")) {
for (int i = 0; i < array.length(); i++) {
// notificacao
notAg.setID(array.getJSONObject(i).getInt("ID"));
if (!notAg.ValidaId()) {
Calendar cal = Calendar.getInstance(TimeZone.getTimeZone("GMT"));
//cal.add(Calendar.DAY_OF_MONTH, -1);
Date currentLocalTime = cal.getTime();
DateFormat date = new SimpleDateFormat("dd-MM-yyyy");
date.setTimeZone(TimeZone.getTimeZone("GMT"));
String localTime = date.format(currentLocalTime);
notAg.setTitulo(array.getJSONObject(i).getString("TITULO"));
notAg.setMensagem(array.getJSONObject(i).getString("MENSAGEM"));
notAg.setCadastro(array.getJSONObject(i).getString("DATA_CAD"));
notAg.setExibir(array.getJSONObject(i).getString("REPETIR"));
notAg.setUltimo_Exib(localTime);
notAg.setStatus(array.getJSONObject(i).getInt("STATUS"));
notAg.Insert();
} else {
notAg.setTitulo(array.getJSONObject(i).getString("TITULO"));
notAg.setMensagem(array.getJSONObject(i).getString("MENSAGEM"));
notAg.setCadastro(array.getJSONObject(i).getString("DATA_CAD"));
notAg.setExibir(array.getJSONObject(i).getString("REPETIR"));
notAg.setStatus(array.getJSONObject(i).getInt("STATUS"));
notAg.Update();
}
}
}
} catch (Exception e) {
}
} catch (Exception e) {
// Restore interrupt status.
//Thread.currentThread().interrupt();
}
// notificaçoes geral equipe
try {
//buscar na api
// buscar no webservice
String json = "json a ser enviando para API";
HttpService servi = new HttpService();
String retorno = servi.post(json);
try {
JSONObject jsonObj = new JSONObject(retorno);
JSONArray array = jsonObj.getJSONArray("Res");
if (jsonObj.getBoolean("Response")) {
for (int i = 0; i < array.length(); i++) {
// notificacao
not.setID(array.getJSONObject(i).getInt("ID"));
if (!not.ValidaId()) {
not.setTitulo(array.getJSONObject(i).getString("TITULO"));
not.setMensagem(array.getJSONObject(i).getString("MENSAGEM"));
not.setData(array.getJSONObject(i).getString("DATA_CAD"));
not.setStatus(array.getJSONObject(i).getInt("STATUS"));
not.Insert();
NewMessageNotification.notify(ct[0], not.getTitulo(), not.getMensagem(), not.getID());
}
}
}
} catch (Exception e) {
}
} catch (Exception e) {
// Restore interrupt status.
//Thread.currentThread().interrupt();
}
// notificaçoes geral setor
try {
//buscar na api
// buscar no webservice
String json = "json a ser enviando para API";
HttpService servi = new HttpService();
String retorno = servi.post(json);
try {
JSONObject jsonObj = new JSONObject(retorno);
JSONArray array = jsonObj.getJSONArray("Res");
if (jsonObj.getBoolean("Response")) {
for (int i = 0; i < array.length(); i++) {
// notificacao
notSet.setID(array.getJSONObject(i).getInt("ID"));
if (!notSet.ValidaId()) {
notSet.setTitulo(array.getJSONObject(i).getString("TITULO"));
notSet.setMensagem(array.getJSONObject(i).getString("MENSAGEM"));
notSet.setData(array.getJSONObject(i).getString("DATA_CAD"));
notSet.setStatus(array.getJSONObject(i).getInt("STATUS"));
notSet.Insert();
NewMessageNotification.notify(ct[0], notSet.getTitulo(), notSet.getMensagem(), notSet.getID());
}
}
}
} catch (Exception e) {
}
// exibir
} catch (Exception e) {
// Restore interrupt status.
//Thread.currentThread().interrupt();
}
}
try {
// recursividade para continuar executando
doInBackground( ct[0] );
} catch(Exception e) {
}
return ct[0];
}
@Override
protected void onPostExecute(Context result) {
// Após a inserção da nota, vamos mostrar um Toast ao usuário
saida = true;
Iniciar = true;
doInBackground(result);
}
}
}
Upshot
I/art: Increasing code cache capacity to 128KB
D/14:46:36: -> Iniciado
D/14:46:41: -> Iniciado
D/14:46:42: -> Iniciado
D/14:46:44: -> Iniciado
D/14:46:45: -> Iniciado
D/14:46:46: -> Iniciado
D/14:46:48: -> Iniciado
D/14:46:49: -> Iniciado
D/14:46:50: -> Iniciado
D/14:46:52: -> Iniciado
D/14:46:53: -> Iniciado
D/14:46:54: -> Iniciado
D/14:46:56: -> Iniciado
D/14:46:56: -> Iniciado
D/14:46:57: -> Iniciado
D/14:46:58: -> Iniciado
D/14:46:59: -> Iniciado
D/14:47:00: -> Iniciado
D/14:47:01: -> Iniciado
D/14:47:03: -> Iniciado
D/14:47:04: -> Iniciado
D/14:47:07: -> Iniciado
D/14:47:09: -> Iniciado
D/14:47:11: -> Iniciado
D/14:47:12: -> Iniciado
D/14:47:15: -> Iniciado
D/14:47:16: -> Iniciado
D/14:47:17: -> Iniciado
D/14:47:18: -> Iniciado
D/14:47:19: -> Iniciado
D/14:47:20: -> Iniciado
D/14:47:21: -> Iniciado
D/14:47:22: -> Iniciado
D/14:47:23: -> Iniciado
D/14:47:24: -> Iniciado
D/14:47:25: -> Iniciado
D/14:47:26: -> Iniciado
D/14:47:27: -> Iniciado
D/14:47:28: -> Iniciado
D/14:47:30: -> Iniciado
D/14:47:32: -> Iniciado
D/14:47:33: -> Iniciado
D/14:47:34: -> Iniciado
D/14:47:35: -> Iniciado
D/14:47:36: -> Iniciado
D/14:47:37: -> Iniciado
D/14:47:38: -> Iniciado
D/14:47:38: -> Iniciado
D/14:47:39: -> Iniciado
D/14:47:40: -> Iniciado
D/14:47:41: -> Iniciado
D/14:47:42: -> Iniciado
D/14:47:43: -> Iniciado
D/14:47:43: -> Iniciado
D/14:47:44: -> Iniciado
D/14:47:45: -> Iniciado
D/14:47:46: -> Iniciado
D/14:47:46: -> Iniciado
D/14:47:47: -> Iniciado
D/14:47:48: -> Iniciado
D/14:47:49: -> Iniciado
D/14:47:50: -> Iniciado
I/art: Background sticky concurrent mark sweep GC freed 3085(212KB) AllocSpace objects, 2(40KB) LOS objects, 1% free, 28MB/28MB, paused 5.805ms total 16.525ms
D/14:47:50: -> Iniciado
D/14:47:51: -> Iniciado
D/14:47:52: -> Iniciado
D/14:47:53: -> Iniciado
D/14:47:54: -> Iniciado
D/14:47:55: -> Iniciado
D/14:47:56: -> Iniciado
D/14:47:57: -> Iniciado
D/14:47:58: -> Iniciado
D/14:47:58: -> Iniciado
D/14:47:59: -> Iniciado
D/14:48:00: -> Iniciado
D/14:48:01: -> Iniciado
D/14:48:02: -> Iniciado
D/14:48:02: -> Iniciado
D/14:48:03: -> Iniciado
D/14:48:04: -> Iniciado
D/14:48:05: -> Iniciado
D/14:48:06: -> Iniciado
D/14:48:07: -> Iniciado
D/14:48:09: -> Iniciado
D/14:48:10: -> Iniciado
D/14:48:11: -> Iniciado
D/14:48:12: -> Iniciado
D/14:48:13: -> Iniciado
D/14:48:13: -> Iniciado
D/14:48:14: -> Iniciado
D/14:48:15: -> Iniciado
D/14:48:16: -> Iniciado
D/14:48:17: -> Iniciado
D/14:48:18: -> Iniciado
D/14:48:19: -> Iniciado
D/14:48:20: -> Iniciado
D/14:48:21: -> Iniciado
D/14:48:22: -> Iniciado
D/14:48:22: -> Iniciado
Application terminated.
And testing on my phone worked properly and with little time delay to view notifications with the app closed and open, you can also call a Service or a Intetservice in the onReceive
.
The guy asks a question and does not come back to mark as solved and abandons the stackoverflow....
– Lollipop
http://www.thiengo.com.br/service-android-understandingand using. In your case see this material, it teaches to resume the service using broadcast... now if you want to save values or something of the kind after the reboot, have to use Sqlite storing there everything you need for when to run the application again
– jonathan bersot