2013-09-19 2 views
0

Мне действительно надоела служба сигнализации, которую я создал. я создал аварийную службу для показа оповещения в своем приложении в 8:00, 16:00, 12:00 каждого дня в моей заявке. Он отлично работает и хорошо показывает. Я отменяю свою службу при закрытии приложения, но иногда появляется предупреждение, не встречая времени, например после выхода из приложения, при удалении приложения из истории вкладок, когда вы берете настройки устройства, я не понимаю, почему это происходит, я проверял решение в SO, но не получил никакого правильного решения, пожалуйста, помогите мне здесь, давая мой код.Аварийная сигнализация Не останавливается или не запускается неправильно

public void StartService() 
{ 
    Calendar c = Calendar.getInstance(); 
    dfcurrent = new SimpleDateFormat("dd MMM yyyy"); 
    long currentTime = System.currentTimeMillis(); 
    currentdate = new Date(System.currentTimeMillis()); 
    String currentdate1 = dfcurrent.format(currentdate); 

    Long datetoalert1 = Utility.DateReturn(currentdate1+" - "+08:00AM); 
    if(datetoalert1 < currentTime) 
    { 
     Date d = new Date(datetoalert1); 
     c.setTime(d); 
     c.add(Calendar.DATE, 1); 
     d = c.getTime(); 
     datetoalert1 = d.getTime(); 

    } 

    Long datetoalert2 = Utility.DateReturn(currentdate1+" - "+04:00PM); 

    if(datetoalert2 < currentTime) 
    { 
     Date d = new Date(datetoalert2); 
     c.setTime(d); 
     c.add(Calendar.DATE, 1); 
     d = c.getTime(); 
     datetoalert2 = d.getTime(); 

    } 

    Long datetoalert3 = Utility.DateReturn(currentdate1+" - "+00:00AM); 

    if(datetoalert3 < currentTime) 
    { 
     Date d = new Date(datetoalert3); 
     c.setTime(d); 
     c.add(Calendar.DATE, 1); 
     d = c.getTime(); 
     datetoalert3 = d.getTime(); 

    } 

    Intent myIntent1 = new Intent(Login.this, MyReceiver.class); 

    pendingIntentSApp1 = PendingIntent.getBroadcast(Login.this, 7, myIntent1,0); 
    pendingIntentSApp2 = PendingIntent.getBroadcast(Login.this, 8, myIntent1,0); 
    pendingIntentSApp3 = PendingIntent.getBroadcast(Login.this, 9, myIntent1,0); 

    AlarmManager alarmManager1 = (AlarmManager)getSystemService(ALARM_SERVICE); 
    AlarmManager alarmManager2 = (AlarmManager)getSystemService(ALARM_SERVICE); 
    AlarmManager alarmManager3 = (AlarmManager)getSystemService(ALARM_SERVICE); 

    alarmManager1.set(AlarmManager.RTC, datetoalert1, pendingIntentSApp1); 
    alarmManager2.set(AlarmManager.RTC, datetoalert2, pendingIntentSApp2); 
    alarmManager3.set(AlarmManager.RTC, datetoalert3, pendingIntentSApp3); 
} 

i m отменяет это ожидающее намерения при закрытии приложения. здесь давая код

public void exit_btn_click(View view) 
{ 

    Intent myIntent1 = new Intent(Login.this, MyReceiver.class); 
    pendingIntentSApp1 = PendingIntent.getBroadcast(Login.this, 7, myIntent1,0); 
    pendingIntentSApp2 = PendingIntent.getBroadcast(Login.this, 8, myIntent1,0); 
    pendingIntentSApp3 = PendingIntent.getBroadcast(Login.this, 9, myIntent1,0); 

    AlarmManager alarmManager1 = (AlarmManager)getSystemService(ALARM_SERVICE); 
    AlarmManager alarmManager2 = (AlarmManager)getSystemService(ALARM_SERVICE); 
    AlarmManager alarmManager3 = (AlarmManager)getSystemService(ALARM_SERVICE); 
    alarmManager1.cancel(pendingIntentSApp1); 
    alarmManager2.cancel(pendingIntentSApp2); 
     alarmManager3.cancel(pendingIntentSApp3); 


    System.exit(0); 
} 

ответ

1

в первую очередь вы не должны делать этого:

AlarmManager alarmManager1 = (AlarmManager)getSystemService(ALARM_SERVICE); 
AlarmManager alarmManager2 = (AlarmManager)getSystemService(ALARM_SERVICE); 
AlarmManager alarmManager3 = (AlarmManager)getSystemService(ALARM_SERVICE); 

достаточно только один ссылка на alarmManager.

Также пытаться удалить System.exit (0); вызов

1

пытаются добавить флаг в ожидании намерения

pendingIntentSApp1 = PendingIntent.getBroadcast(Login.this, 7, myIntent1,PendingIntent.FLAG_UPDATE_CURRENT); 

или лучше сигнализации ID

0

Вам придется отменить PendingIntent, а после отмены тревоги.

  try { 
      alarmManagerstop.cancel(getSendLocationIntent(context, 0)); 
      getSendLocationIntent(context, 0).cancel(); 
      } catch (Exception e) { 
       e.printStackTrace(); 
       } 

Это исправит вашу проблему.

Смежные вопросы