Note that there are some explanatory texts on larger screens.

plurals
  1. POgetDefaultSharedPreferences not Saving Between Services
    text
    copied!<p>I have two services (a DashClock extension service that calls another service), and the second service is supposed to save a value so I can use that value to display in my DashClock extension. However, this value does not appear to be passed into the extension service.</p> <p>DashClock Extension:</p> <pre><code>@Override public void onCreate() { super.onCreate(); be=this; //numUpdates = getSharedPreferences("PREFERENCE", MODE_PRIVATE).getInt("numUpdates", -1); Intent i = new Intent(this, UpdateCheckService.class); i.putExtra(Constants.CHECK_FOR_UPDATE, true); startService(i); //numUpdates = getSharedPreferences("PREFERENCE", MODE_PRIVATE).getInt("numUpdates", 0); registerReceiver(myReceiver, new IntentFilter(Intent.ACTION_TIME_TICK)); } @Override protected void onUpdateData(int reason) { // Get preference value. final SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(getApplicationContext()); //String name = sp.getString(PREF_NAME, "Eric"); Log.d("hiya","updating"); /*while(getSharedPreferences("PREFERENCE", MODE_PRIVATE).getInt("numUpdates", -1)==-1){ Log.d("wait","waiting here"); }*/ //numUpdates = getSharedPreferences("PREFERENCE", MODE_PRIVATE).getInt("numUpdates", -2); int numUpdates = sp.getInt("numUpdates", -2); Log.d("IMPORTANT FROM BrianExtension", "" + numUpdates); Intent intent = new Intent(); intent.setClassName("com.cyanogenmod.updater", "com.cyanogenmod.updater.UpdatesSettings"); // Publish the extension data update. if(numUpdates == 0){ shouldShow = false; } else{ shouldShow = true; } publishUpdate(new ExtensionData() .visible(shouldShow) .icon(R.drawable.cid) .status(numUpdates + " updates") .expandedTitle(numUpdates + " Nightly CM Updates") .expandedBody("Click here to go to CMUpdater.") .clickIntent(intent)); } </code></pre> <p>UpdateCheckService:</p> <pre><code>@Override public void onCreate() { ucs = this; registerReceiver(mReceiver, new IntentFilter("updatethis")); /*prefs = getSharedPreferences("PREFERENCE", Context.MODE_PRIVATE); prefs.registerOnSharedPreferenceChangeListener(mListener);*/ final SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(getApplicationContext()); Thread thread = new Thread(new Runnable(){ @Override public void run() { try { try { int ericUpdates = getAvailableUpdates().getUpdateCount(getBaseContext()); Log.d("yes!", ""+ericUpdates); sp.edit().putInt("numUpdates", ericUpdates).apply(); Log.d("IMPORTANT FROM UpdateCheckService", "" + sp.getInt("numUpdates", -99)); //getSharedPreferences("PREFERENCE", MODE_PRIVATE).edit().putInt("numUpdates", ericUpdates).apply(); //Log.d("IMPORTANT FROM UpdateCheckService", "" + getSharedPreferences("PREFERENCE", MODE_PRIVATE).getInt("numUpdates", -99)); sendBroadcast(new Intent(ucs, BrianUpdaterReceiver.class)); //BrianExtension.be.onUpdateData(4); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } catch (Exception e) { e.printStackTrace(); } } }); thread.start(); //stopSelf(); // Get the System Mod string mSystemMod = TESTING_DOWNLOAD ? "cmtestdevice" : SysUtils.getSystemProperty(Customization.BOARD); if (mSystemMod == null) { Log.i(TAG, "Unable to determine System's Mod version. Updater will show all available updates"); } } </code></pre> <p>The logs show that the value in the "numUpdates" preference is correct in the UpdateCheckService but remains -2 in the BrianExtension.</p> <p>manifest entries:</p> <pre><code>&lt;service android:name=".BrianExtension" android:icon="@drawable/cid" android:label="CyanogenMod Updates" android:permission="com.google.android.apps.dashclock.permission.READ_EXTENSION_DATA"&gt; &lt;intent-filter&gt; &lt;action android:name="com.google.android.apps.dashclock.Extension" /&gt; &lt;/intent-filter&gt; &lt;meta-data android:name="protocolVersion" android:value="1" /&gt; &lt;meta-data android:name="description" android:value="Displays Number of New Nightly Updates Available" /&gt; &lt;/service&gt; &lt;service android:name="com.brianco.cyanogenmodupdatecheckfordashclock.UpdateCheckService" android:process="com.brianco.cyanogenmodupdatecheckfordashclock.UpdateCheckService"&gt; &lt;intent-filter&gt; &lt;action android:name="com.brianco.cyanogenmodupdatecheckfordashclock.IUpdateCheckService"/&gt; &lt;/intent-filter&gt; &lt;/service&gt; </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload