Reputation: 3
so I've made an code in which alarm manager should fire at exact time and repeat every minute, and it partially works, it fires for the first time but it doesn't repeat after given interval(one minute).
Main activity :
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();
}
}
Upvotes: 0
Views: 164
Reputation: 200
Try using setRepeating()
:
manager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),
cal.getTimeInMillis() + 1000*5, 6000, pintent);
For best practices you can see this doc.
Upvotes: 0
Reputation: 85
If its all of your Main Activity i do not understand it. Why do u define button in SetAlarm() ((SetAlarm called on click)) ?!
I think you should try a fully new way. Like this....
In Main Activity onCreate event define button:
Button btn_yrbnt = findViewById(R.id.btn);
Make an event for onClick
btn_yrbnt.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startAlarm();
}
});
Set the alarm in the startAlarm() event
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
Make a new Java class named Alarm.class with BroadCastReceiver superclass
public class Alarm extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
//Type what do u want to do on Alarm's fire!!!!
}}
IMPORTANT! In ur manifest should link the Receiver
<receiver android:name=".Alarm"
android:enabled="true"
android:exported="true"/>
If u do everything good, your alarm works fine! Sry my bad english!
Upvotes: 0
Reputation: 85
I think the minimum repeat in ms is 60000 (1 min) for security, and battery life :)
Value will be forced up to 60000 as of Android 5.1...
Upvotes: 0