public class NotificationService extends Service{
public static boolean registered = false;
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.i("NotificationService", "Service Started!!!@@@@@@@@@@@@@@@@@@@@@@@");
// TODO Add your jobs here
return Service.START_STICKY; // .START_NOT_STICKY;
}
@Override
public IBinder onBind(Intent intent) {
Log.i("NotificationService", "Service binded!!!@@@@@@@@@@@@@@@@@@@@@@@");
// TODO Auto-generated method stub
return null;
}
public static void startMe(final Context ctx, final long secs, boolean force){
if(registered && !force) return;
Log.i("NotificationService", "startMe!!!@@@@@@@@@@@@@@@@@@@@@@@");
(new Thread(){
public void run(){
try {
sleep(secs * 1000); // you can remove this code, if you don't want to have initial delay...
} catch (InterruptedException e) {
}
Calendar cal = Calendar.getInstance();
Intent intent = new Intent(ctx, NotificationService.class);
PendingIntent pintent = PendingIntent.getService(ctx, 0, intent, 0);
AlarmManager alarm = (AlarmManager)ctx.getSystemService(Context.ALARM_SERVICE);
// Start every secs seconds
alarm.setRepeating(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), secs*1000, pintent);
// alarm.cancel(operation);
registered = true;
}
}).start();}
}
2. Update manifest xml.
<application
... >
....
<service android:name="com.examples.NotificationService"
android:enabled="true" />3. Start service
In your activity or some where...
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test);
NotificationService.startMe(getBaseContext(), 30); // starts every 30 secs.
}


