Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>The reason why the <code>CalledFromWrongThreadException</code> is raised, is because the <code>OnChangeListener</code> is not supposed to be called from the <code>IntentService</code>.</p> <p>What you could do though, is sending a Broadcast (where you can actually include the value as well).</p> <p>In case you are only using the <code>SharedPreference</code> for communication, you can replace it entirely (and I would recommend this, as SharedPreferences are wasting Write Cycles).</p> <p>You could use a code like this for sending a Broadcast:</p> <pre><code>/** * Send an Intent with the Broadcast, a permission and a Bundle * * @param context * A context to use * @param broadcast * String to use, eg. "de.bulling.smstalk.ENABLE" * @param permission * Permission needed for receiving * @param bundle * Extras to attach */ public static void send_broadcast(Context context, String broadcast, String permission, Bundle bundle) { //SettingsClass.log_me(tag, "Sending broadcast " + broadcast); Intent i = new Intent(); i.setAction(broadcast); if (bundle != null) { i.putExtras(bundle); } if (permission != null) { context.sendBroadcast(i, permission); } else { context.sendBroadcast(i); } } </code></pre> <p>You should include a custom permission as well, so other apps don't get the Broadcast, but it is not necessarily needed.</p> <p>To receive the Broadcast, register a Receiver in your Activity, eg</p> <pre><code>private final BroadcastReceiver receiver = new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { Bundle b = intent.getExtras(); doSomething(); } }; @Override public void onCreate(Bundle bundle) { super.onCreate(bundle); IntentFilter filter = new IntentFilter(); filter.addAction(YOURBROADCAST); registerReceiver(receiver, filter); [...] } @Override public void onDestroy() { unregisterReceiver(receiver); super.onDestroy(); } </code></pre> <p>Also you should consider using <code>Handler</code>s, so your methods in the OnChangeListener call are run by the MainUI thread.</p> <p>Example:</p> <pre><code>Handler mHandler = new Handler(); Runnable myCode = new Runnable(){ @Override protected void onRun() { yourCode(); } }; mHandler.run(myCode); </code></pre> <p>This has also the advantage of running the code delayed with <code>runDelayed()</code>, so you don't have to use <code>sleep</code>, and you UI will be still responding.</p>
 

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