Note that there are some explanatory texts on larger screens.

plurals
  1. POWhy BroadcastReceiver not change global variable in Application
    primarykey
    data
    text
    <p>I need global variable in my application. Variable will be set and periodically change in BroadcastReceiver. And I get and use it in the Thread in the Service. My code:</p> <p>I create Application Class for globals variables:</p> <pre><code> package com.bklah.blah; import android.app.Application; public class ApplicationBlah extends Application { public boolean eSettings; public boolean getSettings() { return this.eSettings; } public void setSettings( boolean eSettings) { this.eSettings = eSettings; } } </code></pre> <p>I decrale it in AndroidManifest file:</p> <pre><code> &lt;application android:icon="@drawable/icon" android:label="@string/sAppName" android:theme="@android:style/Theme.NoTitleBar" android:name=".ApplicationBlah"&gt; &lt;receiver android:name=".BroadcastBlah" android:process=":remote" /&gt; </code></pre> <p>I periodically change variable in cicle through BroadcastReceiver:</p> <pre><code> public class BroadcastBlah extends BroadcastReceiver { @Override public void onReceive( Context context, Intent intent) { ((ApplicationBlah)context.getApplicationContext()).setSettings( true); // or ... // ((ApplicationBlah)getApplication()).setSettings(true); } } </code></pre> <p>And I try use variable in cicle in Thread in Service:</p> <pre><code> public class ServiceBlah extends Service { public static Thread threadBlah = null; public String fUse( Context context) { boolean eSeetingsCurrent1 =((ApplicationBlah)context.getApplicationContext()).eSettings; boolean eSeetingsCurrent2 = ApplicationBlah.eSettings; boolean eSeetingsCurrent3 = ((ApplicationBlah)context.getApplicationContext()).getSettings(); // --- all this variables always == false, but i need true from Receiver } public void fThreadBlah( final Context context) { final Handler handler = new Handler() { @Override public void handleMessage( Message message) { ... } }; threadBlah = new Thread() { @Override public void run() { final Message message = handler.obtainMessage( 1, fUse( context)); handler.sendMessage( message); } }; threadBlah.setPriority( Thread.MAX_PRIORITY); threadBlah.start(); } } </code></pre> <p>But I always get false in global variable. Please say what is my error?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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