How to trigger alarm in a BroadcastReceiver? - android

I want to start alarm when onReceive() method of broadcastreceiver is invoked.

Use this below code
Calendar cal = Calendar.getInstance();
cal.setTimeInMillis(System.currentTimeMillis());
cal.clear();
cal.set(2012,2,8,18,16);
AlarmManager alarmMgr = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(this, AlarmReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, intent, 0);
// cal.add(Calendar.SECOND, 5);
alarmMgr.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), pendingIntent);
You can use context in onReceive(Context context, Intent intent) instead of this in the above code.

Related

I can't add an Alarm Clock for intent error

I would create an alarm clock, I wrote this code but return this error:
2019-02-05 10:58:13.902 2663-10077/com.google.android.gms E/ChromeSync: [Sync,SyncIntentOperation] Error handling the intent:
Intent { act=android.intent.action.PACKAGE_ADDED
dat=package:com.example.iacopo.alarmgroup flg=0x4000010
cmp=com.google.android.gms/.chimera.GmsIntentOperationService (has
extras) }.
How can I fix it? Thanks. And this code doesn't create an icon of alarm in the notification panel, near the clock.
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Calendar cal = Calendar.getInstance();
cal.setTimeInMillis(System.currentTimeMillis());
cal.clear();
cal.set(2018,1,5,10,0);
AlarmManager alarmMgr = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(MainActivity.this, AlarmReceiver.class);
this.startService(intent);
PendingIntent pendingIntent = PendingIntent.getBroadcast(MainActivity.this, 0, intent, 0);
alarmMgr.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), pendingIntent);
}
}
and the receiver
public class AlarmReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
Log.i("test","ok");
}
}
cal.set(2018,1,5,10,0);
The calendar takes earlier than the current time, so your alarm clock is unlikely to be triggered. Change the time correctly and try again.
This is a basic snippet from developer.android.com. You can change date and time as per your needs and pass the calendar instance to Alarm Manager intent.
// Example: Wake up the device to fire a one-time (non-repeating) alarm in one minute:
private AlarmManager alarmMgr;
private PendingIntent alarmIntent;
alarmMgr = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(context, AlarmReceiver.class);
alarmIntent = PendingIntent.getBroadcast(context, 0, intent, 0);
alarmMgr.set(AlarmManager.ELAPSED_REALTIME_WAKEUP,
SystemClock.elapsedRealtime() +
60 * 1000, alarmIntent);
// Example: Set the alarm to start at approximately 2:00 p.m. say
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.set(Calendar.HOUR_OF_DAY, 14);
// With setInexactRepeating(), you have to use one of the AlarmManager interval
// constants--in this case, AlarmManager.INTERVAL_DAY.
alarmMgr.setInexactRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),
AlarmManager.INTERVAL_DAY, alarmIntent);

Android cancelling pending intent not working

I am facing this really weird problem. I have made a working alarm system. But when I delete the alarm, it calls it.
AlarmBuddy.java (Working class to set original alarm)
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.MONTH, m-1);
calendar.set(Calendar.DAY_OF_MONTH, d);
calendar.set(Calendar.HOUR, 8);
calendar.set(Calendar.MINUTE, repeat%60);
final Intent myIntent = new Intent(this.context, AlarmMiddle.class);
myIntent.putExtra("name", name);
myIntent.putExtra("id", repeat);
alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
super.onBackPressed();
PendingIntent pending_intent = PendingIntent.getBroadcast(AlarmBuddy.this, repeat, myIntent, PendingIntent.FLAG_UPDATE_CURRENT);
alarmManager.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pending_intent);
ListRecord.java THE FLAW
public Boolean cancelAlarm(int id){
Intent myIntent = new Intent();
PendingIntent.getBroadcast(context, id, myIntent,
PendingIntent.FLAG_CANCEL_CURRENT).cancel();
return false;
}
This function is to cancel the alarm. This has some problem to say this again:
The code can SET AND CALL an alarm. But when I delete it from a DIFFERENT class, it immediately calls the alarm. Maybe I am just a noob. Or maybe this is an unsolvable problem.
This is working for me at all in my application see link
Intent intent = new Intent(getBaseContext(), Your_Service.class);
PendingIntent pendingIntent = PendingIntent.getService(getBaseContext(), reqcode, intent, 0);
AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
alarmManager.cancel(pendingIntent);
To cancel the alarm, you need to use the same PendingIntent as you used to set it. You should also tell the AlarmManager to cancel the alarm as well as canceling the PendingIntent. Try this:
public Boolean cancelAlarm(int id) {
Intent myIntent = new Intent(context, AlarmMiddle.class);
PendingIntent pending_intent = PendingIntent.getBroadcast(context, id, myIntent,
PendingIntent.FLAG_NO_CREATE);
if (pending_intent != null) {
alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
alarmManager.cancel(pending_intent);
pending_intent.cancel();
}
return false;
}

I can't set repeating of AlarmManager using setExact()

I tried to set repeating AlarmManager using setExact. In activity:
AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(this, AlarmReceiver.class);
intent.setAction(ACTION_ALARM_RETRY);
PendingIntent sender = PendingIntent.getBroadcast(this, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT);
alarmManager.setExact(AlarmManager.RTC_WAKEUP, 7000, sender);`
And in receiver after my operations I have:
public void onReceive(Context context, Intent intent){
//Here are my operations...
AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(context, AlarmReceiver.class);
intent.setAction(ACTION_ALARM_RETRY);
PendingIntent sender = PendingIntent.getBroadcast(context, 1, intet, PendingIntent.FLAG_CANCEL_CURRENT);
alarmManager.setExact(AlarmManager.RTC_WAKEUP, 7000, sender);
}
But it repeats all time without foggiest pause...
Help please!

Start app at a specific time in Android

I want to display a notification from my application at a specific time say 6.30 am daily. I have successfully done that. Following is the code which executes at specified time. However the code works only if the app is in open state or put to background. It does not work if I reboot the device and don't launch the app, also does not work if I kill the app .
AlarmManager alarmMgr = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
PendingIntent alarmIntent;
alarmMgr = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(MainActivity.this, MyReceiver.class);
alarmIntent = PendingIntent.getBroadcast(MainActivity.this, 0, intent, 0);
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.set(Calendar.HOUR_OF_DAY,6);
calendar.set(Calendar.MINUTE,30);
calendar.set(Calendar.AM_PM,Calendar.AM);
alarmMgr.setInexactRepeating(AlarmManager.RTC_WAKEUP,
calendar.getTimeInMillis(), AlarmManager.INTERVAL_DAY,
alarmIntent);
Intent myIntent = new Intent(MainActivity.this, MyReceiver.class);
pendingIntent = PendingIntent.getBroadcast(MainActivity.this, 0, myIntent,0);
AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC, calendar.getTimeInMillis(), pendingIntent);
Following is the receiver BroadCast method
#Override
public void onReceive(Context context, Intent intent) {
Intent service1 = new Intent(context, MyAlarmService.class);
context.startService(service1);
}
The alarm service class displays a notification.
/...Display notification.../

Alarm not getting cancelled

I am setting the alarm in one activity, and want to cancel it in another class which is a service. Following is my code:
Intent intent = new Intent(SetAlarm.this, AlertFriend.class);
PendingIntent pi = PendingIntent.getActivity(SetAlarm.this, unique_id, inent,
PendingIntent.FLAG_UPDATE_CURRENT);
Calendar c1 = Calendar.getInstance();
c1.set(Calendar.HOUR_OF_DAY, hour);
c1.set(Calendar.MINUTE, min);
c1.set(Calendar.SECOND, 0);
c1.set(Calendar.MILLISECOND, 0);
AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC_WAKEUP, c1.getTimeInMillis(), pi);
And following is where i am deleting the alarm (where a is pi):
Intent intent = new Intent(context, AlertFriend.class);
AlarmManager alarm = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
alarm.cancel(PendingIntent.getActivity(context, a, intent,PendingIntent.FLAG_UPDATE_CURRENT));

Categories

Resources