Несколько alarm в alarmmanager
Добавлено: 07 июн 2014, 04:13
Вот мой Receiver:
Вызываю так:
Но работает не правильно.
Код: Выделить всё
public class Receiver extends BroadcastReceiver {
final public static String ONE_TIME = "onetime";
final public static String SECOND_TIME = "secondtime";
AudioManager amBells;
@Override
public void onReceive(Context context, Intent intent) {
PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "YOUR TAG");
wl.acquire();
Bundle extras = intent.getExtras();
if(extras != null && extras.getBoolean(ONE_TIME) == false){
amBells = (AudioManager)context.getSystemService(Context.AUDIO_SERVICE);
amBells.setRingerMode(AudioManager.RINGER_MODE_SILENT);
}
if(extras != null && extras.getBoolean(SECOND_TIME) == false){
amBells = (AudioManager)context.getSystemService(Context.AUDIO_SERVICE);
amBells.setRingerMode(AudioManager.RINGER_MODE_NORMAL);
}
wl.release();
}
public void SetAlarm(Context context)
{
AlarmManager am = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(context, Receiver.class);
intent.putExtra(ONE_TIME, Boolean.FALSE);
PendingIntent pi = PendingIntent.getBroadcast(context, 0, intent, 0);
am.setRepeating(AlarmManager.RTC, System.currentTimeMillis(), 1000 * 1 * 60 * 60 * 24 * 7, pi);
}
public void SetAlarm2(Context context)
{
AlarmManager am = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(context, Receiver.class);
intent.putExtra(SECOND_TIME, Boolean.FALSE);
PendingIntent pi = PendingIntent.getBroadcast(context, 0, intent, 0);
am.setRepeating(AlarmManager.RTC, System.currentTimeMillis() + 10000, 1000 * 1 * 60 * 60 * 24 * 7, pi);
}
public void CancelAlarm(Context context)
{
Intent intent = new Intent(context, Receiver.class);
PendingIntent sender = PendingIntent.getBroadcast(context, 0, intent, 0);
AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
alarmManager.cancel(sender);
}
}Код: Выделить всё
if(alarm != null)
{
alarm.SetAlarm(context);
}
else
{
Toast.makeText(context, "Alarm is null", Toast.LENGTH_SHORT).show();
}