I have created a small notification service for my application that lets you know if that day you have a scheduled event.
This service what it does is to consult in a small Sqlite database, if that day there is a saved note and if so it sends you a NotificationManager. This operation comes to consume up to 98Mb of RAM with the consequent elimination by Android, leaving the active service with 4.5MB but without performing the task that was planned.
The service is programmed to reload the check every minute so that when the time programmed by the user arrives for him to launch the notifications, he will do so.
I do not know if you need me to include some code to see something in particular and you can help me out with this little problem.
Thanks in advance for your help.
This is the launch of the service
private void LanzarAvisoNotificacion(){
Equipo = EquipoLayout;
EquipoJunto = EquipoJuntoLayout;
mes = MesLayout;
ServicioActivado = 1;
PrimeraNotificacion = 1;
Intent intent = new Intent(Meses.this, ServicioNotificaciones.class);
if (ServicioActivadoLayout == 0){
ToastPersonalizado miToast = new ToastPersonalizado(this, Toast.LENGTH_SHORT);
miToast.show("Activado el servicio de notificación del "+ Equipo);
}
intent.putExtra("mes",mes);
intent.putExtra("mesLetra", MesLetra);
intent.putExtra("dia", DiaLayout);
intent.putExtra("dianulo", dianulo);
intent.putExtra("diaSemana", diaSemana);
intent.putExtra("Equipo", Equipo);
intent.putExtra("EquipoJunto", EquipoJunto);
intent.putExtra("ServicioActivado", ServicioActivado);
intent.putExtra("PrimeraNotificacion", PrimeraNotificacion);
intent.putExtra("AlarmaConectadaOnOff", AlarmaConectadaOnOffLayout);
intent.putExtra("notificacionlanzada", notificacionlanzada);
PendingIntent sender = PendingIntent.getService(Meses.this, 1234567, intent, 0);
AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
am.setRepeating(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), 1000 * 59, sender);
startService(intent);
}
This is the service code
public class ServicioNotificaciones extends Service {
private int i = 1;
//private String PrimeraNotificacionServicio;
public String mesLetraServicio;
public int mesServicio;
public int diaServicio;
public int dianuloServicio;
public int diaSemanaServicio;
public String EquipoServicio;
public String EquipoJuntoServicio;
public int ServicioActivadoServicio;
public int NotificacionLanzadaServicio;
private String mesletraServicio;
private Integer mesnotificacion;
private int ContaNotificaciones;
private int PrimeraNotificacionServicio;
private int AlarmaConectadaServicio;
private String equiponotificacion;
private int HoraNotificacion;
private int MinutoNotificacion;
public Notas[] datosLista;
public ControlNotas usdbh;
private boolean isRunning = false;
private String mesnotificacionletra;
private int DiaSer;
private int MesSer;
private int MesSer1;
private String MesSerLetra;
private int hora;
private int minuto;
@Override
public void onCreate() {
isRunning = true;
}
@Override
public int onStartCommand(Intent intent, int flags, final int startId) {
final Calendar c = Calendar.getInstance();
DiaSer = c.get(Calendar.DAY_OF_MONTH);
MesSer = c.get(Calendar.MONTH);
MesSer1 = MesSer + 1;
hora = c.get(Calendar.HOUR_OF_DAY);
minuto = c.get(Calendar.MINUTE);
mesnotificacion = MesSer1;
ComprobarMesPequeño();
mesServicio = intent.getIntExtra("mes", 0);
mesLetraServicio = intent.getStringExtra("mesLetra");
diaServicio = intent.getIntExtra("dia", 0);
dianuloServicio = intent.getIntExtra("dianulo", 0);
diaSemanaServicio = intent.getIntExtra("diaSemana", 0);
EquipoServicio = intent.getStringExtra("Equipo");
EquipoJuntoServicio = intent.getStringExtra("EquipoJunto");
ServicioActivadoServicio = intent.getIntExtra("ServicioActivado", 0);
PrimeraNotificacionServicio = intent.getIntExtra("PrimeraNotificacion", 0);
AlarmaConectadaServicio = intent.getIntExtra("AlarmaConectadaOnOff", 0);
NotificacionLanzadaServicio = intent.getIntExtra("notificacionlanzada", 0);
if (ContaNotificaciones == 1) {
PrimeraNotificacionServicio = 1;
}
//Creating new thread for my service
//Always write your long running tasks in a separate thread, to avoid ANR
new Thread(new Runnable() {
@Override
public void run() {
//Abrimos la base de datos 'DBControlNotificacion' en modo escritura
ControlNotificacion cont = new ControlNotificacion(getApplicationContext(), "DBControlNotificacion.sqlite", null, 1);
SQLiteDatabase db = cont.getReadableDatabase();
String cod = "1";
Cursor c = db.rawQuery("SELECT equipo, horanotificacion, minutonotificacion FROM ControlNotificacion where codigo=" + cod , null);
if (c.moveToFirst()) {
String equ = c.getString(0);
int hora = c.getInt(1);
int minuto = c.getInt(2);
equiponotificacion = equ;
HoraNotificacion = hora;
MinutoNotificacion = minuto;
db.close();
c.close();
}
if (hora == HoraNotificacion && minuto == MinutoNotificacion){
//Abrimos la base de datos 'DBUsuarios' en modo escritura
usdbh = new ControlNotas(ServicioNotificaciones.this, "DBnotas" + equiponotificacion + ".sqlite", null, 1);
db = usdbh.getReadableDatabase();
Cursor ConsultaNotas = db.rawQuery("SELECT * FROM 'notas' " +
"WHERE diames='" + DiaSer + "'" +
"AND mesmes='" + mesnotificacionletra + "'", null);
if (ConsultaNotas.moveToFirst()) {
//Recorremos el cursor hasta que no haya más registros
datosLista = new Notas[ConsultaNotas.getCount()];
int i = 0;
do {
datosLista[i] = new Notas();
datosLista[i].setDiames(ConsultaNotas.getInt(0));
datosLista[i].setMesmes(ConsultaNotas.getString(1));
datosLista[i].setNota(ConsultaNotas.getString(2));
i++;
} while (ConsultaNotas.moveToNext());
db.close();
if (isRunning) {
// Preparamos el intent que será lanzado si la notificación es seleccionada
Uri soundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
Intent intent = new Intent(ServicioNotificaciones.this, Meses.class);
NotificacionLanzadaServicio = 1;
intent.putExtra("mes",mesServicio);
intent.putExtra("mesLetra", mesLetraServicio);
intent.putExtra("dia", diaServicio);
intent.putExtra("dianulo", dianuloServicio);
intent.putExtra("diaSemana", diaSemanaServicio);
intent.putExtra("Equipo", EquipoServicio);
intent.putExtra("EquipoJunto", EquipoJuntoServicio);
intent.putExtra("ServicioActivado", ServicioActivadoServicio);
intent.putExtra("PrimeraNotificacion", PrimeraNotificacionServicio);
intent.putExtra("AlarmaConectadaOnOff", AlarmaConectadaServicio);
intent.putExtra("notificacionlanzada", NotificacionLanzadaServicio);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK |Intent.FLAG_ACTIVITY_CLEAR_TOP |Intent.FLAG_ACTIVITY_CLEAR_TASK);
PendingIntent pIntent = PendingIntent.getActivity(ServicioNotificaciones.this, 0, intent, 0);
// Creamos la notificación.
CharSequence ticker = "Aviso de Nota";
CharSequence contentTitle = "Calendario Sevilla";
CharSequence contentText = "Tienes una nueva nota para hoy";
Notification noti = new NotificationCompat.Builder(ServicioNotificaciones.this)
.setContentIntent(pIntent)
.setTicker(ticker)
.setContentTitle(contentTitle)
.setContentText(contentText)
.setSmallIcon(R.drawable.icono_aplicacion_pequeno)
.setVibrate(new long[]{1000, 1000, 1000, 1000, 1000})
.setSound(soundUri)
.build();
NotificationManager notificationManager =
(NotificationManager) getSystemService(NOTIFICATION_SERVICE);
// Ocultamos la notificación si ha sido ya seleccionada
noti.flags |= Notification.FLAG_AUTO_CANCEL;
notificationManager.notify(0, noti);
}
}
}
}
}).start();
return Service.START_STICKY;
}
@Override
public IBinder onBind(Intent arg0) {
return null;
}
@Override
public void onDestroy() {
isRunning = false;
stopSelf();
}
Here is the ControNotas class.
public class ControlNotas extends SQLiteOpenHelper {
String sql = "CREATE TABLE notas (id INTEGER PRIMARY KEY AUTOINCREMENT, diames INTEGER, mesmes TEXT, nota TEXT, equipo TEXT)";
public ControlNotas(Context context, String name, SQLiteDatabase.CursorFactory factory, int version) {
super(context, name, factory, version);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(sql);
}
@Override
public void onUpgrade(SQLiteDatabase db, int versionAnte, int versionNue) {
}
public void InsertarUsuario (SQLiteDatabase db, Integer diames, String mesmes, String nota, String equipo){
SQLiteStatement pat = db.compileStatement("INSERT INTO notas (diames, mesmes, nota, equipo) VALUES (?,?,?,?)");
pat.bindLong(1, diames);
pat.bindString(2, mesmes);
pat.bindString(3, nota);
pat.bindString(4, equipo);
pat.execute();
}
}