Last time, I use following coding to run background service.
Intent intent = new Intent(InitActivity.this, GetService.class);
PendingIntent pintent = PendingIntent.getService(InitActivity.this, 0, intent, 0);
Calendar calendar = Calendar.getInstance();
AlarmManager alarm = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
alarm.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), 50 * 1000, pintent);
return getZipInfo.toString();
to make better performance, I want to run my background service once device is connect WIFI/data connection. To make clear, if device is not connect wifi/data connection, my background service will not run for sure. Once connect wifi/data connection, it will run immediately.
To check the WiFi connection you can use this
To execute the code whenever the Wifi is connected, you have to register Brodcast Recevier
Register the receiver
In your Receiver class do this
And as mentioned below, dont forget to add the permission in your AndroidManifest.xml