AlarmManager 延迟触发或根本不触发

mag*_*n94 0 android alarmmanager

大家好,我正在尝试学习如何在 Android 中使用AlarmManager和。BroadcastReceiver我遇到了一些问题AlarmManager:我在 1 分钟距离处设置了两个警报,但只有一个警报响起,而且晚了几分钟(我猜是不可预测的)。

这是我的主要活动(我通过点击按钮设置闹钟)

public class MainActivity extends AppCompatActivity {
    AlarmManager alarmManager;
    Intent intent;
    PendingIntent pendingIntent;
    AtomicInteger atomicInteger;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
        intent = new Intent(Constants.EXTENDED_DATA_STATUS);
        atomicInteger = new AtomicInteger();
        setContentView(R.layout.activity_main);

        Button startButton = (Button) findViewById(R.id.start_button);

        startButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int id = atomicInteger.incrementAndGet();
                pendingIntent = PendingIntent.getBroadcast(getApplicationContext(),id,intent,0);
                Calendar firstLullo = Calendar.getInstance();
                Calendar secondLullo = Calendar.getInstance();
                firstLullo.set(Calendar.HOUR_OF_DAY,10);
                firstLullo.set(Calendar.MINUTE,55);
                secondLullo.set(Calendar.HOUR_OF_DAY,10);
                secondLullo.set(Calendar.MINUTE,56);
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
                    alarmManager.setExact(AlarmManager.RTC_WAKEUP,firstLullo.getTimeInMillis(),pendingIntent);
                    alarmManager.setExact(AlarmManager.RTC_WAKEUP,secondLullo.getTimeInMillis(),pendingIntent);
                }
                Log.d("ALARM","settato con id " + String.valueOf(id));
            }
        });
    }
}
Run Code Online (Sandbox Code Playgroud)

我的接收器只是显示一个 Toast 并使手机振动。 Constants.EXTENDED_DATA_STATUS是一个来自类的字符串Constants,它被添加到intent-filterAndroid Manifest 中我的 Reveiver 中。

我缺少什么?我用谷歌搜索了很多,只发现我必须使用setExact()但没有运气..

提前致谢

Str*_*der 5

只触发一个的原因是因为您总是取消第一个

来自文档:如果已经为同一个 IntentSender 安排了一个警报,则先前的警报将首先被取消。

要解决此问题,请为两个警报使用不同的PendingIntents。

-要让您的接收器多次发射,请在您的onReceive()

示例: 仅当您想要两个彼此分开的接收器时!

//Declare AlarmManager
AlarmManager am = (AlarmManager) LayoutActivity.this.getSystemService(ALARM_SERVICE);

//create new calendar instance for your first alarm
Calendar startTime= Calendar.getInstance();

//set the time of your first alarm
firstLullo.set(Calendar.HOUR_OF_DAY,10);
firstLullo.set(Calendar.MINUTE, 55);
firstLullo.set(Calendar.SECOND, 0);

//create a pending intent 
PendingIntent firstPI = PendingIntent.getBroadcast(yourActivity.this, 0, new Intent("yourFirstAlarmReceiver"), PendingIntent.FLAG_UPDATE_CURRENT);

//schedule time for pending intent, and set the interval to day so that this event will repeat at the selected time every day
am.setRepeating(AlarmManager.RTC_WAKEUP, firstAlarm.getTimeInMillis(), firstPI);

//----------------------------------------------------

//create new calendar instance for your second alarm
Calendar endCalender = Calendar.getInstance();

//Set the time alarm of your second alarm
secondLullo.set(Calendar.HOUR_OF_DAY,10);
secondLullo.set(Calendar.MINUTE,56);
secondLullo.set(Calendar.SECOND, 0);

//create a pending intent to be 
PendingIntent secondPI= PendingIntent.getBroadcast(yourActivity.this, 0, new Intent("yourSecondAlarmReceiver"), PendingIntent.FLAG_UPDATE_CURRENT);

//schedule time for pending intent, and set the interval to day so that this event will repeat at the selected time every day
am.setRepeating(AlarmManager.RTC_WAKEUP, secondLullo.getTimeInMillis(), secondPI);
Run Code Online (Sandbox Code Playgroud)

在Manifest.XML中注册警报:

<receiver android:name="firstAlarm" >
    <intent-filter>
        <action android:name="yourFirstAlarmReceiver" >
        </action>
    </intent-filter>
</receiver>

<receiver android:name="secondAlarm" >
    <intent-filter>
            <action android:name="yourSecondAlarmReceiver" >
            </action>
    </intent-filter>
</receiver>
Run Code Online (Sandbox Code Playgroud)

现在您可以通过以下方式拨打闹钟:

第一个警报:

public class yourFirstAlarmReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {

    //Do something when first alarm goes off

    }
}
Run Code Online (Sandbox Code Playgroud)

第二次警报:

public class yourSecondAlarmReceiverextends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {

    //Do something when second alarm goes off

    }
}
Run Code Online (Sandbox Code Playgroud)

应该可以帮助你。