2013-07-24 42 views
0

我有一个报警列表,我把第一个,当它听起来我把下一个的报警.... 这是作品。 但是如果应用程序在后台有足够的时间,警报不起作用。AlarmManager在后台

我把清单:

<receiver android:name=".Alertas_Broadcast" > 
     <intent-filter> 
      <action android:name="com.pack.pack.Alertas" /> 

      <category android:name="com.pack.pack" /> 
     </intent-filter> 
    </receiver> 

和广播和把新的报警功能:

public class Alertas_Broadcast extends BroadcastReceiver { 
@Override 
public void onReceive(Context context, Intent intent) { 
    Bundle extras = intent.getExtras(); 

    String mensaje = ""; 
    if (extras != null) 
     mensaje = extras.getString("mensaje"); 

    if (!mensaje.equals("")){ 
     Utilidades.generateNotification(context, mensaje, Main.class, null); 
     // I put the next alarm calling setNextAlarm with the new parameters 
    } 
} 
} 

public void setNextAlarm (long milisegundos, String mensaje){ 
    Bundle extras = new Bundle(); 
    extras.putString("mensaje", mensaje); 
    Intent i = new Intent("com.pack.pack.Alertas"); 
    i.putExtras(extras); 

    PendingIntent pintent = PendingIntent.getBroadcast(InfoApp.miContexto, (int) milisegundos, i, PendingIntent.FLAG_UPDATE_CURRENT); 
    AlarmManager alarm = (AlarmManager)InfoApp.miContexto.getSystemService(Context.ALARM_SERVICE); 

    if (milisegundos != 0){ 
     alarm.setRepeating(AlarmManager.RTC_WAKEUP, milisegundos, 99999999, pintent); 
    } 
    else{ 
     alarm.cancel(pintent); 
    } 
} 

问题出在哪里?我想象的问题是接收器的行为,但我不知道如何解决它。 我读过这样的说法,因为有很多资源都花费在服务器上,所以不是一个好主意。

对不起,我的英语,谢谢!

回答

0

如果您的活动在背景中已经足够长,它可能已被系统杀死。您可以尝试使用服务和setForeground()来实现您的目标。

0

你是如何设置下一个警报的?如果你是通过服务来完成的,你需要获得一个唤醒锁。

当手机处于睡眠状态并且您收到闹钟时,手机将只保持清醒状态,而BroadcastReceiver处于onReceive()方法中,然后再次入睡。所以不能保证你的“setNextAlarm”被调用。

+0

我搜索数据库中的下一个日期,并用新参数调用setNextAlarm。我也有清单中的WAKE_LOCK权限,但我没有任何服务 – user1852854