简体   繁体   English

即使没有时间,AlarmManager也会重复通话

[英]AlarmManager repeats call even if its not time

I'm using alarm manager to call for api. 我正在使用警报管理器来调用api。 it is called in a activity onCreate. 在活动onCreate中调用它。 I want it to call an alarm at start of the app then alarms every three hours. 我希望它在应用程序启动时发出警报,然后每三个小时发出一次警报。

Calendar calendar = Calendar.getInstance();
    calendar.setTimeInMillis(System.currentTimeMillis());
    calendar.set(Calendar.HOUR_OF_DAY,1);
    AlarmManager alarmManager1 = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
    Intent myIntent1 = new Intent(LobbyActivity.this,WeatherBroadCastReceiverCurrent.class);
    PendingIntent pendingIntent1 = PendingIntent.getBroadcast(this,0,myIntent1,0);

    alarmManager1.setInexactRepeating(AlarmManager.RTC,calendar.getTimeInMillis(),AlarmManager.INTERVAL_HOUR+
            AlarmManager.INTERVAL_HOUR+AlarmManager.INTERVAL_HOUR/*(1000*60*60*3)*/,pendingIntent1);

That activity is then finished and proceeds to another activity when a button is clicked. 然后,该活动结束,并在单击按钮时继续进行另一活动。 My problem is-if the activity is recreated it calls an alarm even if it is not the time. 我的问题是-如果重新创建活动,即使不是时间,它也会发出警报。 Can I set an alarm on a non activity class so it will not be recalled when the activity is recreated?? 我可以在非活动类上设置一个警报,以便在重新创建活动时不会将其调出吗? if so how? 如果是这样怎么办? Tia 蒂亚

try to run by removing calendar.set(Calendar.HOUR_OF_DAY,1); 尝试通过删除calendar.set(Calendar.HOUR_OF_DAY,1)来运行 and run, it will call alarm at start of app 并运行,它将在应用程序启动时发出警报

To simply get over this, you need to create a flag and make it true so that the activity can check if the Alarm has been set before, it is has, then it will move forward without setting it. 要简单地解决此问题,您需要创建一个标志并将其设置为true,以便活动可以检查是否已设置过警报,是否已设置警报,然后它将继续前进而不进行设置。

Use of SharedPreferences is ideal for this. 为此,使用SharedPreferences是理想的。 This is one of my snippets, edit it according to your need. 这是我的摘要之一,请根据需要进行编辑。

SharedPreferences prefs;
SharedPreferences.Editor ed;
prefs = PreferenceManager.getDefaultSharedPreferences(this);
ed = prefs.edit();
boolean isOpeningForTheFirstTime = prefs.getBoolean("firstTime", true);

if(!isOpeningForTheFirstTime) {
    Intent i = new Intent(this, StartScreen.class);
    startActivity(i);
    finish();
}

And the AlarmManager can be simplified by removing a few things. 通过删除一些内容,可以简化AlarmManager。

public void setAlarm(){

    //To get the current time
    long alertTime = new GregorianCalendar().getTimeInMillis();

    //Interval of a minute
    int timeInterval = 60000;

    //Intent which you want to start
    Intent alertIntent = new Intent(this, ClassName.class);

    //Declaring the alarmManager
    AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);

    //Setting the alarmmanager up.
    alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, alertTime, timeInterval, PendingIntent.getBroadcast(this, 1, alertIntent, PendingIntent.FLAG_UPDATE_CURRENT));
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM