Hello everybody and thanks in advance,
I have a service that checks every minute if the actual time match a given time (hour and minute) and if so, shows a notification. The problem is that the wakelock is "drinking" my battery quickly. I guess that the location of the wakelock is not the correct. Can anybody tell me what's wrong and how to solve it?
Thanks!
Here's my code...
public class ServicioRecordatorio extends Service
{
public static final long INTERVALO_NOTIFICACION = 60 * 1000;
private Handler mHandler = new Handler();
private Timer mTimer = null;
PowerManager.WakeLock wakeLock;
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public void onCreate()
{
if (mTimer != null)
{
mTimer.cancel();
}
else
{
mTimer = new Timer();
}
int MiWakelock = 0;
PowerManager powerManager = (PowerManager) getSystemService(Context.POWER_SERVICE);
MiWakelock = PowerManager.PARTIAL_WAKE_LOCK;
wakeLock = powerManager.newWakeLock(MiWakelock, "MyWakelockTag");
wakeLock.acquire();
mTimer.scheduleAtFixedRate(new NotificacionTimerTask(), 0, INTERVALO_NOTIFICACION);
}
class NotificacionTimerTask extends TimerTask
{
@Override
public void run()
{
mHandler.post(new Runnable()
{
@Override
public void run()
{
MuestraNotificacion();
}
});
}
private void MuestraNotificacion()
{
SharedPreferences prefs = getApplicationContext().getSharedPreferences("com.tonicc.opositest_preferences", Context.MODE_MULTI_PROCESS);
String[] HoraMinutos = prefs.getString("horaRecordatorio", "00:00").split(":");
int Hora = (Integer.parseInt(HoraMinutos[0]));
int Minutos = (Integer.parseInt(HoraMinutos[1]));
Time Ahora = new Time(Time.getCurrentTimezone());
Ahora.setToNow();
if(Ahora.hour == Hora && Ahora.minute == Minutos)
{
Boolean SONIDO_RECORDATORIO_ACTIVADO = prefs.getBoolean("sonidoRecordatorio", false);
Boolean VIBRACION_RECORDATORIO_ACTIVADA = prefs.getBoolean("vibracionRecordatorio", false);
Uri sonido = Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.fin_test);
int notificacionID = 1;
Intent i = new Intent(getApplicationContext(), BienvenidaActivity.class);
i.putExtra("notificacionID", notificacionID);
PendingIntent pendingIntent = PendingIntent.getActivity(getApplicationContext(), 0, i, 0);
CharSequence ticker = getResources().getString(R.string.ac_servicio_recordatorio_01);
CharSequence contentTitle = getResources().getString(R.string.ac_servicio_recordatorio_02);
CharSequence contentText = getResources().getString(R.string.ac_servicio_recordatorio_03);
CharSequence subText = getResources().getString(R.string.ac_servicio_recordatorio_04);
NotificationCompat.Builder noti = new NotificationCompat.Builder(getApplicationContext());
noti.setContentIntent(pendingIntent);
noti.setTicker(ticker);
noti.setContentTitle(contentTitle);
noti.setContentText(contentText);
noti.setSubText(subText);
noti.setSmallIcon(getResources().getIdentifier("icono_app_pequeno", "drawable", getPackageName()));
noti.addAction(getResources().getIdentifier("icono_app_pequeno", "drawable", getPackageName()), ticker, pendingIntent);
noti.setAutoCancel(true);
if(SONIDO_RECORDATORIO_ACTIVADO)
{
noti.setSound(sonido);
}
if(VIBRACION_RECORDATORIO_ACTIVADA)
{
noti.setVibrate(new long[]{100, 250, 100, 500});
}
Notification n = noti.build();
NotificationManager nm = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
nm.notify(notificacionID, n);
}
}
}
@Override
public void onDestroy()
{
super.onDestroy();
mTimer.cancel();
wakeLock.release();
}
}