1
votes

I want to start multiple alarm inside BroadcastReceiver and starting service from that alarm but didn't start running. I was just tested it using context.startService(intent) and it was succeed. But somehow if i'm using alarmManager the service didn't start.

This is my code :

    @TargetApi(Build.VERSION_CODES.LOLLIPOP)
    @Override
    protected void onPostExecute(String s) {
        super.onPostExecute(s);
            String weekDay;
            SimpleDateFormat dayFormat = new SimpleDateFormat("EEEE", Locale.US);
            Calendar calendars = Calendar.getInstance();
            weekDay = dayFormat.format(calendars.getTime());
            //Toast.makeText(contexts,"Day : " + weekDay,Toast.LENGTH_SHORT).show();
            for (int i=1; i< jArray; i++)
            {
                String hari = hari_list.get(i);
                if (weekDay.equals(hari))
                {
                    Intent intent = new Intent(contexts, Service3.class);
                    pendingIntent = PendingIntent.getBroadcast(contexts, i, intent, PendingIntent.FLAG_ONE_SHOT);
                    AlarmManager manager = (AlarmManager) contexts.getSystemService(Context.ALARM_SERVICE);
                    Calendar calendar = Calendar.getInstance();
                    calendar.set(Calendar.HOUR_OF_DAY,8);
                    calendar.set(Calendar.MINUTE, 16);
                    calendar.set(Calendar.SECOND, 00);   
                    manager.setExact(manager.RTC_WAKEUP,calendar.getTimeInMillis(),pendingIntent);
                }
            }

Could you help me ?

2

2 Answers

1
votes

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.

0
votes

you can only call an receiver using an alarmmanager.

try to call an receiver and start the service from that receiver