报警服务不停止或启动不正确

时间:2013-09-19 14:27:30

标签: android android-service

我真的厌倦了我创建的闹钟服务。我创建了一个警报服务,用于在我的应用程序中每天上午8点,下午4点,上午12点显示警报。它工作正常,警报显示完美。我在关闭应用程序时取消了我的服务,但有时候警报会在没有满足时间的情况下出现,例如退出应用程序后,从标签历史记录中删除应用程序时,当获取设备的设置时,我不知道为什么发生这种情况,我正在检查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);
}

关闭应用时,我取消了这个待处理的意图。这里给出了代码

  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);
}

3 个答案:

答案 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 :(得分:1)

尝试将标志添加到待处理的意图

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

或更好的警报ID

答案 2 :(得分:0)

取消闹钟后,您还必须取消PendingIntent。

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

这将解决您的问题。