My live wallpapers get weather data from the Internet at a specific time interval. Everything is implemented through Service and Alarm. How to make it so that with the loss of the Internet and Alarm and Service were suspended and started again with its appearance?

Service

public class GetWeatherService extends Service { private String url, units; private Weather weather; private int updFreq; SharedPreferences sPrefs; InternetReceiver internetReceiver; static Intent intent; public static final String NEW_WEATHER = "ru.sergey.abadzhev.wtlwpd.NEW_WEATHER"; @Override public int onStartCommand(Intent intent, int flags, int startId) { updFreq = Integer.parseInt(sPrefs.getString( WallpaperSettings.PREF_UPD_FREQ, "120")); int alarmType = AlarmManager.ELAPSED_REALTIME_WAKEUP; long timeToRefresh = SystemClock.elapsedRealtime() + updFreq * 60 * 1000; alarms.setRepeating(alarmType, timeToRefresh, updFreq * 60 * 1000, alarmIntent); sPrefs = getSharedPreferences(WallpaperActivity.PREFS, 0); units = sPrefs .getString(WallpaperSettings.PREF_MEASURE_UNITS, "metric"); if (isOnline()) { getWeatherData(); } else { Toast.makeText(getApplicationContext(), getResources().getString(R.string.no_internet_connection), Toast.LENGTH_SHORT).show(); IntentFilter fFilter; fFilter = new IntentFilter("android.net.conn.CONNECTIVITY_CHANGE"); internetReceiver = new InternetReceiver(); registerReceiver(internetReceiver, fFilter); } return Service.START_NOT_STICKY; } AlarmManager alarms; PendingIntent alarmIntent; @Override public void onCreate() { sPrefs = getSharedPreferences(WallpaperActivity.PREFS, 0); alarms = (AlarmManager) getSystemService(Context.ALARM_SERVICE); String ALARM_ACTION; ALARM_ACTION = WeatherAlarmReceiver.ACTION_REFRESH_WEATHER_ALARM; Intent intentToFire = new Intent(ALARM_ACTION); alarmIntent = PendingIntent.getBroadcast(this, 0, intentToFire, 0); } public void getWeatherData() { url = "http://api.openweathermap.org/data/2.5/weather?lat=57.1369473&lon=65.5819746&units=" + units + "&APPID=f9b99d6534aa4c68339db9a6d7064e50&lang=" + getLanguage(); WeatherGetter wg = new WeatherGetter(); wg.execute(url); try { weather = wg.get(); } catch (InterruptedException e) { e.printStackTrace(); } catch (ExecutionException e) { e.printStackTrace(); } announceNewWeather(weather); } private void announceNewWeather(Weather _weather) { if (weather.getDescription() != null) { intent = new Intent(NEW_WEATHER); intent.putExtra("temperature", _weather.getTemp()); intent.putExtra("humidity", _weather.getHumidity()); intent.putExtra("speed", _weather.getSpeed()); intent.putExtra("description", _weather.getDescription()); intent.putExtra("time", _weather.getTime()); intent.putExtra("pressure", _weather.getPressure()); intent.putExtra("tempMin", _weather.getTempMin()); intent.putExtra("tempMax", _weather.getTempMax()); intent.putExtra("deg", _weather.getDeg()); intent.putExtra("id", _weather.getId()); intent.putExtra("sst", _weather.getSunSetTime()); intent.putExtra("srt", _weather.getSunRiseTime()); sendStickyBroadcast(intent); } stopSelf(); } @Override public IBinder onBind(Intent arg0) { return null; } public boolean isOnline() { ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE); NetworkInfo netInfo = cm.getActiveNetworkInfo(); if (netInfo != null && netInfo.isConnectedOrConnecting() && cm.getActiveNetworkInfo().isAvailable() && cm.getActiveNetworkInfo().isConnected()) { return true; } return false; } public class InternetReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { if (isOnline()) { getWeatherData(); try { unregisterReceiver(internetReceiver); } catch (IllegalArgumentException e) { e.printStackTrace(); } } } } public String getLanguage() { String lang; Configuration sysConfig = getResources().getConfiguration(); Locale curLocale = sysConfig.locale; lang = curLocale.getLanguage(); if (lang.equals("es")) { lang = "sp"; } else if (lang.equals("uk")) { lang = "ua"; } else if (lang.equals("sv")) { lang = "se"; } else if (lang.equals("zh")) { if (getResources().getConfiguration().locale.getCountry().equals( "TW")) { lang = "zh_tw"; } else if (getResources().getConfiguration().locale.getCountry() .equals("CN")) { lang = "zh_cn"; } } return lang; } 

}

AlarmReceiver

 public class WeatherAlarmReceiver extends BroadcastReceiver { public static final String ACTION_REFRESH_WEATHER_ALARM = "ru.sergey.abadzhev.wtlwpd.ACTION_REFRESH_WEATHER_ALARM"; @Override public void onReceive(Context context, Intent intent) { Intent startIntent = new Intent(context, GetWeatherService.class); context.startService(startIntent); } 

}

    1 answer 1

     <receiver android:name=".ReceiverName" > <intent-filter > <action android:name="android.net.wifi.STATE_CHANGE" /> <action android:name="android.net.conn.CONNECTIVITY_CHANGE" /> </intent-filter> </receiver> public class ReceiverName extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { ConnectivityManager cm = ((ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE)); if (cm == null) return; if (cm.getActiveNetworkInfo() != null && cm.getActiveNetworkInfo().isConnected()) { // Send here } else { // Do nothing or notify user somehow } } } 

    I just recently started learning android, so I'm not sure, but it seems to me that I need to somehow make friends with the service.

    • The thing is that I have 3 similar services in the program, for each of them to put the receiver as inhumane as it seems to me. How it is now implemented works for me, but after a long hibernation of the device (for example at night), the wallpaper for some reason thinks about 5 seconds and then restarts. Added an example of one service and alarm receiver in the header. - q3er