2

我的应用程序中需要全局变量。变量将在 BroadcastReceiver 中设置并定期更改。我在服务中的线程中获取并使用它。我的代码:

我为全局变量创建应用程序类:

    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;
        }
     }

我在 AndroidManifest 文件中声明它:

    <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" />

我通过 BroadcastReceiver 定期更改 cicle 中的变量:

     public class BroadcastBlah extends BroadcastReceiver
      {
        @Override
        public void onReceive( Context context, Intent intent) 
         {
           ((ApplicationBlah)context.getApplicationContext()).setSettings( true);
           // or ...
           // ((ApplicationBlah)getApplication()).setSettings(true); 
         }
      }

我尝试在服务中的线程中使用 cicle 中的变量:

    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();
        }
     }

但我总是在全局变量中得到错误。请说我的错误是什么?

4

3 回答 3

4

我找到了解决方案:我android:process=":remote"从 Manifest 中的接收者属性中删除。它工作正常!

于 2013-04-24T10:47:07.933 回答
0

您正在使用 contect 获取上下文然后将其转换为 Application,它看起来很奇怪。试试他的,不知道行不行

((ApplicationBlah)getApplication()).setSettings(true); 
于 2013-04-24T09:07:17.937 回答
0

如果只是你追求的原始数据类型,我认为你可以使用 SharedPreferences,它真的很容易使用。关于你的活动:

public static final String PREFS_NAME = "MyPrefs";

@Override
protected void onCreate(Bundle state){
   super.onCreate(state);
   . . .
   SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
   SharedPreferences.Editor editor = settings.edit();
   editor.putBoolean("eSeetingsCurrent1", eSeetingsCurrent1);

然后,您可以通过以下方式在应用程序的任何位置获取此信息:

 SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
 boolean eSeetingsCurrent1 = settings.getBoolean("eSeetingsCurrent1", false);

更多信息@ http://developer.android.com/guide/topics/data/data-storage.html#pref

于 2013-04-24T09:17:27.090 回答