我想在按下按钮时启动 AlarManager。问题是:当我启动应用程序时,AM 就会启动:/
我的代码:
public void scheduleAlarm()
{
int time = 10 * 1000;
intentAlarm = new Intent(this, AlarmReciever.class);
alarm = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
alarm.setRepeating(AlarmManager.RTC_WAKEUP, time, time, PendingIntent.getBroadcast(this,1, intentAlarm, PendingIntent.FLAG_UPDATE_CURRENT));
}
我尝试在按钮的 onClickListener 中调用它。但它从 Activity 开始开始:/
有人可以帮助我吗?
最佳答案
感谢您发布您的代码。试试这个:
public static String ALARM_TO_SET = "ALRMTOSEND";
yourButton.setOnClickListener(new OnClickListener(){
public void onClick(View view){
int time = 10 * 1000;
intentAlarm = new Intent(ALARM_TO_SET);
alarm = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
PendingIntent pIntent = PendingIntent.getBroadcast(this, 0, intentAlarm, 0)
alarm.setRepeating(AlarmManager.RTC_WAKEUP, time, time, pIntent);
}
});
您的广播接收器应在 Manifest.xml 文件中注册为:
<receiver android:name=".AlarmRecieverClass">
<intent-filter>
<action android:name="ALRMTOSEND" />
</intent-filter>
</receiver>
关于java - 单击按钮时启动 AlarmManager,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17742257/