[英]Wakelock not working on Nexus 7
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. 在运行棒棒糖的三星银河s4中,该功能可以完美运行,但在运行果冻豆的Google Nexus 7中,该功能无法正常工作;当屏幕关闭时,不会显示任何通知。
Is this an issue with the Android version? 这是Android版本的问题吗? 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: 尝试使用以下
WakeLock
标志来唤醒屏幕:
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. 这适用于所有设备。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.