1

I have the context in a variable, how I can know if the context is intentService or not?

public void syncToServer(Context context){
    if ( is intentService ){
        hpptClientSend();
    }else{
        hpptClientAsyncSend();
    }
}

The problem is when I want to use within a intentservice AsyncTask using httpclient since android does not allow a parallel thread within a service: An asyncTask launched from an onPostExecute of another AsyncTask does not execute properly in API 10

I need use httpClient into IntentService but same function i used in UI thread and need AsynkTask.

Community
  • 1
  • 1
e-info128
  • 3,727
  • 10
  • 40
  • 57
  • How you are passing Intent object as a parameter to `syncToServer`? – ρяσѕρєя K Jul 27 '15 at 17:52
  • look at what thread you are on http://stackoverflow.com/questions/11411022/how-to-check-if-current-thread-is-not-main-thread – njzk2 Jul 27 '15 at 17:52
  • 2
    "how I can know if the context is Intent or intentService?" -- `Intent` does not extend `Context`. – CommonsWare Jul 27 '15 at 17:52
  • njzk2 Thanks :) works fine. CommonsWare you're right, it is activity or intentservice. ρяσѕρєя its very hard, there are many functions that depend on it, you would have to modify all functions of all kinds to achieve this although it is possible. – e-info128 Jul 27 '15 at 17:55

1 Answers1

1

In Java, use the instanceof operator to determine if an object is an instance of some class or interface. So, context instanceof IntentService will be true if context is an instance of IntentService (or some subclass of IntentService), false otherwise.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • Is there another way to get the instance type directly from the context object? So rather than ask, "Is this context instance of type A, B, C...?" you ask, "What instance type are you?" – CodeNovice May 14 '20 at 13:36
  • @CodeNovice: You can call `getClass()` on a Java object to return its `Class`, if that is what you mean. – CommonsWare May 14 '20 at 13:43