getApplicationContext()在服务中返回null [英] getApplicationContext() returns null in a Service
问题描述
我在自己的进程中有一个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
- Service in another process
- service reference Example2
这篇关于getApplicationContext()在服务中返回null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!