Scheduling an event in AlarmManager on android - android

I have already read some tutorials and read the documentation, but I can't make this work... This is what I have been testing:
This is the way I register the intent to be called with the alarm manager:
Calendar cal = Calendar.getInstance();
cal.add(Calendar.MINUTE, 1)
PendingIntent sender = PendingIntent.getBroadcast(this, 192837, new Intent(this, AlarmReceiver.class), PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
am.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), sender);
My AlarmReceiver:
public class AlarmReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
Intent intent2 = new Intent(context, NewCommit.class);
context.startActivity(intent2);
}
}
And of course I added the receiver in the androidManifest.xml:
<receiver android:process=":remote" android:name="AlarmReceiver"></receiver>
And is INSIDE the application tag.
Any idea? It's driving me crazy, I can't find what's wrong!
Thanks

I just added to the intent from pendingIntent:
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
I hope in helps someone!

You need to put permission in AndroidManifest.xml
<uses-permission android:name="com.android.alarm.permission.SET_ALARM" />

Related

How do I make alarmmanager execute after my application has been closed?

I want to make my alarmmanager to rings although after my application closed.
But now my code don`t call broadcastreceiver after my application closed.
I reg my broadcastreceiver in my manifest.
This is code that setting alarmmanager
final AlarmManager alarmManager=(AlarmManager) getSystemService(ALARM_SERVICE);
Intent intent=new Intent(setTimeActivity.this,autoCheckReceiver.class);
intent.setAction("com.dayo.selfcheck.autoCheckReceiver");
final PendingIntent pendingIntent=PendingIntent.getBroadcast(setTimeActivity.this,0,intent,PendingIntent.FLAG_UPDATE_CURRENT);
alarmManager.cancel(pendingIntent);
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY, timePicker.getHour());
calendar.set(Calendar.MINUTE, timePicker.getMinute());
calendar.set(Calendar.SECOND, 0);
calendar.set(Calendar.MILLISECOND, 0);
alarmManager.setInexactRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), AlarmManager.INTERVAL_DAY, pendingIntent);
And this code is my broadcastreceiver.
public class autoCheckReceiver extends BroadcastReceiver {
private String TAG="autoCR";
#Override
public void onReceive(Context context, Intent it) {
Log.d(TAG,"asdf");
Intent i = new Intent();
i.setClassName("com.dayo.selfcheck", "com.dayo.selfcheck.MainActivity");
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(i);
}
}
this is my manifest file.
<receiver
android:name=".autoCheckReceiver"
android:exported="false">
<intent-filter>
<action android:name="com.dayo.selfcheck.autoCheckReceiver"></action>
</intent-filter>
</receiver>
thanks!
You MUST specify android:exported="true" on your <receiver> declaration in order for AlarmManager to launch your BroadcastReceiver.
You don't need to call cancel() on the AlarmManager to cancel the Intent before you schedule it, as AlarmManager automatically does this.
You do not need to set the ACTION in the Intent for the broadcast, since you are using an explicit Intent (you have specified the exact component to be used).
You also do not need to provide the <intent-filter> in your <receiver> declaration because you are using an explicit broadcast.

Receiver not calling in Alarm Manager android

Hi I need to use notification for particular date in android. So I used alarm manager to trigger in receiver. But Broadcast receiver is not calling. Here is my code.
Here is piece of code..
In Details activity:
Log.e("alarm set", "cal" + cal.getTime());
setAlarm(cal);
private void setAlarm(Calendar targetCal){
//this log got printed.
Log.e("alarm set",""+targetCal.getTime());
Intent intent = new Intent(Details.this, AlarmReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(Details.this, 0, intent, 0);
AlarmManager alarmManager = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC_WAKEUP, targetCal.getTimeInMillis(), pendingIntent);
}
But AlarmReceiver is not calling.
public class AlarmReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent arg1) {
//this is not working.
Log.e("alarm",";alarm");
Toast.makeText(context, "Alarm received!", Toast.LENGTH_LONG).show();
Intent service1 = new Intent(context, AlarmService.class);
context.startService(service1);
}
}
I given permission as,
<uses-permission android:name="com.android.alarm.permission.SET_ALARM"/>
<receiver
android:name=".AlarmReceiver"
android:process=":remote" />
<service
android:name=".AlarmService"
android:enabled="true"
android:exported="true"></service>
I couldn't find what mistake I am doing.
Thanks in advance.

My Alarm Service don't fire

I have searched in everywhere but I can understand because my Service at a specific time of the day don't fire. Nothing happens.
My code for set up the Alarm:
AlarmManager alarmMgr = (AlarmManager) act.getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(act, AlarmReceiver.class);
PendingIntent alarmIntent = PendingIntent.getBroadcast(act, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT);
// set for 5 seconds later
alarmMgr.set(AlarmManager.RTC_WAKEUP, Calendar.getInstance().getTimeInMillis()+5000, alarmIntent);
My code for the Service:
public class AlarmReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
System.out.println("Alarm worked well");
Utils.criarNotificacao(context);
}
}
In the Manifest:
<receiver android:name="br.com.blacktoad.q48h2.utils.AlarmReceiver" android:process=":remote"></receiver>
I can't find any answer.
I find the error!
I need put the:
<receiver android:name="br.com.blacktoad.q48h2.utils.AlarmReceiver" android:process=":remote"></receiver>
in the manifest, but contained in the "application" tag

BroadcastReceiver not being called?

I using a Alarmmanager in the launch activity to execute something in BroadcastReceiver repeatedly, but BroadcastReceiver not being triggered. my code is as follow:
MainActivity.class:
AlarmManager manager = (AlarmManager) getBaseContext().getSystemService(Context.ALARM_SERVICE);
Intent gpsintent =new Intent(getBaseContext(),gps_sendback_alarm_receiver.class);
PendingIntent.getService(getBaseContext(), 0, gpsintent, PendingIntent.FLAG_UPDATE_CURRENT);
Calendar now = Calendar.getInstance();
manager.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, now.getTimeInMillis(), int_gps_sendback_period, pendingIntent);
gps_sendback_alarm_receiver.class:
public void onReceive(Context context, Intent intent) {
//send back the GPS location
}
manifest:
<application>
<receiver android:name="com.example.xmobiler.gps_sendback_alarm_receiver" android:enabled="true"/>
</application>
anybody can help?
you are creating the variable gpsintent but the alarmmanager gets pendingIntent that is not assigned in your example code

BroadcastReceiver is not called

I have to following code to call the BroadcastReceiver:
public class WorkItemAlarmManager {
...
public void initAlarm(HelpMe helpMe, String delay,String period,WorkItem workItem){
//HelpMe = Activity
helpMe.registerReceiver(new WorkItemAlarmHandler() , new IntentFilter("WORK_ITEM_ALARM"));
Intent intent = new Intent("WORK_ITEM_ALARM");
intent.setClass(helpMe, WorkItemAlarmManager.class);
intent.putExtra("work_item", workItem);
PendingIntent mAlarmSender = PendingIntent.getService(helpMe, 0,intent, 0);
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
//System.out.println("START"+System.currentTimeMillis());
calendar.add(Calendar.SECOND, calcDelayMillis(delay));
//System.out.println("END"+calendar.getTimeInMillis());
AlarmManager am = (AlarmManager) helpMe.getSystemService(Context.ALARM_SERVICE);
am.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), mAlarmSender);
}
...
}
and the BroadcastReceiver:
public class WorkItemAlarmHandler extends BroadcastReceiver{
#Override
public void onReceive(Context context, Intent intent) {
....
}
}
But the WorkItemAlarmHandler is never called.
Update:
I tried to register the BroadcastReceiver in the manifest at first. But that didn't work:
<receiver
android:name="de.helpme.alarm.WorkItemAlarmHandler"
android:enabled="true"
android:label="WorkItemAlarmHandler" >
<intent-filter>
<action android:name="WORK_ITEM_ALARM" />
</intent-filter>
</receiver>
Thanks for your answers :). Now it works - but i dont yet know which one is the correct because i implemented both answers at once. If a figure out which was the right one i mark the right as answer.
Update:
The cause of the problem as i see it was:
PendingIntent mAlarmSender = PendingIntent.getService(helpMe, 0,intent, 0);
With this code it works fine:
PendingIntent mAlarmSender = PendingIntent.getBroadcast(helpMe, 0, intent, 0);
Thanks again for all your help!
try this :
In WorkItemAlarmManager ;
public class WorkItemAlarmManager {
...
public void initAlarm(HelpMe helpMe, String delay,String period,WorkItem workItem){
Intent intent =new Intent(helpMe, WorkItemAlarmHandler.class);
intent.setAction("WORK_ITEM_ALARM");
intent.putExtra("work_item", workItem);
PendingIntent mAlarmSender=
PendingIntent.getBroadcast(helpMe, 0, intent, 0);
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
//System.out.println("START"+System.currentTimeMillis());
calendar.add(Calendar.SECOND, calcDelayMillis(delay));
//System.out.println("END"+calendar.getTimeInMillis());
AlarmManager am = (AlarmManager) helpMe.getSystemService(Context.ALARM_SERVICE);
am.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), mAlarmSender);
}
...
}
and in WorkItemAlarmHandler:
public class WorkItemAlarmHandler extends BroadcastReceiver{
#Override
public void onReceive(Context context, Intent intent) {
if(intent.getAction().equals("WORK_ITEM_ALARM")){
Toast.makeText(context, "WORK_ITEM_ALARM", Toast.LENGTH_LONG).show();
}else{
Toast.makeText(context, "repeating alarm",
Toast.LENGTH_LONG).show();
}
}
}
Your code uses:
intent.setClass(helpMe, WorkItemAlarmManager.class);
but with the setClass(...) method you specify the class that should finally handle the intent.
Therefore you should write:
intent.setClass(helpMe, WorkItemAlarmHandler.class);
As far as I remember, if you specify the class or component manually (eg. with the setClass() method) this object has to be specified in the manifest, so make sure the receiver is mentioned there.

Categories

Resources