簡體   English   中英

AlarmManager在錯誤的時間響應

[英]AlarmManager responding at wrong time

對不起,如果以前回答過,但是我到處都是,但是沒有找到合適的解決方案

我正在使用AlarmManager每天早上9點自動觸發通知,但是當我嘗試在模擬器上運行它時,它會立即執行,然后每半小時(准確地說是31-32分鍾)執行一次,而不是每天早上9點才執行一次。

有什么想法嗎? 幫助贊賞。

代碼如下:

public class Home extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_bsheet);

    notificationAlert(savedInstanceState);

}

    private void notificationAlert(Bundle savedInstanceState) {

    AlarmManager manager;
    PendingIntent pendingIntent;
    Intent intent=new Intent(Home.this, Notify.class);
    manager=(AlarmManager)getSystemService(ALARM_SERVICE);
    pendingIntent=PendingIntent.getService(Home.this,
            0, intent, 0);
    GregorianCalendar gcal = new GregorianCalendar();
    gcal.set(Calendar.HOUR_OF_DAY, 9);
    gcal.set(Calendar.MINUTE, 0);
    gcal.set(Calendar.SECOND, 0);
    gcal.set(Calendar.MILLISECOND, 0);

    long initTime = gcal.getTimeInMillis();

    manager.setRepeating(AlarmManager.RTC_WAKEUP, initTime,
            24*60*60*1000, pendingIntent);
}

}

干杯,

編輯:我的意圖是,一旦安裝了該應用程序,它將在上午9點觸發此警報。 我已將警報放在onCreate中,所以我不確定是否僅在每次啟動應用程序時才創建警報,而當我隱藏應用程序時發生了一些奇怪的事情……再次感謝您的見解!

嘗試這個 :

    Calendar currentTime = Calendar.getInstance();
    Calendar alarmTime = Calendar.getInstance();
    currentTime.set(Calendar.HOUR_OF_DAY, 9;
    currentTime.set(Calendar.MINUTE, 0);
    currentTime.set(Calendar.SECOND, 0);
    currentTime.set(Calendar.MILLISECOND, 0);
    if (alarmTime.compareTo(currentTime) <= 0) {
// check for time
alarmTime.add(Calendar.DATE, 1);
}

Intent intent = new Intent(getBaseContext(), Receiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(
    getBaseContext(), 1, intent, 0);
AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
am.setRepeating(AlarmManager.RTC_WAKEUP, alarmTime.getTimeInMillis(),
                        AlarmManager.INTERVAL_DAY, pendingIntent);
manager.setRepeating(AlarmManager.RTC_WAKEUP, initTime, 24*60*60*1000, pendingIntent);

如果initTime < System.currentTimeMillis()警報管理器將立即觸發

來自docs

如果時間是過去的時間,則將立即觸發警報,並根據過去的觸發時間相對於重復間隔的時間來計算警報數。

根據您提供的代碼, gcal.getTimeInMillis()將返回對應於Today 9.00的 millisecods。 因此,如果您在這段時間之后嘗試運行代碼,則initTime將小於當前系統時間,這將觸發AlarmManager的立即運行。

例如,要解決此問題,您可以在日歷中添加一天,然后再通過gcal.getTimeInMillis()如果已經過去),那么它將指向明天9.00,以便明天早上運行

更新1

嘗試過這樣的代碼,它對我來說像預期的那樣工作-每10秒觸發一次服務:

    @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    initManager();
}
private void initManager() {
    AlarmManager manager;
    PendingIntent pendingIntent;
    Intent intent = new Intent(this, NotifyService.class);
    manager=(AlarmManager)getSystemService(ALARM_SERVICE);
    pendingIntent=PendingIntent.getService(this, 0, intent, 0);

    long initTime = System.currentTimeMillis() + 5000;

    manager.setRepeating(AlarmManager.RTC_WAKEUP, initTime,
            10*1000, pendingIntent);        
}

但是,您可以使用另一個選項:有AlarmManager.set()方法,您可以像這樣觸發它

long runTime = /* your calendar time + 24 hours in millis*/
manager.set(AlarmManager.RTC_WAKEUP, runTime, pendingIntent);       

因此它將在runTime觸發您的服務。 然后在該服務中,另一天用重新計算的runTime調用相同的方法,因此它看起來像這樣:

public MainActivity extends Activity{

    protected onCreate(Bundle bundle){
        ....
        initManager();
    }

    private initManager(){
        ...
        long runTime = /* time of your first alarm/notification/whatever you develope */

        Intent intent = new Intent(this, NotifyService.class);
        pendingIntent=PendingIntent.getService(this, 0, intent, 0);
        manager.set(AlarmManager.RTC_WAKEUP, runTime, pendingIntent);
    }
}

和服務:

public NotifyService extends Service{
    ...
    public int onStartCommand(...){
         ...
        /* do stuff */

        Intent intent = new Intent(getApplicationContext(), NotifyService.class);
        pendingIntent=PendingIntent.getService(this, 0, intent, 0);
        long runTime = /* recalculate it according to the next time of firing this service*/
        manager.set(AlarmManager.RTC_WAKEUP, runTime, pendingIntent);

    }

}

因此,每次服務觸發時,您的服務都會在AlarmManager本身中注冊一個意圖。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM