后台服务上的Android getContext [英] Android getContext on a background Service

查看:93
本文介绍了后台服务上的Android getContext的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个 Service ,即使我的应用程序关闭,该服务也可以运行.但是,我需要在此 Service 中使用我的应用程序 Context .当应用程序运行时,该服务也可以正常运行,但是当我关闭应用程序(调用onDestroy())时, getContext()总是返回 null .

服务

 公共类SubscribeService扩展Service {私有上下文上下文;@Override公开IBinder onBind(意图){返回null;}@Override公共无效onCreate(){super.onCreate();上下文=此;//当服务在后台运行时返回nullcontext = MyApp.getContext();//也为null}@Overridepublic int onStartCommand(Intent intent,int flags,int startId){//使用上下文执行操作} 

MyApp

 公共类MyApp扩展了Application {私有静态上下文上下文;公共静态上下文getContext(){返回context.getApplicationContext();}@Override公共无效onCreate(){context = getApplicationContext();super.onCreate();}} 

服务从Activity onCreate()开始

  startService(new Intent(this,SubscribeService.class)); 

在这种情况下我应该如何使用 Context ?

修改

Onik 的帮助下,

设法使其正常运行.我只需要在 super.onCreate(); 之前调用 MyApp.getContext(); 像这样:

  @Override公共无效onCreate(){context = MyApp.getContext();super.onCreate();} 

解决方案

服务扩展了上下文.您可以使用 this ,其中 this 是对 Service 实例的引用.

在下面有关 SubscribeService 类的以下代码的评论中添加更多详细信息:

  @Override公共无效onCreate(){super.onCreate();上下文=此;context = MyApp.getContext();} 

在您的 Service onCreate() context =根据基本的编程范例,此不能为 null

I'm trying to create a Service that runs even when my app is closed. However, I need to use my app Context inside this Service. When the app is running, the service works as well, but when I close the app (onDestroy() was called), the getContext() always returns null.

Service

public class SubscribeService extends Service {

    private Context context;

    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public void onCreate() {
        super.onCreate();
        context = this; //Returns null when service is running on background
        context = MyApp.getContext(); //Also null
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        //do stuff using context
    }

MyApp

public class MyApp extends Application {

    private static Context context;

    public static Context getContext() {
        return context.getApplicationContext();
    }

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

Service start from Activity onCreate()

startService(new Intent(this, SubscribeService.class));

How should I use the Context in this scenario?

Edit

Managed to get it to work properly after Onik's help. I just had to call the MyApp.getContext(); before super.onCreate(); Like so:

@Override
public void onCreate() {
    context = MyApp.getContext();
    super.onCreate();
}

解决方案

Service extends Context. You can use this, where this is the reference to the Service instance.

Putting more details on my comment below regarding the following code of SubscribeService class:

@Override
public void onCreate() {
    super.onCreate();
    context = this;
    context = MyApp.getContext();
}

In your Service's onCreate() context = this cannot be null by a fundamental programming paradigm.

这篇关于后台服务上的Android getContext的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆