Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/180.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/mercurial/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 多次获取非活动类中的上下文_Java_Android_Android Context - Fatal编程技术网

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

Java 多次获取非活动类中的上下文,java,android,android-context,Java,Android,Android Context,我正在开发一个应用程序,其中“更新程序”不断地从服务器请求数据。如果接收到新数据,数据将通过LocalBroadcast发送到活动。为此,我需要在构造函数中传递的当前上下文。此外,新数据被写入单例类(通过应用程序的运行时存储) 问题是,我需要不断地为我的LocalBroadcast创建新的上下文,但它在构造函数中只传递了一次。是否有人知道如何在每次本地广播发送内容时获取当前上下文 我找到了这个答案,但我总是被警告不要将上下文类放在静态字段中。() 谢谢你的阅读和每一个建议。 以下是我的“更新程序

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

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


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

谢谢你的阅读和每一个建议。 以下是我的“更新程序”代码:

改变

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

如果您了解Android中的后台服务会更好

“我总是被警告不要将上下文类放在静态字段中”;代码中没有静态字段;我一般不与android打交道,所以我不能确定(这就是为什么我要留下评论而不是答案),但该代码看起来最终应该是安全的。android sdk中有一个后台任务服务,据我记忆所及,我建议使用它而不是线程。是的,在你和Hendraws发表评论之后,我也考虑使用后台服务,谢谢你的时间谢谢你的回答。如果我写
cntext=context.getApplicationContext()cntxt变量在使用时总是“刷新”的,还是我误解了什么?这就是我需要的。是的,你是对的,后台服务也是一个东西,也许我会改变,使用应用程序上下文,正如它所建议的,它使用应用程序上下文。您最初在代码中接收的上下文是针对提供上下文的单个接收器/UIComponent/任何东西的。应用程序上下文不会更新/刷新,但始终有效。有很多东西也可以用来代替后台服务,如JobScheduler、FirebaseJobScheduler、AlarmManager等。不过我更喜欢FirebaseJobScheduler。它不会总是“刷新”,除非您创建ClientUpdater对象,因为它是从那里重新分配的。虽然重新分配了实例,但实例是相同的。1个android应用程序有1个应用程序上下文实例,因此除非您关闭并重新打开应用程序,否则不会重新创建该应用程序。
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();
            }
        }

    }

}
this.cntxt = context;
cntxt = context.getApplicationContext();