我想在按下按钮时启动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中调用它。但它从活动开始:/
任何人都可以帮助我吗?
答案 0 :(得分:1)
感谢发布您的代码。试试这个:
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>
答案 1 :(得分:0)
您应该在setRepeating
方法
onClickListener
来电
答案 2 :(得分:0)
为什么不为此使用timerTask ..
答案 3 :(得分:0)
试试这个:
Button yourButton = (Button)findViewById(R.id.yourId);
yourButton.setOnClickListener(new OnClickListener(){
public void onClick(View view){
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));
}
});