2

I have a network class making use of the following lib: Lib Docs with 4 function to make a get, post delete and put request to the server. here is the simplified version:

 public static void get(Context context, String url, RequestParams params, AsyncHttpResponseHandler responseHandler) {          
    httpClient.get(context, getAbsoluteUrl(url), params, responseHandler);
} 

Now instead of having to pass the context through via a paramter, Is there no possible way to some how get the context of the calling activity inside the fucntion?

some thing along the lines of:

public static void get(String url, RequestParams params, AsyncHttpResponseHandler responseHandler) {
    Context context = getCallignActivtyCntext() // <====== some thing like this
    httpClient.get(context, getAbsoluteUrl(url), params, responseHandler);
}

the reason I need the specific context is because I have to cancel all network requests when an activity is destroyed, and it groups or identifies all requests by the context.

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Zapnologica
  • 22,170
  • 44
  • 158
  • 253

3 Answers3

2

If you don't actually need the specific context of the caller for your methods, you can just use the Application context through getApplicationContext()

To statically access your Application context you can do something like this:

public class CustomApplication extends Application{

    private static Context context;

    public void onCreate(){
        super.onCreate();
        this.context = getApplicationContext();
    }

    public static Context getContext() {
        return instance;
    }
}

Then in your manifest you declare your application name to match your custom class

<application android:name="com.path.to.class.CustomApplication" ...>
Nyx
  • 2,233
  • 1
  • 12
  • 25
  • But now will this be able to identify each activities context? Because the reason I am needing this context is in orde rto call the cancelRequests see: http://loopj.com/android-async-http/doc/com/loopj/android/http/AsyncHttpClient.html#cancelRequests(android.content.Context, boolean) – Zapnologica Apr 01 '14 at 07:35
  • 1
    Ahh I see. Unfortunately the only way have cancelRequests work correctly is if you pass in the callers context to make the request and to cancel it. – Nyx Apr 01 '14 at 07:37
  • Thx. Im gona have to do that then. And can i get teh specific context of an activity by the following: `ActivityName.this` – Zapnologica Apr 01 '14 at 07:40
  • You can as long as you call it within a non-static function in your Activity class – Nyx Apr 02 '14 at 00:26
0

I don't believe so, no context would exist within a static method. The only way would be to pass a parameter in. You could get this from an intent, but not a standard static method call. There might be a solution but I highly doubt it.

user1840255
  • 287
  • 1
  • 6
  • 15
0

While this is possible by inspecting the stack trace, this would certainly be more complicating than just passing the context.

There is no simpler solution. Moreover, getting the caller of a method is not recommended for security reasons.

FD_
  • 12,947
  • 4
  • 35
  • 62