getApplicationContext()在服务中返回null [英] getApplicationContext() returns null in a Service

查看:1041
本文介绍了getApplicationContext()在服务中返回null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在自己的进程中有一个Service,我已经在清单中声明了它,

I have a Service in it's own process, I've declared it in manifest like that:

   <service
        android:name="com.discountscatcher.util.PointService"
        android:configChanges="orientation"
        android:process=":pointservice" >
    </service>

但是onStartCommand我试图获取一个ApplicationContext,但是它总是返回null,我能忘记什么?

But onStartCommand I was trying to get an ApplicationContext, but it always returns null, what I can forget?

    public int onStartCommand(Intent intent, int flags, int startId) {
    android.os.Debug.waitForDebugger();
    Context mContext = getApplicationContext();
    return Service.START_STICKY;
}

我这样开始:

    startService(new Intent(getApplicationContext(), PointService.class));

Myactivity OnCreate

有什么想法吗?

推荐答案

您在不同的进程中运行Service,因此它与实际应用程序进程之间没有通信.

You are running your Service in a different process so it has no communication with the Actual Application process..

尝试从您的服务中删除此行

Try to remove this line from your service

android:process=":pointservice"

应用程序完成后,应用程序上下文为空.在大多数情况下,无需在其他过程中运行服务.另外也不推荐.

After your application is finished Application context is becomes null. At most situations there is no need to run the service in a different process. Also it is not recommended.

有关更多信息,请点击此处

For more information check here

  1. 在另一个服务中的服务处理
  2. 服务参考示例2
  1. Service in another process
  2. service reference Example2

这篇关于getApplicationContext()在服务中返回null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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