-1

I want to start my intent service with alarm manager and my intent service is being started by service .. but i am unable to do that please help me to do so this is my code service code

public void onCreate() {

        Intent intent = new Intent(this, DownloadService.class);

        startService(intent);

        Calendar cal = Calendar.getInstance();
        cal.add(Calendar.SECOND, 10);

        PendingIntent pintent = PendingIntent.getService(this, 0, intent, 0);
        AlarmManager alarm = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
        alarm.setRepeating(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(),
                10 * 1000, pintent);
        alarm.setInexactRepeating(AlarmManager.RTC_WAKEUP,
                cal.getTimeInMillis(), 10 * 1000, pintent);

    }


    public int onStartCommand(Intent intent, int flags, int startId) {

        Toast t = Toast.makeText(getApplicationContext(), "in service onSatrt commandf ", Toast.LENGTH_LONG);
        t.show();

        intent = new Intent(this, DownloadService.class);

        startService(intent);
        Calendar cal = Calendar.getInstance();
        cal.add(Calendar.SECOND, 10);

        PendingIntent pintent = PendingIntent.getService(this, 0, intent, 0);
        AlarmManager alarm = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
        alarm.setRepeating(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(),
                10 * 1000, pintent);
        alarm.setInexactRepeating(AlarmManager.RTC_WAKEUP,
                cal.getTimeInMillis(), 10 * 1000, pintent);
        return super.onStartCommand(intent, flags, startId);
    }

    public IBinder onBind(Intent arg0) {

        return null;
    }
Cœur
  • 37,241
  • 25
  • 195
  • 267
  • possible duplicate of [How to start Service using Alarm Manager in Android?](http://stackoverflow.com/questions/8321443/how-to-start-service-using-alarm-manager-in-android) – KRP Apr 24 '14 at 05:28

1 Answers1

0

try this

You need to Use PendingIntent.FLAG_UPDATE_CURRENT

Change your code from PendingIntent pintent = PendingIntent.getService(this, 0, intent, 0); to

PendingIntent pintent = PendingIntent.getService(this,
                        0,  serviceIntent, PendingIntent.FLAG_UPDATE_CURRENT);
Sree
  • 3,136
  • 2
  • 31
  • 39