代码之家  ›  专栏  ›  技术社区  ›  Elias

多次在非活动类中获取上下文

  •  0
  • Elias  · 技术社区  · 7 年前

    我正在开发一个应用程序,其中“更新程序”不断从服务器请求数据。如果接收到新数据,数据将通过本地广播发送给活动。为此,我需要传入构造函数的当前上下文。此外,新数据被写入一个单例类(通过应用程序的运行时存储)。

    问题是,我需要不断为LocalBroadcast创建新的上下文,但它在构造函数中只传递了一次。是否有人知道每次本地广播发送内容时如何获取当前上下文?

    我找到了这个答案,但我总是被警告不要将上下文类放在静态字段中。( Get application context from non activity singleton class )

    感谢您的阅读和每一条建议。 这是我的“更新程序”代码:

    import android.app.Activity;
    import android.content.BroadcastReceiver;
    import android.content.Context;
    import android.content.Intent;
    import android.support.v4.content.LocalBroadcastManager;
    
    import java.util.Arrays;
    
    
    public class ClientUpdater extends Thread {
        ClientCommunication cComm;              //this is my class which manages the Server-Client-Communication
        Intent BroadcastIntentUpdate;           //Intent for the LocalBroadcast
        Context cntxt;                          //stores the context passed in the constructor
        GlobalClass Obj1;                       //Instance of my Singleton
    
        public ClientUpdater(ClientCommunication cComm, Context context) {
            this.cComm = cComm;
            this.cntxt = context;
            BroadcastIntentUpdate = new Intent("update");
        }
    
        public void run(){
            while(true){
    
                String vomS1 = cComm.sendData("updateChat" + "~" + "$empty$");      //sends an update request to the server an receives the current chat-state
                if(!vomS1.equalsIgnoreCase("timeout")){
    
                    Obj1 = GlobalClass.getInstance();
                    String[] update = GlobalClass.decode(vomS1, ";");               //decodes the receives message                  
                    String[] altchat = Obj1.getChat();                              //loads the stored chat protocoll from singleton
    
    
                    if(!Arrays.equals(update, altchat)){                            //if the received message has new data...
    
                        BroadcastIntentUpdate.putExtra("message", update);
                        //for ".getInstance(cntxt)" the current Context is always needed right?
                        LocalBroadcastManager.getInstance(cntxt).sendBroadcast(BroadcastIntentUpdate);          //...it's sent to the activity
                        Obj1.setChat(update);                                                                                           //...and stored in the singleton
                    }
                }    
    
    
                try {
                    sleep(500);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
    
        }
    
    }
    
    1 回复  |  直到 6 年前
        1
  •  1
  •   HendraWD    7 年前

    改变

    this.cntxt = context;
    

    cntxt = context.getApplicationContext();
    

    相反哪里 cntxt 是静态上下文。它不会创建活动泄漏,因为它使用应用程序上下文。

    如果您了解Android中的后台服务会更好 https://developer.android.com/training/run-background-service/create-service.html