Android:如何在 IntentService 中获取调用 Activity 的上下文?

标签 android intentservice

如果我打电话

Intent intent = new Intent(ReadingActivity.this, AdService.class);
startService(intent);       

MyActivity 类的 onCreate 方法中,如何从 onHandleIntent( ) IntentService 类的方法

@Override
protected void onHandleIntent(Intent arg0) {
    // TODO Auto-generated method stub
    ((BookLib) getApplication()).createAd(I need to pass the calling activities context here);
}

最佳答案

how do I get access to MyActivity.this from inside the onHandleIntent method of the IntentService class

你不知道。

createAd() 移动到 Activity 中。如果时间有问题,请使用 AsyncTask 而不是 IntentService

IntentService 主要用于您希望工作与任何 Activity 分离的情况(例如,即使用户离开您的 UI,文件下载也应该继续继续做其他事情)。

关于Android:如何在 IntentService 中获取调用 Activity 的上下文?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11727856/

相关文章:

android - 不能将 Class.forName() 与 v4.app.Fragment 一起使用

java - 单击 ListView 项目时应用程序不断崩溃

android - 奇怪 - Android IntentService 杀死了我应用程序的其余部分,但继续自行运行

android - 手动结束 IntentService 工作线程

android - Android同一个View中如何同时使用ScrollView和Horizo​​ntalScrollView

java - 保存图像时找不到文件

java - 屏幕顶部和底部的选项卡

Android:来自 IntentService 的 Toast 永远保留在屏幕上

Android传感器和线程

android - 通过 wifi direct 发送多个文件