Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/231.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/oracle/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android AlarmManager延迟启动或不启动';别开枪_Android_Alarmmanager - Fatal编程技术网

Android AlarmManager延迟启动或不启动';别开枪

Android AlarmManager延迟启动或不启动';别开枪,android,alarmmanager,Android,Alarmmanager,大家好,我正在学习如何在Android中使用AlarmManager和BroadcastReceiver。 我在使用AlarmManager时遇到一些问题: 我在一分钟的距离内设置了两个警报,但只有一个警报触发,而且已经晚了几分钟(我想这是不可预测的) 这是我的主要活动(我通过点击按钮设置警报) 我的听筒只是简单地发出祝酒词,让手机震动。 Constants.EXTENDED_DATA_STATUS是一个来自Constants类的字符串,它被添加到Android清单中my Reveiver的意图

大家好,我正在学习如何在Android中使用
AlarmManager
BroadcastReceiver
。 我在使用
AlarmManager时遇到一些问题
: 我在一分钟的距离内设置了两个警报,但只有一个警报触发,而且已经晚了几分钟(我想这是不可预测的)

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

我的听筒只是简单地发出祝酒词,让手机震动。
Constants.EXTENDED_DATA_STATUS
是一个来自
Constants
类的字符串,它被添加到Android清单中my Reveiver的
意图过滤器中

我错过了什么?我在谷歌上搜索了很多,只发现我不得不使用
setExact()
,但运气不好


提前感谢

之所以只有一个触发,是因为你的总是取消你的第一个

从文档中:如果已经为同一意向书安排了报警,则之前的报警将首先被取消

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

-要让您的接收器发射多次,请在您的
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);
在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>
第二次报警:

public class yourFirstAlarmReceiver extends BroadcastReceiver {

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

    //Do something when first alarm goes off

    }
}
public class yourSecondAlarmReceiverextends BroadcastReceiver {

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

    //Do something when second alarm goes off

    }
}

应该可以帮助你。

之所以只有一个触发,是因为你的取消了你的第一个总是

从文档中:如果已经为同一意向书安排了报警,则之前的报警将首先被取消

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

-要让您的接收器发射多次,请在您的
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);
在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>
第二次报警:

public class yourFirstAlarmReceiver extends BroadcastReceiver {

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

    //Do something when first alarm goes off

    }
}
public class yourSecondAlarmReceiverextends BroadcastReceiver {

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

    //Do something when second alarm goes off

    }
}

应该可以帮助您。

是的,当然上述代码只会触发一个警报,因为您为两个警报设置了相同的id

报警通过其在PendingEvent中的id进行识别和区分

对不同的报警使用不同的id

更新代码:

pendingIntent = PendingIntent.getBroadcast(getApplicationContext(), 1234 ,intent,0);
alarmManager.setExact(AlarmManager.RTC_WAKEUP,firstLullo.getTimeInMillis(),pendingIntent);

pendingIntent = PendingIntent.getBroadcast(getApplicationContext(), 5678, intent,0);
alarmManager.setExact(AlarmManager.RTC_WAKEUP,secondLullo.getTimeInMillis(),pendingIntent);

是的,当然上述代码只会触发一个报警,因为您为两个报警设置了相同的id

报警通过其在PendingEvent中的id进行识别和区分

对不同的报警使用不同的id

更新代码:

pendingIntent = PendingIntent.getBroadcast(getApplicationContext(), 1234 ,intent,0);
alarmManager.setExact(AlarmManager.RTC_WAKEUP,firstLullo.getTimeInMillis(),pendingIntent);

pendingIntent = PendingIntent.getBroadcast(getApplicationContext(), 5678, intent,0);
alarmManager.setExact(AlarmManager.RTC_WAKEUP,secondLullo.getTimeInMillis(),pendingIntent);

你可以发布你的
onReceive()
部分警报吗?你可以发布你的
onReceive()
部分警报吗?还是一样pendingIntent@Strider检查id,PendingEvent值已更改,您如何在不正确知道的情况下进行向下投票,请向上投票,否则,广播的意图仍然是一样的,所以两个都调用相同的广播,这意味着第二个广播将自动取消第一个广播。我很感谢删除下一票。使用相同的名称将节省内存。仍然相同pendingIntent@Strider检查id,PendingEvent值已更改,您如何在不正确知道的情况下进行向下投票,请upvoteNo它不是,广播的意图仍然相同,因此双方调用相同的广播,wich意味着第二个将自动取消第一个。这没关系,我很感谢删除否决票。使用相同的名称将节省内存。不同的PendingEvent和具有不同值的PendingEvent之间有什么区别。当值更改时,它只会变得不同。为什么我要创建不同的接收器?我的意思是,如果未决的意图具有不同的ID,为什么它们被认为是相同的?如果我想发出n次警报,我应该创建n个接收器吗?这对我来说不太合适:/n这是一个例子,如果你想同时使用多个AlarmReceiver,你不需要使用它,如果你想让一个接收器触发多次,那么你应该在你的
onReceive()
中重新安排你的报警。我写这篇文章只是因为你说:
我设置了两个警报
对不起,我当时解释得很糟糕。顺便说一句,问题是我使用了相同的挂起意图,没有更改id,现在它工作了!不同的PendingEvent和具有不同值的PendingEvent之间有什么区别。当值更改时,它只会变得不同。为什么我要创建不同的接收器?我的意思是,如果未决的意图具有不同的ID,为什么它们被认为是相同的?如果我想发出n次警报,我应该创建n个接收器吗?这对我来说不太合适:/n这是一个例子,如果你想同时使用多个AlarmReceiver,你不需要使用它,如果你想让一个接收器触发多次,那么你应该在你的
onReceive()
中重新安排你的报警。我写这篇文章只是因为你说:
我设置了两个警报
对不起,我当时解释得很糟糕。顺便说一句,问题是我使用了相同的挂起意图,没有更改id,现在它工作了!