唤醒锁无法在 Nexus 7 上运行
Wakelock not working on Nexus 7
大家好,在此先感谢您的帮助和时间。
好的,我的问题是我开发了一个显示服务通知的应用程序。我必须使用唤醒锁才能在屏幕关闭时显示通知。
在我的 samsung galaxy s4 运行 lollipop 中完美运行,但在我的 Google Nexus 7 运行 jelly bean 中不起作用,当屏幕关闭时,没有通知显示。
这是 Android 版本的问题吗?我是做错了什么还是忘记了什么?
有人可以帮我解决这个问题吗?
非常感谢。
编辑:
对不起,但我之前没有代码。这是...
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();
}
}
尝试以下 WakeLock
标志来唤醒屏幕:
PowerManager.WakeLock screenOn = ((PowerManager) getSystemService(Context.POWER_SERVICE)).newWakeLock(
PowerManager.SCREEN_DIM_WAKE_LOCK | PowerManager.ACQUIRE_CAUSES_WAKEUP, TAG);
screenOn.acquire();
这对我适用于所有设备。
大家好,在此先感谢您的帮助和时间。
好的,我的问题是我开发了一个显示服务通知的应用程序。我必须使用唤醒锁才能在屏幕关闭时显示通知。
在我的 samsung galaxy s4 运行 lollipop 中完美运行,但在我的 Google Nexus 7 运行 jelly bean 中不起作用,当屏幕关闭时,没有通知显示。
这是 Android 版本的问题吗?我是做错了什么还是忘记了什么?
有人可以帮我解决这个问题吗?
非常感谢。
编辑: 对不起,但我之前没有代码。这是...
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();
}
}
尝试以下 WakeLock
标志来唤醒屏幕:
PowerManager.WakeLock screenOn = ((PowerManager) getSystemService(Context.POWER_SERVICE)).newWakeLock(
PowerManager.SCREEN_DIM_WAKE_LOCK | PowerManager.ACQUIRE_CAUSES_WAKEUP, TAG);
screenOn.acquire();
这对我适用于所有设备。