Reputation: 663
I want to start a service when wifi network connected.
If I start the service when wifi connected, by using
context.startService(intent_alarm);
it works fine.
I want to start the service for every 10 seconds after recieving broadcast. So I have used AlarmManager
Here is the code:
public class NetworkChangeReceiver extends BroadcastReceiver{
public static AlarmManager am;
public static PendingIntent sender;
@Override
public void onReceive(final Context context, final Intent intent) {
Intent intent_alarm = new Intent(context, MyService.class);
sender = PendingIntent.getBroadcast(context, 0, intent_alarm, 0);
am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
long l = System.currentTimeMillis();
l += 3600L;
am.setRepeating(AlarmManager.RTC_WAKEUP,l, 3600L, sender);
//context.startService(intent_alarm);
}
}
I think here I gave 3.6 seconds as the intervel.
But the service not running, I checked it manually. Please tell me what I'm doing wrong?
Upvotes: 0
Views: 895
Reputation: 22066
you have used PendingIntent.getBroadcast instread of PendingIntent.getService so use this - >
Intent intent_alarm = new Intent(context, MyService.class);
sender = PendingIntent.getService(context, 0, intent_alarm, 0);
Upvotes: 1
Reputation: 15379
You need to tell the alarm manager to start your service:
Intent intent_alarm = new Intent(context, MyService.class);
sender = PendingIntent.getService(context, 0, intent_alarm, 0);
Upvotes: 5