2013-04-24 107 views
2

我需要在我的应用程序中使用全局变量。变量将在BroadcastReceiver中设置并周期性更改。我可以在服务中的线程中使用它。我的代码:为什么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; 
     } 
    } 

我decrale它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" /> 

我定期通过广播接收器改变变量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(); 
     } 
    } 

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

+0

你的onReceive甚至被称为? – FWeigl 2013-04-24 08:57:45

+0

@Ascorbin,没错,的onReceive呼吁其事件(我删除登录功能的onReceive用于清除的代码) – 2013-04-24 09:04:08

+1

不能确定,但​​是是有可能,你必须做出eSettings作为一个静态变量?或者制作一个ApplicationBlah的单身人士? – dumazy 2013-04-24 09:06:44

回答

4

我找到解决办法:我从清单接收器的属性删除android:process=":remote"。它工作正常!

0

你使用的一个contect,以获得上下文,然后将其转换为应用程序,它看起来很奇怪。试试他,我不知道它是否会工作。

((ApplicationBlah)getApplication()).setSettings(true); 
+0

我尝试了,但它不适合我... – 2013-04-24 09:13:15

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

+0

谢谢,这不是我的方式:) BroadcastReciever会经常更改此变量,并且我知道SharedPreference用于保存文件,它会冻结系统。 – 2013-04-24 09:22:38

+0

在那种情况下,然后你有尝试把'静态'关键字到你的eSettings甚至使ApplicationBlah静态? – Duc 2013-04-24 10:13:25

相关问题