I have class for call myService with alaram Manager. From mainActivity this is my code to set alarm manager.
Intent downloader = new Intent(context, AlarmReceiver.class);
downloader.putExtra("limit", limit.getSelectedItem().toString());
downloader.putExtra("delay",perMin.getSelectedItem().toString());
downloader.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); `pendingIntent = PendingIntent.getBroadcast(context, 0, downloader,PendingIntent.FLAG_CANCEL_CURRENT);
alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
int delay = Integer.parseInt(perMin.getSelectedItem().toString());
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP,10 * 1000, delay * 1000, pendingIntent);
and here my alarm receiver class
public class AlarmReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
Intent update = new Intent(context, IntervalService.class);
Log.i(tag, "Call service from Alaram Receiver");
context.startService(update);
}}
I know when i want to cancel alarm manager from main activity easily call alarmManager.cancel(pendingIntent) but i want to cancel alarm manager to complete my service task and after that set again like as above.
Just modified your code,
public void setAlarmManager(boolean cancel){
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0,downloader,PendingIntent.FLAG_CANCEL_CURRENT);
AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
int delay = Integer.parseInt(perMin.getSelectedItem().toString());
if(!cancel)
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP,10 * 1000, delay * 1000, pendingIntent);
else
alarmManager.cancel(pendingIntent);
}
call this function when needed,
to Start: setAlarmManager(false);
to Cancel: setAlarmManager(true); //Call this function in Service
Try, this may work. Thanks.
Related
I am new to android programming. What am i doing wrong? AlarmReceiver class not triggering...I want to trigger repeatedly the AlarmReceiver.class ...
I used the code from this page https://developer.android.com/training/scheduling/alarms#precision
OnCreate at MainActivity.java i have the following code
AlarmManager alarmManager = (AlarmManager)this.getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(this,AlarmReceiver.class);
//Check if the PendingIntent already exists
PendingIntent pendingIntent =
PendingIntent.getService(this, 6661, intent,
PendingIntent.FLAG_NO_CREATE);
if (pendingIntent != null && alarmManager != null) {
//cancel here if you want
}
//Run every 60 seconds!
alarmManager.setInexactRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP,
SystemClock.elapsedRealtime() + 60000,
60000, pendingIntent);
The AlarmReceiver.java class includes the following code...
public class AlarmReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
// For our recurring task, we'll just display a message
Log.i("logit","testing...");
}
}
Do you have an entry in your manifest for the receiver like that?
<receiver android:name=".AlarmReceiver"></receiver>
Given that the AlarmManager.setInexactRepeating() function works only with APIs less than API 19, I used the AlarmManager.setExact() function for APIs 19 upwards and used a broadcast receiver to perform the required task then call the alarm function again to make it a repeating alarm. The alarm works but it doesn't respect the intended repeat interval. I did some research to see what others did and I'm at my wit's end.
I created a class to handle the alarm as shown below:
public class AlarmStart {
public AlarmStart() {
}
public void startAlarm(Context context) {
// Start service
if(Build.VERSION.SDK_INT < 19) {
Intent service = new Intent(context, MyService.class);
PendingIntent pendingIntent = PendingIntent.getService(context, 0, service, 0);
AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
alarmManager.setInexactRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP,
AlarmManager.INTERVAL_DAY, AlarmManager.INTERVAL_DAY,
pendingIntent);
}
else {
Intent intent = new Intent(context, MyBroadcastReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
alarmManager.setExact(AlarmManager.ELAPSED_REALTIME_WAKEUP, 1000 * 60 * 60 * 24, pendingIntent);
}
// Enable receiver when device boots
ComponentName receiver = new ComponentName(context, BootReceiver.class);
PackageManager pm = context.getPackageManager();
pm.setComponentEnabledSetting(receiver,
PackageManager.COMPONENT_ENABLED_STATE_ENABLED,
PackageManager.DONT_KILL_APP);
}
Below is the code in the BroadcastReceiver class which makes a call to the service to be executed at intervals (which works well), then makes another call to the startAlarm() function:
public class MyBroadcastReceiver extends BroadcastReceiver {
AlarmStart alarmStart = new AlarmStart();
public MyBroadcastReceiver() {
}
#Override
public void onReceive(Context context, Intent intent) {
// Start service
Intent service = new Intent(context, MyService.class);
context.startService(service);
//Schedule next alarm
alarmStart.startAlarm(context);
}
In AndroidManifest.xml, I set the enabled and exported attributes of the broadcast receiver to true.
Please can someone tell me what I might be doing wrong?
You have messed up the start time.
replace
alarmManager.setExact(AlarmManager.ELAPSED_REALTIME_WAKEUP, 1000 * 60 * 60 * 24, pendingIntent);
with
alarmManager.setExact(AlarmManager.ELAPSED_REALTIME_WAKEUP, SystemClock.elapsedRealtime()+(1000 * 60 * 60 * 24), pendingIntent);
The same thing with
alarmManager.setInexactRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, AlarmManager.INTERVAL_DAY, AlarmManager.INTERVAL_DAY, pendingIntent);
replace with
alarmManager.setInexactRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, SystemClock.elapsedRealtime()+AlarmManager.INTERVAL_DAY, AlarmManager.INTERVAL_DAY, pendingIntent);
The problem was that you have launched the alarm in wrong moment of time.
I have code which is to be run daily; for this I'm trying to use AlarmManager. This is my code for triggering alarms:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Intent i = new Intent(this, AlarmReciever.class);
PendingIntent pi = PendingIntent.getService(this, 0, i, 0);
AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
am.cancel(pi); // cancel any existing alarms
am.setInexactRepeating(AlarmManager.ELAPSED_REALTIME,
SystemClock.elapsedRealtime() + AlarmManager.INTERVAL_DAY,
AlarmManager.INTERVAL_DAY, pi);
}
This part of the code is calling AlarmReciever class as expected, but I want the code in the AlarmReciever class to be executed only once daily. It's being called multiple times. How do I restrict it?
This is the AlarmReciever class:
public class AlarmReciever extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
System.out.println("in alarm reciever class");
}
}
I'm trying to perform some business logic in the onReceive() method.
In the manifest.xml file:
<receiver android:name="com.xyz.reciever.AlarmReciever"></receiver>
<uses-permission android:name="com.android.alarm.permission.SET_ALARM"/>
are declared.
I believe the code you should be using to set your alarm would be this:
Intent i = new Intent(this, AlarmReciever.class);
PendingIntent pi = PendingIntent.getBroadcast(this, 0, i, 0); // <- HERE!!
AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
am.cancel(pi); // cancel any existing alarms
am.setInexactRepeating(AlarmManager.ELAPSED_REALTIME,
SystemClock.elapsedRealtime() + AlarmManager.INTERVAL_DAY,
AlarmManager.INTERVAL_DAY, pi);
since your AlarmReciever is a BroadcastReceiver. Use PendingIntent.getBroadcast().
If you have much work to do in your receiver's onReceive() method, you might also delegate it to an IntentService. See this answer if you decide to do that.
Have a BroadcastReceiver which I want to create a PendingIntent inside the onReceive method
public class MyPushNotificationReceiver extends BroadcastReceiver {
..
public void onReceive(Context context, Intent intent) {
// How to start a PendingIntent here?
}
Most of the doc from Google is not starting within the onReceive method, so any sample cod e I can use?
Thanks.
a sample code which uses a pending intent in broadcast reciver pls check
public class MyScheduleReceiver extends BroadcastReceiver {
// Restart service every 30 minute
private static final long REPEAT_TIME = 1000 * 30 ;
#Override
public void onReceive(Context context, Intent intent) {
AlarmManager service = (AlarmManager) context
.getSystemService(Context.ALARM_SERVICE);
Intent i = new Intent(context, MyStartServiceReceiver.class);
PendingIntent pending = PendingIntent.getBroadcast(context, 0, i,
PendingIntent.FLAG_CANCEL_CURRENT);
Calendar cal = Calendar.getInstance();
// Start 30 seconds after boot completed
cal.add(Calendar.SECOND, 30);
//
// Fetch every 30 seconds
// InexactRepeating allows Android to optimize the energy consumption
service.setInexactRepeating(AlarmManager.RTC_WAKEUP,
cal.getTimeInMillis(), REPEAT_TIME, pending);
// service.setRepeating(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(),
// REPEAT_TIME, pending);
}
}
this code is from vogella ...
I must create an AlarmManager that repeating every seconds, I use this code
Intent in = new Intent(context,Tempo_Indietro.class);
in.putExtra("id_widget", appWidgetIds[i]);
PendingIntent pi = PendingIntent.getActivity(context, 0, in, PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager alarm = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
alarm.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), 1000, pi);
but it don't start...why ??
To start a Scheduled Activity: you can use like:
Step1: Setting for AlarmManager
Intent intent =new Intent(context,AlarmReceiver.class);
PendingIntent pi = PendingIntent.getBroadcast(context, 0, in, PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager am = (AlarmManager)getSystemService(ALARM_SERVICE);
am.setRepeating(AlarmManger.RTC_WAKEUP,System.currentTimeMillis(), 1000, pi);
Step2: creating a BroadcastReceiver
public class AlarmReceiver extends BroadcastReceiver {
//override onReceive(Context, Intent) method
#Override public void onReceive(Context context, Intent intent)
{
//.........
Intent i = new Intent(context,Tempo_Indietro.class);
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(i);
//..........
}
}
Note: Don’t forget to include the newly created activity, receiver in the AndroidManifest.xml file.