It does not reach the doInBackground method when the Internet is turned off on the device, what could be the error? The stream checks if the value has changed with SharedPreferances, if so, it changes the style of the element on the screen.

import android.content.Context; import android.content.SharedPreferences; import android.content.pm.ActivityInfo; import android.net.ConnectivityManager; import android.os.AsyncTask; import android.os.Bundle; import android.os.Handler; import android.preference.PreferenceActivity; import android.preference.PreferenceManager; import android.view.MotionEvent; import android.view.View; import android.view.ViewGroup; import android.widget.LinearLayout; import android.widget.RelativeLayout; import com.google.android.gms.ads.AdRequest; import com.google.android.gms.ads.InterstitialAd; public class Settings extends PreferenceActivity{ SharedPreferences sp; int btnOper1; int btnOper2; public Handler h; CheckPref checkPref; InterstitialAd interstitial; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); addPreferencesFromResource(R.xml.pref); setContentView(R.layout.settings); sp = PreferenceManager.getDefaultSharedPreferences(this); interstitial = new InterstitialAd(this); interstitial.setAdUnitId("ca-app-pub-1768060289488574/5823162247"); // Создаём запрос к AdMob AdRequest adRequesti = new AdRequest.Builder().build(); // Начинаем загружать объявление interstitial.loadAd(adRequesti); final RelativeLayout view = (RelativeLayout) findViewById(R.id.rel); view.setOnTouchListener(new View.OnTouchListener() { @Override public boolean onTouch(View v, MotionEvent event) { switch (event.getAction()) { case MotionEvent.ACTION_DOWN: view.setBackgroundColor(getResources().getColor(R.color.settings_pressed)); return true; // if you want to handle the touch event case MotionEvent.ACTION_UP: view.setBackgroundColor(getResources().getColor(R.color.settings)); // interstitial.show(); finish(); return true; } return false; } }); h = new Handler() { public void handleMessage(android.os.Message msg) { style(); } }; style(); } @Override public void onPause() { if (isOnline()) interstitial.show(); checkPref.cancel(true); super.onPause(); } public boolean isOnline() { ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE); return cm.getActiveNetworkInfo() != null && cm.getActiveNetworkInfo().isConnectedOrConnecting(); } @Override protected void onResume() { LinearLayout linerLayout = (LinearLayout) findViewById(R.id.linearLayout); linerLayout.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, 0)); checkPref = new CheckPref(); checkPref.execute(); super.onResume(); } public void style(){ LinearLayout liner = (LinearLayout) findViewById(R.id.liner); btnOper2 = Integer.parseInt(sp.getString("operations", "0")); if (btnOper2 == 0) { liner.setBackgroundResource(R.drawable.ravno_blue); } else if (btnOper2 == 1) { liner.setBackgroundResource(R.drawable.ravno_green); } else if (btnOper2 == 2) { liner.setBackgroundResource(R.drawable.ravno_red); } else if (btnOper2 == 3) { liner.setBackgroundResource(R.drawable.ravno_yellow); } else if (btnOper2 == 4) { liner.setBackgroundResource(R.drawable.ravno_orange); } else if (btnOper2 == 5) { liner.setBackgroundResource(R.drawable.ravno_pink); } } class CheckPref extends AsyncTask<Void, Void, Void> { @Override protected void onPreExecute() { super.onPreExecute(); } @Override protected Void doInBackground(Void... params) { try{ while (true){ btnOper1 = Integer.parseInt(sp.getString("operations", "0")); if (btnOper1!=btnOper2) h.sendEmptyMessage(1); System.out.println("###################"); Thread.sleep(100); } }catch (Exception e){ System.out.println(e); } return null; } @Override protected void onPostExecute(Void result) { super.onPostExecute(result); } } } 

    1 answer 1

    I can not determine what the problem is in your code, but you are trying to invent a bicycle in it that has long been perfected, and it is called OnSharedPreferenceChangeListener

    • how to contact him? is it a listener? - Fibs
    • Thanks for the help, really the bike was still that) - Fibs