Hello everybody and thanks in advance for the help and time.
Okay my problem is I have developed an application that shows a notification with a Service. I had to use a wakelock in order to display the notification when the screen is off.
That works perfectly in my samsung galaxy s4, running lollipop, but don't work in my Google Nexus 7 running jelly bean, when screen is off, no notification is displayed.
Is this an issue with the Android version? Am I doing something wrong or forgetting something?.
Can anybody help me about this?
Thank you very much.
EDIT: Sorry, but I hadn`t the code before. Here it is...
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.net.Uri;
import android.os.Handler;
import android.os.IBinder;
import android.os.PowerManager;
import android.support.v4.app.NotificationCompat;
import android.text.format.Time;
import java.util.Timer;
import java.util.TimerTask;
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();
}
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()
{
Time today = new Time(Time.getCurrentTimezone());
today.setToNow();
SharedPreferences prefs = getApplicationContext().getSharedPreferences("com.tonicc.opositest_preferences", Context.MODE_MULTI_PROCESS);
Boolean SONIDO_RECORDATORIO_ACTIVADO = prefs.getBoolean("sonidoRecordatorio", false);
Boolean VIBRACION_RECORDATORIO_ACTIVADA = prefs.getBoolean("vibracionRecordatorio", false);
String[] HoraMinutos = prefs.getString("horaRecordatorio", "00:00").split(":");
int Hora = (Integer.parseInt(HoraMinutos[0]));
int Minutos = (Integer.parseInt(HoraMinutos[1]));
PowerManager powerManager = (PowerManager) getSystemService(POWER_SERVICE);
wakeLock = powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "MyWakelockTag");
wakeLock.acquire();
if(today.hour == Hora && today.minute == Minutos)
{
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);
}
wakeLock.release();
}
}
@Override
public void onDestroy()
{
super.onDestroy();
mTimer.cancel();
}
}
Try the following WakeLock
flags to wake up the screen:
PowerManager.WakeLock screenOn = ((PowerManager) getSystemService(Context.POWER_SERVICE)).newWakeLock(
PowerManager.SCREEN_DIM_WAKE_LOCK | PowerManager.ACQUIRE_CAUSES_WAKEUP, TAG);
screenOn.acquire();
This works for me on all devices.