Tell me, I'm stupid or what - there is Activity and Service. They both write / read SharedPreferences. But one reads what it has saved, and the other reads "emptiness." Activity reads from the main thread, the service writes from the created thread. But the storage is the same. How can this be?

-------- addition - code ---------------

  1. There is a Config class with static variables and methods. In particular, there are methods Save and Update. One variable was left for an example, attacks of their own such (the clumsiness of the save / restore code itself is, of course, but such is the legacy)

    public class Config { static public StorageInterface st = null; static public int[] redraw = new int[100]; //--- необходимость обновить public static void save() { String redraw = ""; for (int i=10; i<=50; i+=10) { redraw += ","+Config.redraw[i]; } Config.st.setItem("redraw", redraw); Config.st.setItem("act", ""+Config.act); Config.st.setItem("locked", ""+Config.locked); Config.st.setItem("screen_off", ""+Config.screen_off); } public static void update() { String[] redraw = Config.st.getItem("redraw", ",0,0,0,0,0").split(","); for (int i=10; i<=50; i+=10) { Config.redraw[i] = Integer.parseInt(redraw[i/10], 10); } } } 

Saving and reading (just a wrapper in essence)

  public class StorageInterface { SharedPreferences sPref; Context activ; public StorageInterface(Context a) { // Config.log("NEW CONTEXT = "+a); activ = a; } public void link(Context a) { activ = a; } //Play an audio file from the webpage @JavascriptInterface public void setItem(String item, String val) { // sPref = getPreferences(MODE_PRIVATE); sPref = activ.getSharedPreferences("st", Context.MODE_PRIVATE); Editor ed = sPref.edit(); ed.putString("st_"+item, val); if (item.equals("redraw")) Config.log("ST save: "+"st_"+item+" = "+val); ed.commit(); } @JavascriptInterface public String getItem(String item) { return getItem(item, ""); } @JavascriptInterface public String getItem(String item, String val) { sPref = activ.getSharedPreferences("st", Context.MODE_PRIVATE); String tmp = sPref.getString("st_"+item, val); if (item.equals("redraw")) Config.log("ST read: "+"st_"+item+" = "+tmp); return( tmp ); } } 

In the activation itself:

  public class webActivity extends Activity implements MediaPlayer.OnCompletionListener, View.OnClickListener, LockscreenUtils.OnLockStatusChangedListener, OnTouchListener/*, SensorEventListener*/ { ... @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); if (Config.st == null) Config.st = new StorageInterface(this); else Config.st.link(this); } //--- НУ и гдето в коде: Config.Update(); //--- и далее доступ к самой переменной if (Config.redraw[i]==1) { } } 

In the service, something like this:

  public class LockscreenService extends Service implements SensorEventListener, OnClickListener { private Thread T = null; @Override public void onCreate() { super.onCreate(); Config.log("LSS Created"); if (Config.st == null) Config.st = new StorageInterface(this); else Config.st.link(this); } @Override public int onStartCommand(Intent intent, int flags, int startId) { if (intent.hasExtra("act")) { int act = intent.getIntExtra("act", 0); if (act == 1) { T = new Thread( new updateMediaTask() ); if (T!=null) T.start(); } } } 

Well, this created stream pdateMediaTask in its run does too

  Config.Update(); ... Config.wedraw[i] = 1; ... Config.Save(); 

Brrr, until he wrote the brain itself broke ... In general, the essence is this - the service for the event starts the stream, he climbs onto the server, checks the data, if necessary, erects flags and throws it into preferences. Then, when the activation is active again, it will look into these preferences and, if necessary, update the elements ..
And here, the cheto does not update ... We don’t ride skis, I ....

  • 2
    need more code ... - Nikotin N
  • now we will depict .. there it’s just that everything is scattered in different places, it will get too much code .... now I’ll do the squeeze. - Alexander
  • one
  • Is this about android: process = ": Any"? I have no such thing at all where ... Apparently I missed something else. - Alexander
  • In general, remade instead of SharedPreferences for storage in files - it works. - Alexander

0