How to Start Service Using Alarm Manager in Android

How to start Service using Alarm Manager in Android?

This is what I have used, for starting service after 30 seconds from current time,

Intent intent = new Intent(DashboardScreen.this, ServiceClass.class);
PendingIntent pintent = PendingIntent.getService(DashboardScreen.this, 0, intent, 0);
AlarmManager alarm = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
alarm.setRepeating(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), 30*1000, pintent);

Try it, and let me know what happen...

EDIT:

In your manifest.xml file

 <service android:enabled="true" android:name=".ServiceClass" />

start a service with Alarmmanager at specific date and time and Repeating

it's better to use job scheduler or firebase dispatcher for your requirement

refer this links
https://medium.com/google-developers/scheduling-jobs-like-a-pro-with-jobscheduler-286ef8510129

https://github.com/firebase/firebase-jobdispatcher-android

Start service periodically with AlarmManager

Edited

Regarding the first question :
See this from the developer website

  setInexactRepeating(), you have to use       one of the AlarmManager interval constants--in this case, AlarmManager.INTERVAL_DAY.

So change your 1000 to use of of the constans

Regarding your other question you could override the application object and start it there. This way it is only called when launching the app.

Using Alarmmanager to start a service at specific time

HI friends,
After a lot of researching and with reference from "Pentium10"'s question on the same topic i managed to get it working. Though i still cant understand why the "date" concept and the Calendar(non GregorianCalendar) object which i have mentioned in the question are not working correctly.

Calendar cur_cal = new GregorianCalendar();
cur_cal.setTimeInMillis(System.currentTimeMillis());//set the current time and date for this calendar

Calendar cal = new GregorianCalendar();
cal.add(Calendar.DAY_OF_YEAR, cur_cal.get(Calendar.DAY_OF_YEAR));
cal.set(Calendar.HOUR_OF_DAY, 18);
cal.set(Calendar.MINUTE, 32);
cal.set(Calendar.SECOND, cur_cal.get(Calendar.SECOND));
cal.set(Calendar.MILLISECOND, cur_cal.get(Calendar.MILLISECOND));
cal.set(Calendar.DATE, cur_cal.get(Calendar.DATE));
cal.set(Calendar.MONTH, cur_cal.get(Calendar.MONTH));
Intent intent = new Intent(ProfileList.this, IntentBroadcastedReceiver.class);
PendingIntent pintent = PendingIntent.getService(ProfileList.this, 0, intent, 0);
AlarmManager alarm = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
alarm.setRepeating(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), 30*1000, pintent);

Starting service with Alarm Manager on Android?

To start a service with a PendingIntent, you need to use the static method PendingIntent.getService() to retrieve the appropriate PendingIntent.

Your code seems to be using PendingIntet.getBroadcast() for whatever reason. If you intend to have the AlarmManager indeed send a broadcast, you can start your service in your BroadcastReceiver, else change that line to getService():

from

alarmIntent = PendingIntent.getBroadcast(getApplicationContext(), 0, alarmServiceIntent, 0);

to

alarmIntent = PendingIntent.getService(getApplicationContext(), 0, alarmServiceIntent, 0);

scheduling service using AlarmManager in android

You have to use

alarmManager.setInexactRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), AlarmManager.INTERVAL_DAY, alarmIntent);

instead of

alarmManager.setInexactRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), AlarmManager.INTERVAL_DAY, alarmIntent);

How to keep a service alive using AlarmManager.setInexactRepeating()?

Your service is not starting because of AlarmManager.ELAPSED_REALTIME_WAKEUP, while it should be using AlarmManager.RTC_WAKEUP

If you want to run every 10s keep in mind that above API 21 alarm intervals below 60s are rounded up to 60s.

Also, consider using WakefulIntentService
https://github.com/commonsguy/cwac-wakeful

using AlarmManager.setRepeating to run a service every 5 seconds

Two things that I noticed here is:

1.Name of your broadcast receiver is AlarmReceiver. But in code you are using AlarmManager.class

AlarmManager am = (AlarmManager)getSystemService(ALARM_SERVICE);
Intent i = new Intent(getBaseContext(), **AlarmManager.class**);
PendingIntent pendingIntent = PendingIntent.getBroadcast(
MainActivity.this, 0, i, PendingIntent.FLAG_UPDATE_CURRENT);


AlarmManager am = (AlarmManager)getSystemService(ALARM_SERVICE);
Intent i = new Intent(getBaseContext(), **AlarmReceiver.class**);
PendingIntent pendingIntent = PendingIntent.getBroadcast(
MainActivity.this, 0, i, PendingIntent.FLAG_UPDATE_CURRENT);

  1. I cannot find the declaration of your broadcast Receiver (AlarmReceiver) in AndroidManifest.xml.

Fix these two and it should work. Enjoy :)

Start Service from BroadcastReceiver with AlarmManager

At the moment you are sending out a Broadcast Intent that is configured like a service Intent.

Intent intent = new Intent(contexts, Service3.class);
pendingIntent = PendingIntent.getBroadcast(contexts, i, intent, PendingIntent.FLAG_ONE_SHOT);

Try getService instead of getBroadcast to directly start the service in the alarm manager.

If you want to use a Broadcast you need to send a correct Broadcast Intent and register a listener to that Broadcast in your Manifest.

Using AlarmManager to run a Service when App is not running


i found this code in internet and it works, may be  can help you.

package YourPackage;
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.PowerManager;
import android.widget.Toast;

public class Alarm extends BroadcastReceiver
{
@Override
public void onReceive(Context context, Intent intent)
{
PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "");
wl.acquire();

// Put here YOUR code.
Toast.makeText(context, "Alarm !!!!!!!!!!", Toast.LENGTH_LONG).show(); // For example

wl.release();
}

public void SetAlarm(Context context)
{
AlarmManager am=(AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
Intent i = new Intent(context, Alarm.class);
PendingIntent pi = PendingIntent.getBroadcast(context, 0, i, 0);
am.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(),1000 * 60 * ,pi);
}

public void CancelAlarm(Context context)
{
Intent intent = new Intent(context, Alarm.class);
PendingIntent sender = PendingIntent.getBroadcast(context, 0, intent, 0);
AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
alarmManager.cancel(sender);
}
}


//Set Alarm from Service:


package YourPackage;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.os.IBinder;

public class YourService extends Service
{
Alarm alarm = new Alarm();
public void onCreate()
{
super.onCreate();
}

@Override
public int onStartCommand(Intent intent, int flags, int startId){
alarm.SetAlarm(YourService.this);
return START_STICKY;
}



public void onStart(Context context,Intent intent, int startId)
{
alarm.SetAlarm(context);
}

@Override
public IBinder onBind(Intent intent)
{
return null;
}
}
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"></uses-permission>
...
<receiver android:name=".AutoStart">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"></action>
</intent-filter>
</receiver>




package YourPackage;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;

public class AutoStart extends BroadcastReceiver
{
Alarm alarm = new Alarm();
@Override
public void onReceive(Context context, Intent intent)
{
if (intent.getAction().equals("android.intent.action.BOOT_COMPLETED"))
{
alarm.SetAlarm(context);
}
}
}


Related Topics



Leave a reply



Submit