37

In my application, I am trying to start a service using Alarm manager. When I am clicking a button service should start in a particular time which I am giving. My code for Alarm Manager is given below:

public void onClick(View view) 
{
    if(view == m_btnActivate)
    {
        Calendar cur_cal = Calendar.getInstance();
        cur_cal.setTimeInMillis(System.currentTimeMillis());
        cur_cal.add(Calendar.SECOND, 50);
        Log.d("Testing", "Calender Set time:"+cur_cal.getTime());
        Intent intent = new Intent(DashboardScreen.this, ServiceClass.class);
        Log.d("Testing", "Intent created");
        PendingIntent pi = PendingIntent.getService(DashboardScreen.this, 0, intent, 0);
        AlarmManager alarm_manager = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
        alarm_manager.set(AlarmManager.RTC, cur_cal.getTimeInMillis(), pi);
        Log.d("Testing", "alarm manager set");
        Toast.makeText(this, "ServiceClass.onCreate()", Toast.LENGTH_LONG).show();
    }
}

And bellow is my Service Class:

    public class ServiceClass extends Service{

    @Override
    public void onCreate() {
        // TODO Auto-generated method stub
        super.onCreate();
        Log.d("Testing", "Service got created");
        Toast.makeText(this, "ServiceClass.onCreate()", Toast.LENGTH_LONG).show();
    }

    @Override
    public IBinder onBind(Intent arg0) {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public void onDestroy() {
        // TODO Auto-generated method stub
        super.onDestroy();
    }

    @Override
    public void onStart(Intent intent, int startId) {
        // TODO Auto-generated method stub
        super.onStart(intent, startId);
        Toast.makeText(this, "ServiceClass.onStart()", Toast.LENGTH_LONG).show();
        Log.d("Testing", "Service got started");
    }

}

If the service will start it should print the log messages in the service class. But it is not showing. Can any one help?

Aditi Parikh
  • 1,522
  • 3
  • 13
  • 34
Arindam Mukherjee
  • 2,255
  • 11
  • 48
  • 83

2 Answers2

93

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" />
user370305
  • 108,599
  • 23
  • 164
  • 151
  • 2
    Yes it is working..thanks..but now how can i stop the service in a particular time?? – Arindam Mukherjee Nov 30 '11 at 05:38
  • 1
    can you help me for the above question?? How can i stop the alarm manager in a particular time.. – Arindam Mukherjee Nov 30 '11 at 10:09
  • Use alarmManager.cancel(pintent); to stop the alarm manager. – user370305 Nov 30 '11 at 10:51
  • I tried with the filterEquals() method..can you please checke? – Arindam Mukherjee Nov 30 '11 at 11:16
  • http://stackoverflow.com/questions/4212824/identify-and-cancel-an-alarm-send-to-an-alarmmanager – Arindam Mukherjee Nov 30 '11 at 11:17
  • So again i have to create another intent or not? – Arindam Mukherjee Nov 30 '11 at 11:28
  • Intent intent2 = new Intent(DashboardScreen.this, com.innominds.util.ServiceClass.class); PendingIntent pi2 = PendingIntent.getService(DashboardScreen.this, req, intent2, 0); AlarmManager alarm_stop = (AlarmManager)getSystemService(Context.ALARM_SERVICE); alarm_manager.set(AlarmManager.RTC_WAKEUP, cal_stop.getTimeInMillis(), pi2); if(intent2.filterEquals(intent)) { alarm_stop.cancel(pi); } – Arindam Mukherjee Nov 30 '11 at 11:28
  • No, try to use same intent for cancelling the alarm your pintent. – user370305 Nov 30 '11 at 11:30
  • No if i use the same intent then i can use that filterEquals() method? – Arindam Mukherjee Nov 30 '11 at 11:34
  • Look at [Here](http://developer.android.com/resources/samples/ApiDemos/src/com/example/android/apis/app/AlarmController.html), [Here](http://developer.android.com/resources/samples/ApiDemos/src/com/example/android/apis/app/AlarmService.html) and [Here](http://stackoverflow.com/questions/3330522/how-to-cancel-this-repeating-alarm) also [Cancel Alarm using alarmManager.cancel(pendingIntent) ](http://android-er.blogspot.com/2011/05/cancel-alarm-using-alarmmanagercancelpe.html) then you can understand what I want to say.. – user370305 Nov 30 '11 at 11:47
  • i wrote the below code: Intent intent = new Intent(DashboardScreen.this, com.innominds.util.ServiceClass.class); Log.d("Testing", "Intent created"); PendingIntent pi = PendingIntent.getService(DashboardScreen.this, req, intent, 0); AlarmManager alarm_manager = (AlarmManager)getSystemService(Context.ALARM_SERVICE); alarm_manager.setRepeating(AlarmManager.RTC_WAKEUP, cur_cal.getTimeInMillis(),30*1000, pi); Log.d("Testing", "Intent created"); AlarmManager alarm_stop = (AlarmManager)getSystemService(Context.ALARM_SERVICE); alarm_stop.cancel(pi); – Arindam Mukherjee Nov 30 '11 at 12:17
  • do i still need a BroadcastReceiver when using this? or i only need two files? the activity and the service? – Kairi San Feb 16 '16 at 07:43
  • Does this code work for Android Oreo version and above? `If your app targets API level 26 or higher, the system imposes restrictions on using or creating background services unless the app itself is in the foreground` – Fivos Apr 01 '19 at 13:20
  • Isn't the minimum time between alarms 60 seconds? – Ondrej Sotolar May 13 '19 at 10:55
16

I know this is an old question, but just to help the people from google, here is how you can keep your service alive.

    Intent ishintent = new Intent(this, HeartBeat.class);
    PendingIntent pintent = PendingIntent.getService(this, 0, ishintent, 0);
    AlarmManager alarm = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
    alarm.cancel(pintent);
    alarm.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(),5000, pintent);

The service is killed and restarted every 5 seconds.

Yemanzo
  • 186
  • 1
  • 3