Как исправить ошибку в диспетчере тревог? - PullRequest
0 голосов
/ 04 августа 2020

Я хочу сделать в своем приложении функцию для отправки уведомления каждые 5 секунд:

   @Override
   protected void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       final AlarmManager am = (AlarmManager) this.getSystemService(Context.ALARM_SERVICE);
       final Intent intent = new Intent(getBaseContext(), ShowFormula.class);
       final PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT );
       buttonStart.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view){
                am.setInexactRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), 5000, pendingIntent);
                Log.i(TAG, "started");
            }
        }); 

, а это код получателя:

    @Override
    public void onReceive(Context context, Intent intent){
        Log.i(TAG, "onReceive working");
        if (b) {
            Log.i(TAG, "notification started");
            makeNotification(context);
        }
    public void makeNotification(Context context){
        Log.i(TAG, "notification continued");
        <...notification code...>
    }

и это журналы:

2020-08-04 18:23:34.451 5492-5492/com.example.learner I/MyApp: started
2020-08-04 18:24:02.074 5492-5492/com.example.learner I/MyApp: onReceive working
2020-08-04 18:24:02.077 5492-5492/com.example.learner I/MyApp: notification started
2020-08-04 18:24:02.077 5492-5492/com.example.learner I/MyApp: notification continued
2020-08-04 18:24:34.455 5492-5492/com.example.learner I/MyApp: onReceive working
2020-08-04 18:24:34.456 5492-5492/com.example.learner I/MyApp: notification started
2020-08-04 18:24:34.456 5492-5492/com.example.learner I/MyApp: notification continued
2020-08-04 18:25:57.792 5492-5492/com.example.learner I/MyApp: onReceive working
2020-08-04 18:25:57.793 5492-5492/com.example.learner I/MyApp: notification started
2020-08-04 18:25:57.793 5492-5492/com.example.learner I/MyApp: notification continued
2020-08-04 18:26:57.804 5492-5492/com.example.learner I/MyApp: onReceive working
2020-08-04 18:26:57.805 5492-5492/com.example.learner I/MyApp: notification started
2020-08-04 18:26:57.805 5492-5492/com.example.learner I/MyApp: notification continued
2020-08-04 18:28:02.358 5492-5492/com.example.learner I/MyApp: onReceive working
2020-08-04 18:28:02.359 5492-5492/com.example.learner I/MyApp: notification started
2020-08-04 18:28:02.359 5492-5492/com.example.learner I/MyApp: notification continued

Как видите, onReceive () вызывается случайными интервалами. где может быть проблема? Помогите пожалуйста!

1 Ответ

0 голосов
/ 04 августа 2020

заменить

am.setInexactRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), 5000, pendingIntent);

на

am.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), 5000, pendingIntent);

setRepeating точнее

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...