1

I have a receiver inside a service as i need the scheduled alarm to work even when the activity is destroyed. This is what i have done.

/**
 * Created by rishabh on 14/2/16.
 */
public class MyService  extends Service {

@Nullable
@Override
public IBinder onBind(Intent intent) {
    return null;
}

public class MyReceiver extends BroadcastReceiver{

    @Override
    public void onReceive(Context context, Intent intent) {
        Calendar c = Calendar.getInstance();
        int hour=c.get(Calendar.HOUR_OF_DAY);
        int minute=c.get(Calendar.MINUTE);
        Calendar calendar = new GregorianCalendar(1990, 1, 1, hour, minute);
        SimpleDateFormat sdf = new SimpleDateFormat("hh:mm");
        String date = sdf.format(calendar.getTime());
        String str=date.charAt(0)+""+date.charAt(1)+""+date.charAt(3)+""+date.charAt(4);
        String LOG_TAG="DevicePolicyAdmin";
        Log.v(LOG_TAG, "Service Started");
        MainActivity.minochaDevicePolicyManager.resetPassword(str, 0);
    }
}
}

And to trigger the receiver the scheduling alarm is here

    Intent intent3=new Intent(MainActivity.this,MyService.class);
    startService(intent3);
            Intent intent2 = new Intent(MainActivity.this, MyService.MyReceiver.class);
            PendingIntent pintent = PendingIntent.getService(MainActivity.this, 0, intent2, 0);
            AlarmManager alarm = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
            alarm.setRepeating(AlarmManager.RTC_WAKEUP, c.getTimeInMillis(), 60 * 1000, pintent);

The onReceive method in the MyService class only executes once. why? What could be the issue. Please help

AxeManTOBO
  • 487
  • 1
  • 4
  • 17

3 Answers3

0

you need to register it in service as follows,

IntentFilter filter = new IntentFilter();
  filter.addAction("SOME_ACTION");
  filter.addAction("SOME_OTHER_ACTION");

context.registerReceiver(receiver, filter);

Also in onReceive method you need to check for the action of broadcast receiver fired for.

Hope this will help you.

Silvans Solanki
  • 1,267
  • 1
  • 14
  • 27
0

One of your problems is that your getting a service using the intent which use the broadcast receiver. Start changing this:

PendingIntent pintent = PendingIntent.getService(MainActivity.this, 0, intent2, 0);

For this:

PendingIntent pintent = PendingIntent.getService(MainActivity.this, 0, intent3, 0);
Miguel Benitez
  • 2,322
  • 10
  • 22
0

Try moving MyReceiver to an independent file as public class and use this code to start.

 Intent intent2 = new Intent(getApplicationContext(), MyReceiver.class);
            PendingIntent pintent = PendingIntent.getService(getApplicationContext(), 0, intent2, 0);
            AlarmManager alarm = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
            alarm.setRepeating(AlarmManager.RTC_WAKEUP, c.getTimeInMillis(), 60 * 1000, pintent);

Update 1 :

This may help you -How to repeat my service using alarm manager?

Update 2 : Check this too- Android - Periodic Background Service - Advice

Community
  • 1
  • 1
Arun Shankar
  • 2,603
  • 2
  • 26
  • 36