java - 我如何知道上下文是 Activity 还是 IntentService?

标签 java android multithreading android-intent android-asynctask

我在变量中有上下文,我如何知道上下文是否是intentService?

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

问题是当我想在使用httpclient的intentservice AsyncTask中使用时,因为android不允许在服务中使用并行线程: An asyncTask launched from an onPostExecute of another AsyncTask does not execute properly in API 10

我需要在 IntentService 中使用 httpClient,但与我在 UI 线程中使用的函数相同,并且需要 AsynkTask。

最佳答案

在 Java 中,使用 instanceof 运算符来确定对象是否是某个类或接口(interface)的实例。因此,如果 contextIntentService 的实例(或 IntentService 的某个子类),context instanceof IntentService 将为 true,否则为 false

关于java - 我如何知道上下文是 Activity 还是 IntentService?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31659905/

相关文章:

android - ListViewItem 背景颜色

android - 将 ArrayList<Object> 存储为 JSON SharedPreference 或 SQLite 数据库表?

java - 即使第一个线程使用 ReentrantLock 锁定,第二个线程也会执行

c# - 嵌入Python,用线程代码结束子解释器

java - 在不同用户上创建地理围栏

java - java while 循环中数字的除数

java - 无法返回转换为 String[] 的 Object[]

Java+JSP : Query String handling

Android:注销时清除 Activity 历史记录

c++ - 跨线程函数调用修改变量