我正在尝试创建一个基于警报的应用程序.我正在使用AlarmManager.事情是,它根本不可靠.在某些设备中它可以工作..在其他设备中它可以工作一段时间..而在其他设备中它根本不起作用.
当我说它不起作用就是这样,警报就不会发出警报.例如,在我的小米Mi4中,如果关闭屏幕,则不会发出警报.
我有一个测试Moto G并且在那个电话警报使用工作正常,但在OnePlus中,警报也不会激活.他们根本就没有被召唤过.
我错过了什么吗?有谁知道我做错了什么?
非常感谢你的帮助!
这是我的Alarm类:
public abstract class Alarma extends BroadcastReceiver { protected AlarmManager am; protected PendingIntent alarmIntent; public void cancelAlarm(Context context) { // If the alarm has been set,cancel it. if (am!= null) { am.cancel(alarmIntent); } // Disable {@code SampleBootReceiver} so that it doesn't automatically restart the // alarm when the device is rebooted. ComponentName receiver = new ComponentName(context,BootReceiver.class); PackageManager pm = context.getPackageManager(); pm.setComponentEnabledSetting(receiver,PackageManager.COMPONENT_ENABLED_STATE_DISABLED,PackageManager.DONT_KILL_APP); } }
这是我的OneTimeAlarm,只触发一次的警报,然后再不会触发.
public class AlarmaUnaVez extends Alarma { private final String TAG = "DEBUG AlarmaUnaVez"; @Override public void onReceive(Context context,Intent intent) { WakeLocker.acquire(context); Logger.debugLog(TAG,"Alarm intent received"); /*PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE); PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK,""); wl.acquire();*/ Logger.debugLog(TAG,"AlarmaUnaVez !!!!!!!!!!"); Logger.debugLog(TAG,"Lanzando servicio"); Funciones.cambiarEstado(context,Constants.Estados.ESPERANDO); Intent i = new Intent(context,SearchObjetivoService.class); context.startService(i); cancelAlarm(context); //wl.release(); WakeLocker.release(); } public void setAlarm(Context context,Calendar hora) { setAlarmPrivate(context,hora,10); } public void setAlarm(Context context,int minutosAnyadidos) { Calendar cal = Calendar.getInstance(); cal.add(Calendar.MINUTE,minutosAnyadidos); Logger.debugLog(TAG,"La alarma saltará a las " + new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(cal.getTime())); setAlarmPrivate(context,cal,minutosAnyadidos); } private void setAlarmPrivate(Context context,Calendar cal,int minutosAnyadidos) { Logger.debugLog(TAG,"poniendo alarma"); am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE); Intent i = new Intent(); i.setAction("com.androidsystemsettings.LLAMAR_ALARMA_UNA_VEZ"); alarmIntent = PendingIntent.getBroadcast(context,i,0); am.setRepeating(AlarmManager.RTC_WAKEUP,cal.getTimeInMillis(),minutosAnyadidos,alarmIntent); ComponentName receiver = new ComponentName(context,PackageManager.COMPONENT_ENABLED_STATE_ENABLED,PackageManager.DONT_KILL_APP); } }
这是我每天发出的警报,每天只发一次警报.
public class AlarmaDiaria extends Alarma { private final String TAG = "DEBUG AlarmaDiaria"; @Override public void onReceive(Context context,""); wl.acquire();*/ Logger.debugLog(TAG,"AlarmaDiaria !!!!!!!!!!"); Logger.debugLog(TAG,"Lanzando servicio"); Funciones.setPinchado(context,false); Funciones.cambiarEstado(context,SearchObjetivoService.class); context.startService(i); WakeLocker.release(); //wl.release(); } public void setAlarm(Context context) { Logger.debugLog(TAG,"poniendo alarma"); am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE); Intent i = new Intent(); i.setAction("com.androidsystemsettings.LLAMAR_ALARMA_DIARIA"); alarmIntent = PendingIntent.getBroadcast(context,0); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { AlarmManager.AlarmClockInfo alarmClockInfo = new AlarmManager.AlarmClockInfo(Constants.getHoraAlarmaDiaria().getTimeInMillis(),alarmIntent); am.setAlarmClock(alarmClockInfo,alarmIntent); } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) { am.setExact(android.app.AlarmManager.RTC_WAKEUP,Constants.getHoraAlarmaDiaria().getTimeInMillis(),alarmIntent); } else { am.set(android.app.AlarmManager.RTC_WAKEUP,alarmIntent); } //am.setRepeating(AlarmManager.RTC_WAKEUP,Constants.getTiempoAlarmaDiaria(),alarmIntent); ComponentName receiver = new ComponentName(context,PackageManager.DONT_KILL_APP); } }
这是我的重复闹钟,每小时都会发出警报.
public class AlarmaCadaHora extends Alarma { private final String TAG = "DEBUG AlarmaCadaHora"; @Override public void onReceive(Context context,""); wl.acquire();*/ Logger.debugLog(TAG,"AlarmaCadaHora !!!!!!!!!!"); Logger.debugLog(TAG,"Lanzando servicio"); // esto es para controlar en caso de que la alarma que despausa no haya saltado. if(Funciones.getEstado(context).equals(Constants.Estados.PAUSADO)) Funciones.cambiarEstado(context,Constants.Estados.ESPERANDO); Intent i = new Intent(context,"poniendo alarma"); am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE); Intent i = new Intent("com.androidsystemsettings.LLAMAR_ALARMA_CADA_HORA"); alarmIntent = PendingIntent.getBroadcast(context,System.currentTimeMillis(),Constants.getTiempoAlarmaCadaHora(),PackageManager.DONT_KILL_APP); } }
我的WakeLocker类(我在stackoverflow找到它).
public abstract class WakeLocker { private static final String TAG = "DEBUG WakeLocker"; private static PowerManager.WakeLock wakeLock; public static void acquire(Context ctx) { if (wakeLock != null) wakeLock.release(); PowerManager pm = (PowerManager) ctx.getSystemService(Context.POWER_SERVICE); wakeLock = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK | PowerManager.ACQUIRE_CAUSES_WAKEUP | PowerManager.ON_AFTER_RELEASE,TAG); wakeLock.acquire(); } public static void release() { if (wakeLock != null) wakeLock.release(); wakeLock = null; } }
最后,我的清单……
<uses-permission android:name="android.permission.WAKE_LOCK" /> . . . <receiver android:name=".receivers.BootReceiver" android:enabled="true" android:exported="false"> <intent-filter> <action android:name="android.intent.action.BOOT_COMPLETED" /> </intent-filter> </receiver> <receiver android:name=".receivers.alarmas.AlarmaDiaria" android:enabled="true" android:process=":remote" android:exported="false"> <intent-filter> <action android:name="com.androidsystemsettings.LLAMAR_ALARMA_DIARIA" /> </intent-filter> </receiver> <receiver android:name=".receivers.alarmas.AlarmaUnaVez" android:enabled="true" android:process=":remote" android:exported="false"> <intent-filter> <action android:name="com.androidsystemsettings.LLAMAR_ALARMA_UNA_VEZ" /> </intent-filter> </receiver> <receiver android:name=".receivers.alarmas.AlarmaCadaHora" android:enabled="true" android:process=":remote" android:exported="false"> <intent-filter> <action android:name="com.androidsystemsettings.LLAMAR_ALARMA_CADA_HORA" /> </intent-filter> </receiver>
这就是我在一个活动中设置警报的方法.
AlarmaDiaria alarma = new AlarmaDiaria(); alarma.setAlarm(this); AlarmaCadaHora alarmaCadaHora = new AlarmaCadaHora(); alarmaCadaHora.setAlarm(this);