警报管理器不会重复吗?

时间:2019-10-07 18:37:39

标签: java android alarmmanager

因此,我编写了一个代码,其中警报管理器应在准确的时间触发并每分钟重复一次,并且它部分起作用,第一次触发,但在给定的间隔(一分钟)后不再重复。 / p>

主要活动:

public void SetAlarm()
    {
        final Button button = findViewById(R.id.button); // replace with a button from your own UI
        BroadcastReceiver receiver = new BroadcastReceiver() {
            @Override public void onReceive( Context context, Intent _ )
            {
                Toast.makeText(context, "Nope", Toast.LENGTH_SHORT).show();
                context.unregisterReceiver( this ); // this == BroadcastReceiver, not Activity
            }
        };

        Calendar cal = Calendar.getInstance();
        cal.set(Calendar.HOUR_OF_DAY, 20);
        cal.set(Calendar.MINUTE, 30);
        cal.set(Calendar.SECOND, 0);

        this.registerReceiver( receiver, new IntentFilter("com.blah.blah.somemessage") );

        PendingIntent pintent = PendingIntent.getBroadcast( this, 0, new Intent("com.blah.blah.somemessage"), 0 );
        AlarmManager manager = (AlarmManager)(this.getSystemService( Context.ALARM_SERVICE ));

        // set alarm to fire 5 sec (1000*5) from now (SystemClock.elapsedRealtime())
        manager.setInexactRepeating( AlarmManager.RTC_WAKEUP, cal.getTimeInMillis() + 1000*5, 6000, pintent );
    }

    public void klik(View view) {
        SetAlarm();
    }

}

3 个答案:

答案 0 :(得分:0)

尝试使用setRepeating()

manager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),
    cal.getTimeInMillis() + 1000*5, 6000, pintent); 

有关最佳做法,您可以查看this doc

答案 1 :(得分:0)

为了安全性和电池寿命,我认为以毫秒为单位的最小重复次数是60000(1分钟)

  

从Android 5.1开始,值将被强制高达60000 ...

答案 2 :(得分:0)

如果您的所有主要活动我都不了解。为什么要在SetAlarm()((单击时调用SetAlarm))中定义按钮?

我认为您应该尝试一种全新的方式。像这样...

  1. 在主活动中的onCreate事件定义按钮:

    Button btn_yrbnt = findViewById(R.id.btn);
    
  2. 为onClick举办活动

    btn_yrbnt.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            startAlarm();
        }
    });
    
  3. 在startAlarm()事件中设置警报

    Intent intent = new Intent(this, Alarm.class);  //In 4th step u understand it
    PendingIntent pendingIntent = PendingIntent.getBroadcast(this.getApplicationContext(), 0, intent, 0);
    AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
    
    Calendar calendar = Calendar.getInstance();
    calendar.setTimeInMillis(System.currentTimeMillis());
    calendar.set(Calendar.HOUR_OF_DAY, 8);
    calendar.set(Calendar.MINUTE, 10);
    calendar.set(Calendar.SECOND, 1);
    alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), REPEAT_TIME, pendingIntent);  //Repeat time is bigger than 60000ms but u can use AlarmManager.INTERVAL_DAY for example or AlarmManager.INTERVAL_FIFTEEN_MINUTES
    
  4. 使用BroadCastReceiver超类创建一个名为Alarm.class的新Java类

    public class Alarm extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
         //Type what do u want to do on Alarm's fire!!!!
    }}
    
  5. 重要!在清单中应链接接收方

    <receiver android:name=".Alarm"
        android:enabled="true"
        android:exported="true"/>
    

如果您做的一切都很好,您的闹钟就可以正常工作! 对不起我的英语不好!