android - 从应用程序 class 更改 Activity UI

android - changing Activity UI from application class

我扩展了应用程序 class 以便在 android 中创建类似单例的对象。

在这个对象中,我的所有 HTTP 都与我的服务器一起工作,所有其他活动都可以访问它并调用 GET、POST 等方法

代码:

public class HttpManagerInstance extends Application {
    private HttpClient httpClient;
    private HttpGet get;

    @Override
    public void onCreate() {
        httpClient = new DefaultHttpClient();
        get = new HttpGet("http://10.100.102.9:8000/users/");
        super.onCreate();

    }


    public Void getUsers() throws Exception {
        new executeRequest().execute(get);
        return null;
    }

    private class executeRequest extends AsyncTask<HttpRequest, Void, Integer> {

        @Override
        protected Integer doInBackground(HttpRequest... params) {
            // TODO Auto-generated method stub
            HttpRequest request = params[0];
            HttpResponse response;
            String result="";
            try {
                response = httpClient.execute((HttpUriRequest) request);
            } catch (ClientProtocolException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            return responseCode;
        }

        @Override
        protected void onPostExecute(Integer result) {
            // TODO Auto-generated method stub
            switch (result) {
            case HttpStatus.SC_OK:
                // request was fine

                // Here I want to updated the GUI of the activity that called this method.
                break;
            }
        }

    }

}

这就是我从 Activity 调用方法的方式:

HttpManagerInstance sampleApp = (HttpManagerInstance)getApplicationContext();
sampleApp.getUsers();

再次 - 我想访问 Activity 的 UI,它调用了放置 REQUEST ACCEPTED 消息的方法。

也许传递上下文?有什么想法吗?

我会创建一个侦听器:

public class HttpManagerInstance extends Application {
    private HttpClient httpClient;
    private HttpGet get;

    public interface ResponseListener{
      public void onSuccess(Object data);
    }


    @Override
    public void onCreate() {
        httpClient = new DefaultHttpClient();
        get = new HttpGet("http://10.100.102.9:8000/users/");
        super.onCreate();

    }


    public Void getUsers(ResponseListener listener) throws Exception {
        new executeRequest(listener).execute(get);
        return null;
    }

    private class executeRequest extends AsyncTask<HttpRequest, Void, Integer> {

        private ResponseListener mListener;

        public executeRequest(ResponseListener listener){
         this.mListener = listener;
        }

        @Override
        protected Integer doInBackground(HttpRequest... params) {
            // TODO Auto-generated method stub
            HttpRequest request = params[0];
            HttpResponse response;
            String result="";
            try {
                response = httpClient.execute((HttpUriRequest) request);
            } catch (ClientProtocolException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            return responseCode;
        }

        @Override
        protected void onPostExecute(Integer result) {
            // TODO Auto-generated method stub
            switch (result) {
            case HttpStatus.SC_OK:
                // request was fine

                // Here I want to updated the GUI of the activity that called this method.
                if(this.mListener != null) mListener.onSuccess(whatEverDataYouWant);
                break;
            }
        }

    }

}

然后,在你的 activity:

    HttpManagerInstance sampleApp = (HttpManagerInstance)getApplicationContext();
    sampleApp.getUsers(new ResponseListener(){
       public void onSuccess(Object data){
         //update your ui!
       }

});

简短的回答是您不能直接从另一个 activity 引用 UI。我的建议是在 Application class 上设置回调并在 executeRequest#onPostExecute 上调用,然后在 Activity 上实施回调并更新 UI 从那里。

如果您需要帮助来实施回调检查this question

如果您需要显示消息,可以选择 Dialog Class 或 Toast Class,您可以在此处查看更多信息: 对话:http://developer.android.com/guide/topics/ui/dialogs.html 干杯:http://developer.android.com/guide/topics/ui/notifiers/toasts.html

但是,如果您想在实际 activity 中访问或修改控件,则使用 Runnable class,如果您在 AsyncTask 中工作,则使用 context.runOnUiThread() 方法。真正的问题是您不能使用控件声明在 AsyncTask 中更改 UI。您需要抛出一个 Runnable 进程才能与 activity!! 进行通信。例如:

context.runOnUiThread(new Runnable() {
                public void run() {
                    //Declaration of variables
                    TextView MyTextView = (TextView) context.findViewById(R.id.txtvMyControl);

                    MyTextView.setText("My title");
                }
}

如果我能帮到你说我,祝你好运!