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:
I create Application Class for globals variables:
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;
}
}
I decrale it in AndroidManifest file:
<application android:icon="@drawable/icon"
android:label="@string/sAppName"
android:theme="@android:style/Theme.NoTitleBar"
android:name=".ApplicationBlah">
<receiver android:name=".BroadcastBlah"
android:process=":remote" />
I periodically change variable in cicle through BroadcastReceiver:
public class BroadcastBlah extends BroadcastReceiver
{
@Override
public void onReceive( Context context, Intent intent)
{
((ApplicationBlah)context.getApplicationContext()).setSettings( true);
// or ...
// ((ApplicationBlah)getApplication()).setSettings(true);
}
}
And I try use variable in cicle in Thread in Service:
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();
}
}
But I always get false in global variable. Please say what is my error?