Im my app I am writing one service which runs in the background and after every hour it updates the local db in devices as soon as new updates are received and i am sending the status bar notification to the user.
In my services i am making a rest api call but i need to check that if internet connection is down the service for making api calls should not run. I know how to make it in activity but don't know how to do it in the services.
SchedulingService.java
public class SchedulingService extends IntentService {
DbHandler DbHandler;
List<Shipment> _shipmentList = new ArrayList<>();
@Override
protected void onHandleIntent(Intent intent) {
checkNotificationStatus();
// Waking up the Alarm Manager after some time
TrackerAlarmReceiver.completeWakefulIntent(intent);
}
public void checkNotificationStatus() {
List<String> notificationList = new ArrayList<String>();
HashMap<User, String> notificationMap = new HashMap<User, String>();
UserTracker userTracker;
UserService apiService;
DbHandler = new DbHandler(this);
_userList = DbHandler.getAllActiveUsers();
// Here i am making the API call
RestAdapter restAdapter = new RestAdapter.Builder()
.setEndpoint(ApiMessage.API_URL)
.setLogLevel(RestAdapter.LogLevel.FULL)
.setConverter(new GsonConverter(new Gson()))
.build();
apiService = restAdapter.create(UserService.class);
sendNotification(message);
}